Right heres a good one, so good i dont know where to start :P
Just like Apache, i want to be able to display the directories and files of my webserver using PHP.
Examples in the images attached
Printable View
Right heres a good one, so good i dont know where to start :P
Just like Apache, i want to be able to display the directories and files of my webserver using PHP.
Examples in the images attached
this is basically what you can do. there's plenty of things you can do to make it able to browse other directories, I could give you an old script I made to look at if you like.
PHP Code:<?php
$dir = "./pictures/";
echo '<h1>' . $dir . '</h1>' . "\n<pre>";
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) !== false){
$nfile = $dir . $file;
echo ' -> <a href="' . $nfile . '">$file</a>' . "\n";
}
}
}
echo '</pre>';
?>
wow works like a charm, however is there one to list files aswell?
it already does list files. it lists all of the files and directories in $dir.
you can check if a file returned by the above code is a directory by using is_dir() (usage: is_dir(filepath)), and then do something else to it. to get a file or directory's last date modified you can use the filemtime() function.
edit from here on -->
oh, and oops: I forgot to escape the $file variable in the code I posted. If you didn't already figure it out, you should change it to this: (I also modified the code a bit to show you distinct directories/files)
this way, all directories are displayed first (logically like Apache does) and all files are displayed last. might be a bit slower than the original because you are storing them in variables and then flushing, but otherwise you can't really sort them. When I do this, I usually trap files in an array so that I can sort them alphabetically or using other array sort functions.PHP Code:<?php
$dir = "./";
$dirs = "";
$files = "";
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) !== false){
$nfile = $dir . $file;
if(is_dir($nfile)){
$dirs .= ' -> DIR: <a href="' . $nfile . '">' . $file . '</a>' . "\n";
}else{
$files .= ' -> FILE: <a href="' . $nfile . '">' . $file . '</a>' . "\n";
}
}
}
}
//output
echo "<h1>$dir</h1>\n";
echo "<h3>directories:</h3>\n<blockquote><pre>\n";
echo $dirs;
echo "</pre></blockquote>\n<h3>files:</h3>\n<blockquote><pre>\n";
echo $files;
echo "</pre></blockquote>\n";
?>
Um, why not just use Apache's indexing? Would be a damn sight more efficient.
Also, never use echo() to output HTML code.
you usually create a custom indexing thing to be able to do things the default apache one isn't really capable of. if you're just listing out every file, then yes, it's more efficient to use apache's. however, if you're building a script that, for example, listed out all of the PHP files in a given directory and let you view the source (without making phps or txt files, just using fopen()), it would be a lot easier to have your own indexing system that looked at the file extension and printed it only if it was something you wanted.
and why is that?Quote:
Originally Posted by penagate
Well duh, but he said "just like Apache".Quote:
Originally Posted by kows
PHP is an embedded language, not the other way round; it goes within the HTML code. Output is what PHP does by default. Using echo() to output instead of just closing the PHP tags is unnecessarily inefficient. Also, you make it harder to spot syntactic errors in your HTML, and you have escape certain characters; it's ugly.Quote:
and why is that?