Page 1 of 1

Still need help deleting files

Posted: Mon May 31, 2010 6:33 pm
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);
}

?>

Re: Still need help deleting files

Posted: Mon May 31, 2010 6:42 pm
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);
}

?>

Re: Still need help deleting files

Posted: Tue Jun 01, 2010 12:01 am
by epadgett
I tried it and it's printing Array(). So does that mean that the array is empty?
Thanks

Re: Still need help deleting files

Posted: Tue Jun 01, 2010 1:44 am
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!