Reputation: 2824
I use following code to delete old image from ftp...
unlink(getcwd() . '/images/' . $row['pic']);
But it throws errors in case there is not image, so i have tried using file_exists() but that didn't work too, so how can i check if there is an image before trying to delete. thanks.
if(file_exists(getcwd() . '/images/pics/' . $row['pic']))
{
unlink(getcwd() . '/images/pics/' . $row['pic']);
}
Upvotes: 3
Views: 6123
Reputation: 15
I had the same problem. I allowed the user to upload UP TO 8 images. But let's say they only uploaded four... Then deleted the post. I had an error for the other four images because they didn't exist. So this is what I did...
$GetImages = "SELECT * FROM sales_images WHERE ID = $imgID";
$GotImages = $conn->query($GetImages);
while($pic = $GotImages->fetch_assoc())
{
$path = '../images/sales/';
$img1 = $pic['img1'];
$img2 = $pic['img2'];
$img3 = $pic['img3'];
$img4 = $pic['img4'];
$img5 = $pic['img5'];
$img6 = $pic['img6'];
$img7 = $pic['img7'];
$img8 = $pic['img8'];
}
if (!empty($img1)) {
unlink($path . $img1);
}
if (!empty($img2)) {
unlink($path . $img2);
}
if (!empty($img3)) {
unlink($path . $img3);
}
if (!empty($img4)) {
unlink($path . $img4);
}
if (!empty($img5)) {
unlink($path . $img5);
}
if (!empty($img6)) {
unlink($path . $img6);
}
if (!empty($img7)) {
unlink($path . $img7);
}
if (!empty($img8)) {
unlink($path . $img8);
}
This worked great and threw no errors. Hope it helps :)
Upvotes: 1
Reputation: 10283
First you can store the image path in a $path
variable. Then you may test if the image is there and it's writable, and only then, delete it.
$path = getcwd() . '/images/pics/';
if( file_exists( $path . $row['pic'] ) && is_writable( $path . $row['pic'] ) ){
unlink( $path . $row['pic'] );
}
If you are using PHP 5 and want to know more about any exception that may be thrown, in the meantime you can surround the whole expression with a try...catch
statement:
try{
$path = getcwd() . '/images/pics/';
if( file_exists( $path . $row['pic'] ) && is_writable( $path . $row['pic'] ) ){
unlink( $path . $row['pic'] );
}
} catch (Exception $exc) {
echo $exc->getMessage();
echo $exc->getTraceAsString();
}
This probably won't solve the problem, but may help clarifying why it is happening.
Upvotes: 3
Reputation: 4996
often hosters use different user for ftp and apache… could it be, that you don't have chmodded your images, so the www-user can't delete them?
edit:
maybe is_writable is the better solution for you:
if(is_writable(dirname(__FILE__) . '/images/pics/' . $row['pic'])){
unlink(dirname(__FILE__) . '/images/pics/' . $row['pic']);
}
Upvotes: 3
Reputation: 5478
First check what getcwd() is returning. Probably false, so that means you do not have the right permissions set. Therefore your path is not correctly constructed. Check getcwd() docs, change permissions or contact your system administrator. Also take a snap at dirname(__FILE__)
Upvotes: 3
Reputation: 641
Try to add @ in front of unlink(getcwd() . '/images/' . $row['pic']);
@unlink(getcwd() . '/images/' . $row['pic']);
If there is a file it would be deleted, and if there is not you will not get a error message.
Upvotes: -4