Click to See Complete Forum and Search --> : Questions from a PHP wannabe...


Joshie the CK
03-27-2001, 11:59 PM
Question for all the PHP... rabbi's? Monks is already taken... no.. I get it.. PHP priests.. :D

anyway, my Dad needed me to hack up a simple upload script, since the BOFH at work won't give him FTP access (so much for perks to being a manager..), so I hacked one up and it worked just find and dandy...
So I think to myself "hey this is cool. Let's see what else we can do with this." and add another page that lists the files in the directory.. Pretty snazzy... Next, the script shows filesizes...

Then I had to go and get fancy... Now I want to add a delete button to each file...

Help! I've tried 13 ways to sunday to get this to work!

Here's the code: (I know it's not elegant, hell, half of it probably isn't even right... but it works...)

$dh = opendir( $file_dir );
while ($file = readdir( $dh)) {
if (($file !=".") && ($file != ".."))
{
print "<tr>\n<td><a href=\"$file_url$file\">$file</a></td>\n<td><div align=\"right\">";
print filesize( "$file_dir$file" );
print "</div></td>\n<td>";
print "This is where the delete "button" should go";
print "</td></tr>\n\n";
}
}

(obviously the $file_dir variable is being defined elsewhere. Don't worry.)
I know about unlink thank you. Painfully and well aware of it... It's figuring out a way to implement it that's messing me up...
Ideally I think I'd like just a hyperlink. Although I could deal with a "submit" style button...
And right now I'm off to look into using checkboxes...

The thing that keeps screwing me up though, (I think) is that $file is different in each case.
But that seems to work for displaying the filesize, so that might not be it...
The other mess, is figuring how to make it so it doesn't try to unlink the file 'till AFTER I've clicked "delete".

Thanks for any help...

[ 27 March 2001: Message edited by: Joshie the CK ]

Fandelem
03-28-2001, 02:24 PM
okay.. whipped up a quick script.. you should be able to run this and get an idea.. it's a variation, however, because the original script i have edit's the contents of files.. and it also will start at $directory root and will allow you to delete files within subdirectories as well.. but i added a delete feature.. but you should get the idea.. (it would be super super small if it was just delete..)


<?php

#############
#
# you must have the correct permissions to delete files!!
#
#############
// this is the path to this script.
$home = "http://www.fandelem.com/tim/test.php";

############################
#
# edit_content (this is just an html page to display certain files for editing)
#
############################

if ( $admin == "edit_content" ) { // let's find out all the files for editing
// this is the base 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 Or Delete</h1>
directory: <font size=+1>$directory</font><br><br>
<select name=\"filename\">";
clearstatcache($filename); // okay clear last file cached
$dirhandle = opendir($directory);
while($filename = readdir($dirhandle)) {
if ( !is_dir($filename) ) {
print("<option value=\"$filename\">$filename</option>");
}
}
closedir($dirhandle);
echo "
</select>
<input type=\"hidden\" name=\"confirm\" value=\"yes\">
<input type=\"hidden\" name=\"edit_content_1\" value=\"yes\">
<input type=\"submit\" name=\"admin\" value=\"Edit\">
<input type=\"submit\" name=\"admin\" value=\"Delete\">
</table>
</table>
</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") && ($admin == "Edit") ) { // 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=\"confirm\" value=\"yes\">
<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\">
<input type=\"submit\" name=\"admin\" value=\"Delete\">
</table>
</table>
</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 .= '/';
}
// i personally don't want anyone editing .php files over the web.. so this fixes that.
if ( !eregi("\.php$", $filename) ) {
$file_edit .= $filename;
}
else {
echo "sorry, editting .php files isn't allowed right now.";
exit;
}

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>
</table>
</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';
$fp = fopen("temp.html", "w");
fwrite ($fp, stripslashes($edit_area), strlen($edit_area));
fclose($fp);
copy("temp.html",$file_to_edit);
chmod($file_to_edit, 0777);
}
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

#############################################
#
# **THIS WILL DELETE THE SPECIFIED FILE**
#
#############################################

else if ( ($admin == "Delete") && ($confirm == "yes") ) { // okay we got the goahead

// incoming $filename
$directory = "/home/kdavis/www/tim/"; // no special permissions needed
chdir($directory); // make sure we're in the same cwd
$new_file = $directory;
$new_file .= $filename; // this should now be $directory/$filename

echo "Deleting $new_file...";
$delete = unlink($new_file);
if ($delete) {
echo "complete.";
}
else {
echo "failed.";
}


}

#####################
# default displaying page

else {
// you need to subsitute your domain & script here, then keep the ?admin=edit_content portion
echo "<a href=\"$home?admin=edit_content\">File Menu</a>";
}

?>


hope this helps,

~kyle

Sweede
03-28-2001, 07:28 PM
the script above has a lot of HTML errors such as missing table end tags and the opening HTML tag, double tags and missing "'s

but other than that, it should work fine.

this is all in one :D

plus its very good code http://www.suneworld.com/programs/

Joshie the CK
03-28-2001, 11:35 PM
Thanks a HEAP guys!!!
This'll be a big help!!