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.
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.
Try just use website root and do not use $_SERVER['DOCUMENT_ROOT'].
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);
}
}
Related
I make a site and it has this feature to upload a file and that file is uploaded to a server
Im just a newbie to php I download xampp and I run this site that i made in my local machine.
My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
question:
How can I got permission to use unlink();?
I only run this on my localmachine using xampp
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
To do this you must be using superuser account or be the same user that have uploaded the file.
You can go to the directory from your command line and check the permissions that are set to the file.
The easiest solution is to loggin as administrator/root and delete the file.
Here is another work around:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
Read this URL
How to use Unlink() function
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
Try to use this in the path:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';
I have an image sharing website and recently I've noticed I can't delete some of my images via script.
My file is writable and exist (so it's not a permission issue), but why can't I unlink it?
echo $file = base_path('./files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg');
echo is_writable($file) ? ' #is_writable ' : ' !!is_writable ';
echo is_file($file) ? ' #is_file ' : ' !!is_file';
$res = unlink($file);
var_dump($res);
Here is the result:
./home/siteecom/domains/site.com/public_html/files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg
#is_writable #is_file bool(false)
I've also tried relative path ... didn't work
I recently run into an issue like this before.
Firstly you'll need to turn on error reporting as unlink() will tell you exactly what's wrong:
ini_set('display_errors', 1);
error_reporting(E_ALL);
You'll want to make sure the directory that contains the file you want to delete is writable (please supply the chmod permissions for us to help furthur).
You should look into using realpath() to get the absolute path to the file. (I don't think this is the issue as it doesn't throw a file not found error).
Your problem is almost certainly related to incorrect permissions for the directory you're trying to delete in and also the script thats trying to delete said files.
If you could supply those permissions of both with something like:
echo "Directory = ".substr(sprintf('%o', fileperms(DIRECTORY)), -4) . "<br />";
echo "PHP File = ".substr(sprintf('%o', fileperms(SCRIPT)), -4) . "<br />";
We can try and help you further.
You probably missed that in order to delete a file you'll need write permissions to the file and it's ancestor folder. Make sure that the directory ./files/images/2013/11 is writable by PHP.
I am having trouble using fopen() to create a text document for later use as a cookie file.
I have read the documentation for this function, but to no avail.
Notes:
Ubuntu
read / writable ("w+")
I have tried several storage locations including:
/home/jack/Desktop/cookie
/var/www/cookie
/home/jack/Documents/cookie
PHP
echo "debug";
echo "\r\n";
$cookie = fopen("/home/jack/Documents/cookie", "w+");
fclose($cookie);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
Result
file is not created
Output to browser: debug Cookie file does not exist.Cookie file is not writable.
Other Fun Facts
I have tried using fopen(realpath("/home/jack/Documents/cookie"), "w+")
echo "\r\n" gives a space. Why not a newline?
I believe the problem must be something to do with my permissions to create the file, but I have no problem "right-click" creating the text document on the Desktop.
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
echo "debug";
echo "\n";
$jack = "jack";
$cookie = "/home/jack/Documents/cookie";
touch($cookie);
chmod($cookie, 0760);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
fclose($cookie);
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
Instead of fopen()..
touch() to create
chmod() for permissions
I also added user name jack to www-data group.
chmod($path, 0760) group read / write
Reference
chmod() octal values here.
Look at the documentation for file_exists again. It does not take a file handle as an argument, it takes a string filename. The same is true for is_writable. Even if it did, you are opening the file handle and then immediately closing it, so I'm not sure why you're trying to use the file pointer at all after it's been closed.
You may be correct in that you have improper permissions set, but I would start here, first.
Also, if you're only trying to create the file, you may look into using the touch method, instead:
if( touch( $filename ) ) {
// It worked!
} else {
// It didn't work...
}
The web server is not executing as your user. touch /home/jack/Documents/cookie && chmod 777 /home/jack/Documents/cookie to allow the web server user to access the file.
Note this is BAD in production environments.
It looks like a permission issue. What user is PHP running as? It's likely running as www-data or something similar. You should make sure that the folders you are trying to write to are writable by either the user or group that PHP is running as. If you created those folders while logged in a jack, they probably belong to jack:jack and are not accessible by www-data:www-data.
You can also add jack to the www-data group, to make things a bit easier for development.
I'm using below PHP code here to create a particular folder if it didn't exist. I'm using joomla 2.5
$path = my/path/goes/here;
$folder_permissions = "0755";
$folder_permissions = octdec((int)$folder_permissions);
//create folder if not exists
if (!JFolder::exists($path)){
JFolder::create($path, $folder_permissions);
}
But this code throws below error
JFolder::create: Could not create directory
What could be the reason for this?
2 things I think might be causing the problem:
You forgot a ; on the end of octdec((int)$folder_permissions)
Try removing the whole line $folder_permissions = octdec((int)$folder_permissions)
Update:
This is what I used to create a simple folder:
$destination = JPATH_SITE.'/'."modules/mod_login";
$folder_name = "new_folder";
JFolder::create($destination .'/'. $folder_name, 0755);
What could the reason be?
Simple: The user that tries to create that directory doesn't have permission (or your Joomla is broken).
The user that runs the code is probably www-data (on most *nix/Apache). ALso 'nobody' or 'apache' are possible.
If you have rootpermission try this:
1) become the webuser (eg www-data)
2) Jump to the directory my/path/goes/here
3) type: mkdir mynewdir
And you will probably find out that the user doesn't have sufficient permissions to do so.
edit your configuration.php file under Joomla home direcory, change:
From:
var $tmp_path = '/home/public_html/your_name/tmp';
To:
var $tmp_path = 'tmp';
and tmp should have 777 permission
I make a site and it has this feature to upload a file and that file is uploaded to a server
Im just a newbie to php I download xampp and I run this site that i made in my local machine.
My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
question:
How can I got permission to use unlink();?
I only run this on my localmachine using xampp
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
To do this you must be using superuser account or be the same user that have uploaded the file.
You can go to the directory from your command line and check the permissions that are set to the file.
The easiest solution is to loggin as administrator/root and delete the file.
Here is another work around:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
Read this URL
How to use Unlink() function
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
Try to use this in the path:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';