Help to delete uploaded 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

Help to delete uploaded files

Post by epadgett »

Hello! I made a script to allow trusted members of my site to upload files and store on the server. The script allows the member to upload a file, it then sets the permission and stores it in the designated folder and displays the contents of the folder as links as well as displays the upload form to allow another upload. The script also updates a database that I will use for another purpose. This script is working perfect. My problem is I am trying to create a script that will display a checkbox next to the file links that are in the folder and allow the user to check the box and then delete that file from the server. The script will have to go through the checkbox array and then delete the files that are checked. I've been trying to use the unlink function, but I am lost. Any help would be appreciated. Below is the code for the upload incase you need it. It is the first part of a 3 step process. But again, all this works fine and I just need to delete a file with a user selected checkbox.
Thank you,
Jerry

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><br />";
}
closedir($dir_handle);
?>




<h5>Upload new file to upload directory</h5>


<form enctype="multipart/form-data" action="upload.php" method="POST"> Please choose a file: <input name="uploaded" type="file" /><br /> <input type="submit" value="Upload" /> </form> 




User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help to delete uploaded files

Post by requinix »

Use this HTML for the checkbox:

Code: Select all

<input type="checkbox" name="delete[]" value="file-name-goes-here.ext" />
Then $_POST["checkbox"] will be an array of all the files to delete. Use basename on each and make sure it exists in the directory before trying to delete it.
epadgett
Forum Newbie
Posts: 7
Joined: Sun May 30, 2010 6:09 pm

Re: Help to delete uploaded files

Post by epadgett »

Ok, on the html form code, how do I pass the filename where it says name of file here so that I can do it dynamically. I need the scrtipt to pass the name to it and as of now the script is looping through and using the $file variable. And, is unlink the way to go? or is there a different way?
Thanks
Jerry
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help to delete uploaded files

Post by requinix »

My post told you how to pass the filenames back.

And yes, to delete files use unlink().
epadgett
Forum Newbie
Posts: 7
Joined: Sun May 30, 2010 6:09 pm

Re: Help to delete uploaded files

Post by epadgett »

Well, I tried it but not having luck yet. Some reason it's not passing the file name with it but I'm sure it's just me. I have'nt used the basename yet I was hoping to get it running first then add that I figured that was for security on the script not hangin up. I'll keep working on it. Thanks for your help,
Jerry
lankke
Forum Newbie
Posts: 1
Joined: Mon May 31, 2010 12:23 am

Re: Help to delete uploaded files

Post by lankke »

Can you post you latest bunch of code.

I will be able to help you out.
epadgett
Forum Newbie
Posts: 7
Joined: Sun May 30, 2010 6:09 pm

Re: Help to delete uploaded files

Post by epadgett »

Here is the first page of code this is where I show the files in the directory along with a checkbox with each file linked.

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>Upload new file to upload directory</h5>


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




Here is my next page of code but as you see I didn't get too far, I had to take a break from it. I've tried other things but this is what it ended up being before I stopped, I know I got some work to do still. Everything works fine with displaying the files and uploading to the server but I don't know how to delete them. Thanks!

Code: Select all


<?php

$delete=$_POST['delete'];

foreach($delete as $value) {
	unlink($value);
} 
?>

Post Reply