Luke Warm Posted November 11, 2009 Share Posted November 11, 2009 I'm working on a project to upload files. The uploading is working fine. I want to add a button to delete individual files loaded into a list. I've tried the unlink() function to no avail. I've attached my code. Any help would be appreciated... [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/ Share on other sites More sharing options...
dreamwest Posted November 11, 2009 Share Posted November 11, 2009 You need the absolute path /home/user/htdocs/uploads/ and $_FILES["file"]["name"] if(unlink("/home/user/htdocs/uploads/".$_FILES["file"]["name"])){ echo 'Deleted original file!'; } Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-955860 Share on other sites More sharing options...
Luke Warm Posted November 12, 2009 Author Share Posted November 12, 2009 I took your advice and created a function from the code you sent but it still doesn't work. I inserted the function into the page PHP code. Did not create a separate "include" file, if that makes a difference. Here's the function: function deleteMe() { if(unlink("/var/www/html/uploads/".$_FILES["file"]["name"])){ echo 'Deleted original file!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-956299 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 I've added this code to the page: // Delete file when button clicked if(isset($_GET['delete'])) { unlink("uploads/".$image); echo "<center>File deleted!</center>"; } With a link added for each file to be deleted: <a href='imageUploads.php?delete=$image'>Delete</a> It deletes ALL the images in the directory instead of just one. NOTE: "uploads" is the directory where the files are stored... Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-956995 Share on other sites More sharing options...
xcoderx Posted November 13, 2009 Share Posted November 13, 2009 i guess u must also specify file name/id ? Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957004 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 Exactly how do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957009 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 I would echo out your unlink path+file to make sure you are actually getting the correct location/file. By the way the $_FILES array only is used when there is a file uploaded from your script via POST. If the file has already been uploaded your sever previously, it wont be in $_FILES. Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957019 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 When I put my mouse over the "delete" link, it shows the correct path: imagesUpload.php?delete=imagename.jpg Even if I change the code so "imagesUploads.php?delete=" shows full path, still deletes all files in the directory. Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957027 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 When I put my mouse over the "delete" link, it shows the correct path: imagesUpload.php?delete=imagename.jpg Even if I change the code so "imagesUploads.php?delete=" shows full path, still deletes all files in the directory. Looks like you never define $image. so unlink is removing all your files. I would use is_file() function along with unlink() <?php $path = "Whatever your path is"; $image = $_GET['delete']; $pf = $path.$image; if(is_file($pf)){ unlink($pf); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957041 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 $path = "/var/www/html/uploads"; $image = $_GET['delete']; $pf = $path.$image; if(is_file($pf)){ unlink($pf); } <a href='imageUploads.php?delete=$image'>Delete</a> Getting: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in imagesUpload.php Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957045 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 $path = "/var/www/html/uploads"; $image = $_GET['delete']; $pf = $path.$image; if(is_file($pf)){ unlink($pf); } <a href='imageUploads.php?delete=$image'>Delete</a> Getting: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in imagesUpload.php Where is the error occurring? by the way $path = "/var/www/html/uploads/"; Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957053 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 It's occurring in the line: $path = "/var/www/html/uploads/"; Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957057 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 It's occurring in the line: $path = "/var/www/html/uploads/"; What are the few lines above it Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957059 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 This is the start of the php: <?php # This script lists the images in the "uploads" directory. require_once ('includes/functions.inc.php'); $dir = 'uploads/'; // Define the directory to view. $files = scandir($dir); // Read all the images into an array. // Display each image caption as a link to the JavaScript function: foreach ($files as $image) { if (substr($image, 0, 1) != '.') { // Ignores anything starting with a period. // Get the image's size in pixels: $image_size = getimagesize ("$dir/$image"); // Calculate the image's size in kilobytes: $file_size = round ( (filesize ("$dir/$image")) / 1024) . "kb"; // Make the image's name URL-safe: $image = urlencode($image); This is where the "$path" statment begins... Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957063 Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 Hi, what I gave you is the delete procedure. it should be in your delete function/file other than that I dont see what would cause your new error Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957080 Share on other sites More sharing options...
Luke Warm Posted November 13, 2009 Author Share Posted November 13, 2009 I placed the code in my includes file as a function, getting error from that file now. Thanks anyway... Quote Link to comment https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.