Click to See Complete Forum and Search --> : html > view all files in current directory
Nehal Mistry
03-24-2001, 06:21 PM
is there a php/html script to view all the files in the current directory, cos on one of my webpages, the server only views filename not file size when u dont have an index.html file....
thx... ps, i prefer html script... but either is fine... ;)
Super Bakemono
03-24-2001, 10:53 PM
Do you know php? Or perl? You could write a script to run ls and generate an html file based on the output, that's what I would do =]
Nehal Mistry
03-25-2001, 12:36 AM
i dont know how, so if anyone can please tell me how!
Ben Briggs
03-25-2001, 10:33 PM
Something like this should work...
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
my $cgi = new CGI();
print $cgi->header, $cgi->start_html("Directory Listing");
print $cgi->hr;
print $cgi->br;
print `ls -h -l -a`;
print $cgi->br;
print $cgi->hr;
print $cgi->end_html;
That should work... I didn't debug (or even test it), but it's pretty simple :).
[ 25 March 2001: Message edited by: Ben Briggs ]
Fandelem
03-25-2001, 11:41 PM
welp, this was on my list of to-do's for a website i've been working on.. so i decided to go ahead and get it done now and help both of us out ;o)
(disclaimer: i've only been working with php for a less than a week)
here is the code that will:
1. be accessed by http://www.yourdomain.com/file.php?admin=edit_content
2. then, once you change $directory (only) in ($admin == "edit_content") ifblock it will dynamically create a dropdown of every file and directory *from* the directory specified in $directory.
3. based on if you choose a file or directory (which the script figures out) it will either bring you to another dropdown box identical to the first one, or it will display a textbox where you can edit the contents and save them (to the original file or a different one).
any other questions or confusion, let me know.. i'm sure i messed up somewhere along the line ;o)
(it's rather long.. sorry.. html and all ;o)
############################
#
# edit_content (this is just an html page to display certain files for editing)
#
############################
else if ( $admin == "edit_content" ) { // let's find out all the files for editing
// this is the directory to get file contents
$directory = "/home/kdavis/www/tim/"; // no special permissions needed
echo "
<head>
<SCRIPT LANGUAGE=\"javascript\">
<!-- hide
function passCheck(){
return true;}
// unhide -->
</SCRIPT>
</head>
<body>
<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#769257\" vlink=\"#769257\" alink=\"#769257\">
<table border=0 cellpadding=2 cellspacing=0><tr><td bgcolor=ffffff>
<table border=1 bordercolor=169257 cellpadding=8 cellspacing=0>
<FORM ACTION=\"$PHP_SELF\" NAME=generic METHOD=post onSubmit=\"return passCheck()\">
<h1>Pick A File To Edit</h1>
directory: <font size=+1>$directory</font><br><br>
<select name=\"filename\">";
clearstatcache($dirhandle); // okay clear last file cached
$dirhandle = opendir($directory);
while($filename = readdir($dirhandle)) {
// okay this basically excludes "." and ".." directories from our box
print("$filename (filename)");
if ( !is_dir($filename) ) {
print("<option value=\"$filename\">$filename</option>");
}
}
closedir($dirhandle);
echo "
</select>
<input type=\"hidden\" name=\"edit_content_1\" value=\"yes\">
<input type=\"submit\" value=\"Edit\">
</table>
</form>
</html>
";
} // end edit_content ifblock
############################
#
# edit_content (this will either display the file contents or if they select a directory another dropdown)
#
############################
else if ( $edit_content_1 == "yes" ) { // let's edit the file contents or check for another directory.
// incoming: $filename
$directory = "/home/kdavis/www/tim/"; // no special permissions needed
chdir($directory); // make sure we're in the same cwd
$new_directory = $directory;
$new_directory .= $filename;
$new_directory .= '/'; // this should now be $directory/$filename/
if ( is_dir($filename) ) { // okay this means it's a directory-- give another dropdown box.
echo "
<head>
<SCRIPT LANGUAGE=\"javascript\">
<!-- hide
function passCheck(){
return true;}
// unhide -->
</SCRIPT>
</head>
<body>
<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#769257\" vlink=\"#769257\" alink=\"#769257\">
<table border=0 cellpadding=2 cellspacing=0><tr><td bgcolor=ffffff>
<table border=1 bordercolor=169257 cellpadding=8 cellspacing=0>
<FORM ACTION=\"$PHP_SELF\" NAME=generic METHOD=post onSubmit=\"return passCheck()\">
<h1>Pick A File To Edit</h1>
directory: <font size=+1>$new_directory</font><br><br>
<select name=\"filename\">";
clearstatcache($dirhandle); // okay clear last file cached
$dirhandle = opendir($new_directory);
while($filename = readdir($dirhandle)) {
// okay this basically excludes "." and ".." directories from our box
// i only want to include .html files in this (remove it if you don't)
if ( !is_dir($filename) ) {
print("<option value=\"$filename\">$filename</option>");
}
}
closedir($dirhandle);
echo "
</select>
<input type=\"hidden\" name=\"edit_content_1\" value=\"yes\">
<input type=\"hidden\" name=\"old_dir\" value=\"{$HTTP_POST_VARS['filename']}\">
<input type=\"submit\" value=\"Edit\">
</table>
</form>
</html>
";
} // end if_dir block
else {
// $old_dir should be concatenated
$file_edit = $directory;
$file_edit .= $old_dir;
if ( $old_dir != "" ) {
$file_edit .= '/';
}
$file_edit .= $filename;
echo "
<head>
<SCRIPT LANGUAGE=\"javascript\">
<!-- hide
function passCheck(){
return true;}
function Cancel(){
location = \"$home?admin=edit_content\";
}
// unhide -->
</SCRIPT>
</head>
<body>
<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#769257\" vlink=\"#769257\" alink=\"#769257\">
<table border=0 cellpadding=2 cellspacing=0><tr><td bgcolor=ffffff>
<table border=1 bordercolor=169257 cellpadding=8 cellspacing=0>
<FORM ACTION=\"$PHP_SELF\" NAME=generic METHOD=post onSubmit=\"return passCheck()\">
<h1>Edit Mode</h1>
file: <font size=+1><input type=\"text\" value=\"$file_edit\" size=\"40\" name=\"file_edit\"></font><br><br>
";
clearstatcache($filehandle); // okay clear last file cached
if($filehandle = fopen("$file_edit", "r+")) {
$full = "";
while(!feof($filehandle))
{
$file_contents = fgets($filehandle, 4096);
$full .= $file_contents;
}
fclose($filehandle);
} else {
print('Sorry, I could not open this file! Check that the permissions are set properly
for this file and that you used the correct absolute path to the file.');
}
echo "<TEXTAREA COLS=79 ROWS=30 name=\"edit_area\" WRAP=VIRTUAL>$full</TEXTAREA>
<input type=\"hidden\" name=\"edit_content_done\" value=\"yes\">
<input type=\"submit\" value=\"Save\"><input type=\"button\" value=\"Cancel\" onclick=\"Cancel()\">
</table>
</form>
</html>
";
} // end else block (editmode)
} // end edit_mode completely..
##############################
#
# edit_content_done (this will re-write the file and save the changes)
#
#############################
else if ( $edit_content_done == "yes" ) { // ok let's make the changes
// incoming: edit_area
clearstatcache($filehandle); // okay clear last file cached
$file_to_edit = $HTTP_POST_VARS['file_edit'];
if ( !file_exists($file_to_edit) ) { // this is just for proper output
$made_file = '1';
}
if($filehandle = fopen("$file_to_edit", "w+")) {
fwrite ($filehandle, stripslashes($edit_area), strlen($edit_area));
fclose($filehandle);
echo "<font size=+2>$file_to_edit</font> has been ";
if ($made_file == '1') {
echo "created and ";
}
echo "modified.<p><a href=\"$home\">Return To Main</a>";
}
else
echo "Failed to modify file.";
} // end edit_content_done ifblock
if i could help anyone, all the better.. if not.. sorry.. just ignore me ;o)
~kyle
[ 25 March 2001: Message edited by: Fandelem ]