Still need help deleting files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
epadgett
Forum Newbie
Posts: 7
Joined: Sun May 30, 2010 6:09 pm

Still need help deleting files

Post by epadgett »

I am trying to delete files that have been uploaded to the server. Folder name is upload. Here is the first script which displays the filenames in the folder. This works correctly.

Code: Select all

<?

    // Define the full path to your folder from root
    $path = "/../upload/";

    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");

    // Loop through the files
    while ($file = readdir($dir_handle)) {

    if($file == "." || $file == ".." || $file == "index.php" || $file == "upload.php" || $file == "index2.php") 

continue;
        echo "<a href=\"$file\">$file</a><input type=\"checkbox\" name=\"delete[]\" value=\"$file\" /><br />";
}
closedir($dir_handle);
?>



<h5>Delete file from upload directory</h5>


<form action="delete2.php" method="POST">   <input type="submit" value="Delete" /> </form> 







Here is the script to delete the files but I've changed it to echo the files to see if they are being passed from the form and It just echoes .* and if I remove that line of code it echoes the directory name which is upload.


Code: Select all

<?php

$files = $_POST['delete']; 
$path = "/upload/$files";
$path = "/upload/$files.*";
if ($file = basename($path));{

 echo($file);
}

?>
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Still need help deleting files

Post by phdatabase »

Try this:

Code: Select all

<?php

echo"<pre>";
print_r($_POST);
echo"</pre>";

$files = $_POST['delete'];
$path = "/upload/$files";
$path = "/upload/$files.*";
if ($file = basename($path));{

 echo($file);
}

?>
epadgett
Forum Newbie
Posts: 7
Joined: Sun May 30, 2010 6:09 pm

Re: Still need help deleting files

Post by epadgett »

I tried it and it's printing Array(). So does that mean that the array is empty?
Thanks
epadgett
Forum Newbie
Posts: 7
Joined: Sun May 30, 2010 6:09 pm

Re: Still need help deleting files

Post by epadgett »

Got it. The problem was I was ending the script before the actual code for the form was running. I was using echo to display the checkbox and after I ended the script is where I had the actual form code. Backwards sort of. Simple! Thanks to everyone!
Post Reply