PHP Function Rename Permission denied - php

In server, script create new folder, set chmod to 0777, but then it tryes to move files to that folder i get error: Permission denied.
mkdir("../".$new_1, 0777);
chmod("../".$new_1, 0777);
mkdir("../".$new_1."/".$new_2, 0777);
chmod("../".$new_1."/".$new_2, 0777);
rename("files/".$failai[$i].".jpg", "../".$new_1.'/'.$new_2."/".$failai[$i].".jpg");
Warning: rename(files/new_file.jpg,../112a/112b/Tech_diz_1.jpg) [function.rename]: Permission denied in ..code/Jpg&Html.php on line 82
Any solutions?

you'll need to have read and write permissions in the source folder, too.
only having permissions for the target-folder isn't enough as the file is removed from it's source.

Do you have the write access to the file? If not, make sure you chmod the file to 777 or at least to 644.
Also, check the existence of the file by giving a file_exists() on the file name before you rename. :)
Also, after moving file, you might need to set the permissions using chmod() to make it available for renaming. You can do it this way:
<?php
chmod($uploadedFile, 0755);
?>

You should also have permission to change the file "files/".$failai[$i].".jpg". I would guess that that is going wrong

Related

change file permmissons after copy them failed

I tried to to the following:
Copy files and folder from folder .../myfolder/subfolder etc.
to folder .../anotherfolder/subfolder etc.
Copy this works fine.
The Folder has permissions 0755 and, if I copy that, the new folder has the same permissions.
So I want to change the permission to 0644, I tried to do that with
if(!chmod($path, 0644)){ die("Error"); }
it failed with the message
chmod(): Permission denied
my path is relative, the same as I used to copy.
I don't understand why it doesn't work.
Thank for helping and explain

Apache server file permissions

I know this was brought up many times. Still I can not find a sutable answer.
I want to upload a file to a server and want to set temporary 777 permission to the file where I put content. I am the only owner of the file and the directory and it does not seem to be an option to change or add an owner from the control panel and I dont want to set 777 to that folder. Since neither apache nor script own the file chmod does not work. Is there a way to get the ownership to php, a particular script or the server throught chown function? The script that POST a file is not publically accessible btw.
Thanks.
Dunno if I am right or not.
First you can get the owner of the file with this
$filename = 'index.php';
print_r(posix_getpwuid(fileowner($filename)));
with
chmod('/var/www/folder/', 0777)
chmod('/var/www/folder/index.php', 0777)
you can change any folder or file to 0777
you can also check with chmod the file or folder have
if( chmod($path, 0777) ) {
chmod($path, 0755);
}

PHP move_uploaded_file permission denied (permission set to 755)

I'm trying to upload a user's photo using a simple HTML input form, but I'm getting the following error. I've set the permissions of my upload folder to 755. I tried 777 and that works, but I've read that setting it to 777 is not advised and that I should be able to use 755?
Warning: move_uploaded_file(uploads/2014_08_21_11_03_14k.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/yadayada/register.php on line 136
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php8KQwyh' to 'uploads/2014_08_21_11_03_14k.jpg' in /home/yadayada/register.php on line 136
This is my php code:
$userPhotoUrl = 'uploads/'.date('Y_m_d_H_i_s').$_FILES['photo']['name'];
if (is_uploaded_file($_FILES['photo']['tmp_name'])) {
if (!move_uploaded_file($_FILES['photo']['tmp_name'], $userPhotoUrl)) {
// show error message
return;
}
} else {
// show error message
return;
}
First of all, you have to understand what is 755.
For folder, 755 means drwxr-xr-x, which means:
Owner has Read, Write & Execute permission
Group & Public have Read and Execute permission only
As the user running PHP is probably not the owner of the folder, it does not have write permission to the folder. Either:
You chown the folder to PHP's user; or
You make it 777: everybody has Read, Write & Execute permission
Of course, the latter choice has a security issue, as if somebody uploads an executable shell script to your folder, he can execute the script. Therefore, you should stick with the first choice.
You should probably chown the upload folder (move) to the same user as PHP runs under. Try this
chown -R nobody uploaddir
chmod -R 755 uploaddir
Have alook on this Permission
if you set 755, your webserver will be the owner of the folder.

move_uploaded_file() failed to open stream: no such file or directory

I've done your standard checks (is the directory there, are lax enough permissions set), and I'm pretty sure I've covered your standard stupid human tricks. Here's the code that's failing:
move_uploaded_file($_FILES['image1']['tmp_name'], "/public_html/flashsale/assets/img/products/T".$_FILES['image1']['name']);
The directory is there - I copied the path from FileZilla. I even set the permissions to 777, both in FileZilla and in the file manager on the HostGator control panel. This code generates two warnings:
Message:
move_uploaded_file(/public_html/flashsale/assets/img/products/Tsirloin.jpg)
[function.move-uploaded-file]: failed to open stream: No such file or
directory
Message: move_uploaded_file() [function.move-uploaded-file]: Unable to
move '/tmp/phpI5GZ3S' to
'/public_html/flashsale/assets/img/products/Tsirloin.jpg'
In that order. So, the file is being uploaded, the directory exists and is set to 777, what else could I be missing?
you do not need to put the full directory to the file. try to remove /public_html/flashsale/ from your link and see if that will work.
In addition, the file does not need to have 777 permission, I myself upload files to folders with 755 permissions.
also, you can use getcwd(); in the directory your aiming to. the function will give you the directory that you need to use for moving your file. source
The Problem
$dirpath = dirname(getcwd())
This is what I used initially to get the directory path to my /public_html/upload folder. $dirpath will contain
/public_html/upload
The Solution(On server)
$dirpath = realpath(dirname(getcwd()))
Since I’m on a shared hosting environment, the right way of getting move_uploaded_file to work is using this as the destination:
realpath(dirname(getcwd())) returns something like:
/home/cpanelusername/public_html/upload
Make sure the path you are traversing. public_html is not required
$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);
$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{
echo "success";
}
else
{
echo "fail";
}
Hope this helps
For Ubuntu 14.04 with XAMPP, I also have problem with upload but after I have fixed with sudo chmod -R 777 destination, it works well.
Here is what I did:
Temporary folder to upload in my Ubuntu 14.04 XAMPP is /opt/lampp/temp/. If I want my upload files to /opt/lampp/temp/testupload as destination folder, then I need config bellow.
Go to temp folder
cd /opt/lampp/temp/
Create 'testupload' folder under /opt/lampp/temp/
sudo mkdir testupload
Change permission 777
sudo chmod -R 777 /opt/lampp/temp/testupload/
PHP code
move_uploaded_file($_FILES["file"]["tmp_name"], "/opt/lampp/temp/testupload/" . $_FILES["file"]["name"])
Solution for Windows and ISS:
The IUSR account needed permissions in the destination directory. Not the ISS_IUSR account, just the IUSR account.

fopen Creates File, But how to change permissions?

I am creating a new file using fopen.
$filename = 'user_data/10.xml';
$openhandle = fopen($filename, 'w+');
Then I check if the file has been created using: file_exists() function.
The problem is: The file is being created with some owner, probably the folder name, but its not me. Also the permissions of the file is only readable by the owner.
And since I am not the owner, I can't read the file, or change the permissions.
But If attempt to change it using:
chown($filename, 'myusername');
chmod($filename, 777);
I tried changing the file owner and permissions using the Terminal using sudo. That worked properly.
So I also tried using the functions above with shell_exec() so it runs in root.
But had no luck.
Although, I don't have much experience with file permission numbers, the chown command is also not working.
So how should I change the owner and permissions of the file so i'm the owner and its readable and writable by my other PHP scripts?
You should be able to chmod it using only the following line:
chmod($filename, 0777);
Note the 0 before the 777.
Also do not change the ownership before it has been chmod'ed

Categories