Jump to content

Delete a file using unlink does not work


angelali

Recommended Posts

@ 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  :shrug:).

 

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

//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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.