angelali Posted May 22, 2012 Share Posted May 22, 2012 I want to delete a file, I'm using Windows XP, however, it gives me Permission Denied: $file =$row['img'] ; $filedel = "mages/".$file; unlink ($filedel); Is the code wrong? I tried setting permission to the folder but in vain... Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/ Share on other sites More sharing options...
Alex Posted May 22, 2012 Share Posted May 22, 2012 I'd suggest reading the comments on the php manual page for unlink(), there's a fair amount of discussion about this error and the various things that could cause it there. http://php.net/manual/en/function.unlink.php Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347532 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 I read everything there, I tried almost everything, but it is not deleting the fle. For example, I even put '@' before unlink, but in vain Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347533 Share on other sites More sharing options...
Alex Posted May 22, 2012 Share Posted May 22, 2012 @ is used to for error suppression, so that would hide the error but not fix it. First I'd check to make sure your path is correct. Is it supposed to be "images/" and not "mages/" (just a guess ). After that I'd make sure that this file has proper permissions. This means the file must have write permissions to the directory you're trying to remove the file from. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347534 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 well the path is correct, I name it to 'mages' itself. For the file permission, Im on Windows XP local host, and it's read only the folder. I will not be able to assign chmod on Windows if Im not mistaken! I was trying to change the file permissions but in vain... Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347535 Share on other sites More sharing options...
Alex Posted May 22, 2012 Share Posted May 22, 2012 If you don't know, Google it. A simple google for "windows file permissions" will yield you all you need. http://support.microsoft.com/kb/308419 Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347536 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 I assigned full control on the folder, read, write etc....but the problem remain... :'( Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347539 Share on other sites More sharing options...
Alex Posted May 22, 2012 Share Posted May 22, 2012 The file needs to have the permissions. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347545 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 I applied permissions to folder, files etc.... Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347546 Share on other sites More sharing options...
PFMaBiSmAd Posted May 22, 2012 Share Posted May 22, 2012 It would probably help if you posted the complete actual error message. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347679 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 Warning: unlink(images/) [function.unlink]: Permission denied in C:\xampp\htdocs\troll\includes\images.php on line 54 Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347684 Share on other sites More sharing options...
PFMaBiSmAd Posted May 22, 2012 Share Posted May 22, 2012 Warning: unlink(images/) [function.unlink]: Permission denied in C:\xampp\htdocs\troll\includes\images.php on line 54 Your $file variable is either empty or doesn't exist at all and you are trying to unlink the images/ folder, not a file in that folder. Your code needs to check that you actually have retrieved data from your database before trying to use that data. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347692 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 If I paste the full codes here, will you see them? Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347695 Share on other sites More sharing options...
mrMarcus Posted May 22, 2012 Share Posted May 22, 2012 If I paste the full codes here, will you see them? Yes, but please hurry 'cause the suspense is killing me. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347700 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 As you see, its a file upload system, where images are stored in a folder while its details in database..When the user will click on a button to delete the image, he does that by checking check-boxes, he can delete multiple images by checking check-boxes. The deletion of images in database works PERFECTLY, except it does not delete the respective image/s in the folder... <?php //Connect to database $connect = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('imgdatabase', $connect) or die ('Connection Failed'); //Display images $display = mysql_query("SELECT * FROM photos WHERE email='$reg'"); echo "<form action='images.php' method='post'>"; echo "<table> <tr> <th>#</th> <th>Your images</th> <th>Image names</th> <th><input type='submit' value='Delete'/></th> </tr>"; echo "<hr/>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='upload/".$row['imgame']."' alt='alt text' width='200' height='150' class='thumb'/> </td>"; echo "<td><a href='upload/".$row['imgname']."' target='_blank'>".$row['imgname']."</a></td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; //Delete images details in database if (isset($_POST['delete'])) { foreach ($_POST['delete'] as $delete) { $ids[] = mysql_real_escape_string(strip_tags($delete)); } mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"); //Delete image in folder $file =$row['imgname'] ; $filedel = "images/".$file; unlink ($filedel); echo "File deleted"; } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347726 Share on other sites More sharing options...
mrMarcus Posted May 22, 2012 Share Posted May 22, 2012 It won't as long as you have $file =$row['imgname'] ; outside of your while() loop since $row is no longer available. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347729 Share on other sites More sharing options...
mrMarcus Posted May 22, 2012 Share Posted May 22, 2012 I am also just noticing that you are displaying your images in a different directory than that of the one you're attempting to remove them from: upload/ echo "<td><img src='upload/".$row['imgame']."' alt='alt text' width='200' height='150' class='thumb'/> </td>"; vs images/ $filedel = "images/".$file; Are you sure this is correct? Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347732 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 <?php //Connect to database $connect = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('imgdatabase', $connect) or die ('Connection Failed'); //Display images $display = mysql_query("SELECT * FROM photos WHERE email='$reg'"); echo "<form action='images.php' method='post'>"; echo "<table> <tr> <th>#</th> <th>Your images</th> <th>Image names</th> <th><input type='submit' value='Delete'/></th> </tr>"; echo "<hr/>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='images/".$row['imgame']."' alt='alt text' width='200' height='150' class='thumb'/> </td>"; echo "<td><a href='images/".$row['imgname']."' target='_blank'>".$row['imgname']."</a></td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; //Delete images details in database if (isset($_POST['delete'])) { foreach ($_POST['delete'] as $delete) { $ids[] = mysql_real_escape_string(strip_tags($delete)); //Delete image in folder $file =$row['imgname'] ; $filedel = "images/".$file; unlink ($filedel); } mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"); echo "File deleted"; } mysql_close($connect); ?> Permission Denied again! Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347733 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 No its 'images' I changed it from 'upload' Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347736 Share on other sites More sharing options...
mrMarcus Posted May 22, 2012 Share Posted May 22, 2012 No its 'images' I changed it from 'upload' You should update your code then, no? And like I said, $row is not accessible outside of the while() loop that you have in your script. They are two separate instances, first being the form display, second being if the $_POST['delete'] button has been pressed. Those two blocks of code are not interacting with eachother in the manner that you are obviously thinking. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347737 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 I updated it... Outside the while loop? I did put it inside, see the last ful codes above...is it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347738 Share on other sites More sharing options...
mrMarcus Posted May 22, 2012 Share Posted May 22, 2012 You should really start indenting your code to make it more readable for yourself as it's still outside. However, that block doesn't apply to your deletion block. //Connect to database $connect = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('imgdatabase', $connect) or die ('Connection Failed'); //Display images $display = mysql_query("SELECT * FROM photos WHERE email='$reg'"); echo "<form action='images.php' method='post'>"; echo "<table> <tr> <th>#</th> <th>Your images</th> <th>Image names</th> <th><input type='submit' value='Delete'/></th> </tr>"; echo "<hr/>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='images/".$row['imgame']."' alt='alt text' width='200' height='150' class='thumb'/> </td>"; echo "<td><a href='images/".$row['imgname']."' target='_blank'>".$row['imgname']."</a></td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; //Delete images details in database if (isset($_POST['delete'])) { foreach ($_POST['delete'] as $delete) { $ids[] = mysql_real_escape_string(strip_tags($delete)); //Delete image in folder $file =$row['imgname'] ; $filedel = "images/".$file; unlink ($filedel); } mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")"); echo "File deleted"; } mysql_close($connect); ?> There is your code, as-is, cleaned up. You can now see that $row is not being set when a user tries to delete certain files. Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347740 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 So how to do it, help me... Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347742 Share on other sites More sharing options...
mrMarcus Posted May 22, 2012 Share Posted May 22, 2012 //Connect to database $connect = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('imgdatabase', $connect) or die ('Connection Failed'); //Display images $display = mysql_query("SELECT * FROM photos WHERE email='$reg'"); // are you sanitizing $reg? echo "<form action='images.php' method='post'>"; echo "<table> <tr> <th>#</th> <th>Your images</th> <th>Image names</th> <th><input type='submit' value='Delete'/></th> </tr>"; echo "<hr/>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='images/".$row['imgame']."' alt='alt text' width='200' height='150' class='thumb'/> </td>"; echo "<td><a href='images/".$row['imgname']."' target='_blank'>".$row['imgname']."</a></td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; //Delete images details in database if (isset($_POST['delete'])) { $ids = array_map('mysql_real_escape_string', $_POST['delete']); $sql = "SELECT `imgname` FROM `photos` WHERE `img_ID` IN (". implode(',', $ids) .")"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_row($result)) { unlink('images/'. $row['imgname']); } $del = mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',', $ids).")"); } else { echo 'Please select a file to delete.'; // you can change up your error handling to be showed to the user } } else { trigger_error(mysql_error()); // remove once everything is working } } mysql_close($connect); ?> Edit: had a typo ($res instead of $row) Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347747 Share on other sites More sharing options...
angelali Posted May 22, 2012 Author Share Posted May 22, 2012 Permission denied again I passed 5 hours determining this damn problem, I have even broken 2 bottles because of this... Quote Link to comment https://forums.phpfreaks.com/topic/262911-delete-a-file-using-unlink-does-not-work/#findComment-1347750 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.