I try to move a temporary file located in the /tmp dir to another dir somewhere else on the server using the rename() function. But I get an error:
Permission denied (Code: 2)
for the temporary file. How can I move a temporary file to another location? If I check that the file exists with file_exists() I get true. And if I copy() the temporary file it works fine.
Here's my code so far:
$toPath = '/var/www/htdocs/myproject/some/file.pdf'
$fileName = 'myfile.pdf';
$filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $fileName;
rename($filePath, $toPath); // Permission denied (Code: 2) here
this is caused by sticky bit set on the directory:
drwxrwxrwt. 8 root root 4096 Feb 6 09:38 .
sticky bit basically stops non-owners of file to rename or delete the file. It is usually set on /tmp and similar directories where multiple users have write permissions and/or save temporary files in, in order to prevent accidental deletion.
For more info, see: https://www.thegeekstuff.com/2013/02/sticky-bit/
Related
I'm frustrated about deleting file in ubuntu using PHP unlink().
I created a very simple simulation as follow:
create a folder named "files" beneath /var/www with 766 permission.
upload a file, let say "image.png" in that folder & set the permission into 666
create a php file named delete.php, set the permission to 644 and upload to /var/www directory
Call the file in browser (I use localhost)
The "image.png" still exists in "files" directory
Here is the php script of delete.php :
$filename = 'image.png';
$file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $filename;
unlink($file);
I also tried the following script :
$filename = 'image.png';
$dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'files';
chdir($dir);
unlink($filename);
But still can't delete the file.
Unlink throws a warning on failure. Check if E_WARNING is visible for you to find out whats going on.
It usually boils down to user rights. Keep in mind if your script is executed by a browser, usually a user named wwwrun or wwwdata (or something similar) is executing it on your server.
Check if this user has permissions to delete, then try again.
The folder/owner of the directory could be a different user than the user being used to run php.
You should create a folder with the user php assigned. If you cannot do that yourself ask your ISP to do it. That is how I solved a similar problem.
One user cannot delete files of another user on a unix system.
If you would set it to 777 then you could delete it...
I have two files:
b.php and test.txt
<?php
$b = "test.txt";
unlink($b);
?>
and the error is: Warning: unlink(test.txt) [function.unlink]: Permission denied
why? b.php and test.txt is 777 and the same group/login
if I set 777 on the parent directory I can execute unlink but i have to set 777 and back to 755?
You (as in the process that runs b.php, either you through CLI or a webserver) need write access to the directory in which the files are located. You are updating the directory content, so access to the file is not enough.
Note that if you use the PHP chmod() function to set the mode of a file or folder to 777 you should use 0777 to make sure the number is correctly interpreted as an octal number.
You'll first require to close the file using fclose($handle); it's not deleting because the file is in use. So first close the file and then try.
in addition to all the answers that other friends have , if somebody who is looking this post is looking for a way to delete a "Folder" not a "file" , should take care that Folders must delete by php rmdir() function and if u want to delete a "Folder" by unlink() , u will encounter with a wrong Warning message that says "permission denied"
however u can make folders & files by mkdir() but the way u delete folders (rmdir()) is different from the way you delete files(unlink())
eventually as a fact:
in many programming languages, any permission related error may not
directly means an actual permission issue
for example, if you want to readSync a file that doesn't exist with node fs module you will encounter a wrong EPERM error
// Path relative to where the php file is or absolute server path
chdir($FilePath); // Comment this out if you are on the same folder
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465
$do = unlink($FileName);
if($do=="1"){
echo "The file was deleted successfully.";
} else { echo "There was an error trying to delete the file."; }
Try this. Hope it helps.
The file permission is okay (0777) but i think your on the shared server, so to delete your file correctly use;
1. create a correct path to your file
// delete from folder
$filename = 'test.txt';
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete.
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root
// your file will be removed from the folder
That small code will do the magic and remove any selected file you want from any folder provided the actual file path is collect.
In Windows and before PHP version 7.3.0, check that your file has been closed before unlinking it,
as said in https://www.php.net/manual/en/function.unlink.php :
On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.
As an exemple :
$fullFilePath = 'C:\Users\MyUserName\www\myApp\public\test.txt';
$handle = fopen($fullFilePath , 'w+');
fopen($filePath, 'w+');
fputs($handle, 'Some text in the file');
fclose($handle);
unlink(realpath($insertedLinesFilePath));
I'm using this PHP code below to create dynamic xml files.
...
$file_name = "";
$rss_feed_dir = $_SERVER['DOCUMENT_ROOT'] . '/xml/';
chmod($rss_feed_dir, 0777);
$file = $rss_feed_dir . $file_name . '.xml';
$file_handle = fopen($file, "w");
fwrite($file_handle, $xml);
fclose($file_handle);
The file will be created if the directory permissions are set 0777, and seems to fail at 0755. I've read on many sites that world execute permissions can pose a security risk.
Should I chmod back to 0755 at the end of this script?
Is there a better way to set the directory permissions?
I suspect it is because whichever directory your rss_feed_dir points to, is a directory owned by another user - other than the user that is being used to run your script.
So 755 permissions would exclude you from write access to the directory if your script is not running with the specified group or user. Which explains why you cannot do a write file command successfully when permissions were 755, but successful when 777.
If this php file is on a server and being called from the web, try modifying your directory ownership itself:
chown -R YOUR_USER:www-data YOUR_RSS_DIRECTORY_HERE
And then chmod to 775
Hope that helps!
I am creating a directory and I am getting the error
Warning: chmod() [function.chmod]: No such file or directory in /home/www/public_html/console/pubs-add.php on line 104
The php file that is running the code is in www/console and the directory that I am trying to create is in www/images/gallery.
I have tried many variations of setting the path such as ../images/gallery or home/www but nothing seesm to work
define("PATH", "/www/images/gallery");
$dir = 'a1234';
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
You mkdir command just uses $dir, which is just 'a1234' (in the current working directory). This will fail (and make the chmod fail too).
The solution: you probably want to prefix $dir with PATH...
Dear chmod() create some time problem. So i will suggest u that use this
mkdir("/path/to/my/dir", 0700);
if u want the created directory should be ready and wirtable then use this
mkdir("/path/to/my/dir", 0777);
The problem is that you cannot chmod a file that you haven't made. For that reason, I've changed the line
$task = chmod($targetfilename, 0777); //make it writable
to
$task = chmod($dir, 0755); //make folder writable
Tip: If you want a folder to be writable, chmod it to 755 and not 777. 777 is for files.
It makes the $dir in your current working directory, however, it doesn't mean that this equals your $targetfilename. I would say that you have to do mkdir($targetfilename) rather than mkdir($dir).
I'm getting the following error when trying to call mkdir() on a server...
Warning: mkdir() [function.mkdir]:
Permission denied in
/home/server/public_html/wp-content/themes/mytheme/catimages/cat-images.php
on line 373
The function is below. Its attempting to create a folder under the site's "wp-content/uploads folder". I've verified that the PHP Version is 5.2.15 and that the files inside the theme folder are writable, but that does not necessarily means the uploads folder is writable I suppose.
How can I find out if the uploads folder is writable?
protected function category_images_base_dir()
{
// Where should the dir be? Get the base WP uploads dir
$wp_upload_dir = wp_upload_dir();
$base_dir = $wp_upload_dir[ 'basedir' ];
// Append our subdir
$dir = $base_dir . '/cat-images';
// Does the dir exist? (If not, then make it)
if ( ! file_exists( $dir ) ) {
mkdir( $dir ); //THIS IS LINE 373
}
// Now return it
return $dir;
}
is_writable() is probably the function you're looking for.
http://cz.php.net/manual/en/function.is-writable.php says:
Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Also, the directly next line is relevant here:
Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').
In other words, check if your directory is writable by the user id of the web server - this may be a different user id than yours! Set the appropriate permissions - e.g. set the usergroup of the folder to that of the server's user, and grant read, write, and execute to group. (chgrp somegroup uploads; chmod g+r uploads; chmod g+w uploads; chmod g+x uploads)
Make sure the parent folder is writable to the process that the web server runs as.
Edit: Oops, premature reply. Does your host give you a GUI file browser thingy?