adam291086 Posted October 1, 2007 Share Posted October 1, 2007 I have this page that allows me to delete a picture from a folder. When i press delete the error message Error deleting photo! Please try again. This is an echo message from my code. I dont know what to change to get the file to delete. Heres the code below <?php include_once("config.php"); // No album id has been selected if( !$_GET['photo_id'] ){ // Display error message to user $msg .= "Photo not selected. Please choose a photo you wish to delete!"; $msg .= "<br /><a href=\"edit_photos.php\">Edit photos</a>"; displayPage($msg, "Error Selecting Photo"); } else { con; // Retrieve image and thumbnail path information $sql = "SELECT photo_location, thumbnail_location FROM photos WHERE photo_id = " . addslashes($_GET['photo_id']); $result = @mysql_query($sql) or die("Error retrieving records: " . mysql_error()); while ($row = mysql_fetch_array($result)){ $photo_location = $row['photo_location']; $thumb_location = $row['thumbnail_location']; } if (@unlink("../photos/" . $photo_location)){ } else { die("Error deleting photo!<br /><a href=\"index.php\">Please try again</a>."); } if (@unlink("../thumbs/" . $thumb_location)){ } else { die("Error deleting thumbnail!<br /><a href=\"index.php\">Please try again</a>."); } // Delete specified album $sql = "DELETE FROM photos WHERE photo_id = " . addslashes($_GET['photo_id']); $result = @mysql_query($sql) or die("Error deleting record: " . mysql_error()); // Display success to user $msg .= "Photo has been successfully deleted!<br /><a href='index.html'>Return to Admin Menu</a>"; displayPage($msg, "Photo Deleted!"); } ?> Sorry about the layout, im using a works computer without an editor. Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/ Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Does php have permission to delete these files? Often, unless the files where uploaded via php, you will not have permissions to remove them. Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359109 Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 It doesnt like this part of the code if (@unlink("../" . $photo_location)){ } else { die("Error deleting photo!<br /><a href=\"index.php\">Please try again</a>."); } In other words, it cant delete ../$photo_location try adding echo $photo_location; to your script to make sure its a valid image name Also, is the image dir a level below the folder your script is in, hence the ../ Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359113 Share on other sites More sharing options...
adam291086 Posted October 1, 2007 Author Share Posted October 1, 2007 The files are uploaded via php and i have set the Chmod value to 777 which is everything Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359114 Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 try creating a small script that simple does unlink("../theimagename.jpg"); If that doesnt work, permissions will be off If it does work, then I bet the variable storing the image name is NULL Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359117 Share on other sites More sharing options...
adam291086 Posted October 1, 2007 Author Share Posted October 1, 2007 I have echoed the image locations and they are present and correct. Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359119 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Well, the best you can do is follow jaymc's advice. Id'e also remove the @ error supressor to see if you can get a decent error message. Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359120 Share on other sites More sharing options...
adam291086 Posted October 1, 2007 Author Share Posted October 1, 2007 Thanks guys all working now. Did the little script and found out the location of the pictures were wrong. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359123 Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 Quote Link to comment https://forums.phpfreaks.com/topic/71368-solved-deleting-files-from-a-folder-using-php/#findComment-359129 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.