2

I am developing application in PHP. There is image folder. I want to delete image from the folder When user click on delete button. My code is running well. But the UNLINK() function not working. My code is

unlink('../photo_gallery/'.$image_name['imge_name'],777);

I am getting this error

Warning: unlink() expects parameter 2 to be resource,

and if i remove 777 then it shows

Warning: unlink(../photo_gallery/): Permission denied

Please suggest.

Rahul Narhe
  • 181
  • 2
  • 14
  • you dont need to pass `777` in unlink. set your `photo_gallery` folder to 777 – user2936213 Jan 21 '14 at 11:54
  • Two tips: 1) Stop typing random parameters unless functions no longer trigger warnings—that stuff is [documented](http://php.net/unlink) 2) If a program doesn't have permission to do something, it can't normally grant permissions to itself; that would beat the whole purpose of having permissions. – Álvaro González Jan 21 '14 at 11:55

6 Answers6

4

Just remove the second parameter. and try this

if ( is_file( '../photo_gallery/' . $image_name['imge_name'] ) ) {
   chmod ( '../photo_gallery/' . $image_name['imge_name'] , 777 );
   unlink ( '../photo_gallery/' . $image_name['imge_name'] );
}
zzlalani
  • 22,960
  • 16
  • 44
  • 73
2

in php unlink(); and u must have right to delete that file .

$pathOfFile='/var/www/avc/abc.jpg';
unlink($pathOfFile);
ghost
  • 467
  • 2
  • 6
  • 19
2

try this

filename = '../photo_gallery/'.$image_name['imge_name'];

if(file_exists($filename))
{
  chmod($filename, 777);
  unlink($filename);
  echo "file has deleted";
}
else
{
 echo "file not exists";
}
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
1

Change the right on the folder. The server user running php must have rights on it to delete it.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
1

The process in which you run your application (e.g. from Apache) needs to have write permission to the folder you want to delete from. check permission on "photo_gallery" folder

a.yastreb
  • 1,493
  • 1
  • 10
  • 11
1

You are using this function wrong.

unlink('../photo_gallery/'.$image_name['imge_name']);

Be sure you have permision (chmod) on folder to write.

http://www.php.net/manual/es/function.unlink.php

Christian
  • 484
  • 3
  • 15