Reputation: 1866
I can't figure out why file_exists() don't find my file though the path seem right.
Here is my script :
if($_POST['delete-avatar'] == 'on') {
$q = execute_query("SELECT avatar FROM gj_customers WHERE id_customer = '$_GET[id]'");
$customer = $q->fetch_assoc();
$img_path = $_SERVER['DOCUMENT_ROOT'] . WEBSITE_ROOT . $customer['avatar'];
var_dump($img_path);
if(!empty($customer['avatar']) && file_exists($img_path)){
unlink($img_path);
}
}
I'm on MAMP, my website is in htdocs/gamejutsu/www/ , WEBSITE_ROOT contains '/gamejutsu/www/' and avatars are in img/avatars/ .
var_dump on $img_path returns me : /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/Sonik_avatar.jpg.
If I go on localhost:8888/gamejutsu/www/img/avatars/Sonik_avatar.jpg the image is displayed.
I never enter in the if bloc. If I remove the file_exists test I have a warning : unlink(/Applications/MAMP/htdocs/gamejutsu/www/img/avatars/Sonik_avatar.jpg): No such file or directory in /Applications/MAMP/htdocs/gamejutsu/www/admin/members.php on line 67
I'm pretty sure i'm missing something but everything seems ok for me and I use the same method somewhere else on my website without any problem.
Thank you for your help.
Upvotes: 0
Views: 888
Reputation: 208
It is complicated debug your application without access but you can try what return realpath();
var_dump(realpath($img_path))
If returns null file doesn't exists or your application haven't acces to it.
Try open your console and this command:
cd /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/
If you have error you have bad path and must manualy check it (easiest will be by console) if you can cd in this directory. Please add here your ls of this directory for check permissions:
ls -la /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/
And if everything look good try change your console user to webserver user (usually www-data) and try acces to file
sudo su
su www-data
cat /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/Sonik_avatar.jpg
If you can acces to this file (it will be binary data) permissions are ok, in other case check if you have right permission to file and if you good owner and group of file.
Upvotes: 1
Reputation: 3
Another way is by identifying the path.
if($_POST['delete-avatar'] == 'on') {
$q = execute_query("SELECT avatar FROM gj_customers WHERE id_customer = '$_GET[id]'");
$customer = $q->fetch_assoc();
$img_path =' filename' . WEBSITE_ROOT . $customer['avatar'];
var_dump($img_path);
if(!empty($customer['avatar']) && file_exists($img_path)) {
unlink($img_path);
}
}
Upvotes: 0