|
-
Oct 25th, 2006, 07:24 AM
#1
Thread Starter
Frenzied Member
Displaying directories and files using PHP
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
-
Oct 25th, 2006, 12:37 PM
#2
Re: Displaying directories and files using PHP
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>';
?>
-
Oct 25th, 2006, 01:17 PM
#3
Thread Starter
Frenzied Member
Re: Displaying directories and files using PHP
wow works like a charm, however is there one to list files aswell?
-
Oct 25th, 2006, 05:05 PM
#4
Re: Displaying directories and files using PHP
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)
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";
?>
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.
Last edited by kows; Oct 25th, 2006 at 05:15 PM.
-
Oct 27th, 2006, 06:57 AM
#5
Re: Displaying directories and files using PHP
Um, why not just use Apache's indexing? Would be a damn sight more efficient.
Also, never use echo() to output HTML code.
-
Oct 27th, 2006, 02:44 PM
#6
Re: Displaying directories and files using PHP
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.
 Originally Posted by penagate
never use echo() to output HTML code.
and why is that?
Last edited by kows; Oct 27th, 2006 at 02:48 PM.
-
Oct 28th, 2006, 02:26 AM
#7
Re: Displaying directories and files using PHP
 Originally Posted by kows
you usually create a custom indexing thing to be able to do things the default apache one isn't really capable of.
Well duh, but he said "just like Apache".
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|