Can't copy file with php, permission denied - php

I'm trying to copy an uploaded file through php to a folder in the home directory. I gave every permission to the Apache user (www-data) but when I'm trying to copy it I get a warning "failed to open stream: Permission denied".
copy("$target_file", "/home/pap-x/meshes/Part_A.dae");
What's wrong?

In addition to www-data having permissions to the directory of $target, it must have at least execute permissions to every directory above the target. If any of those directories deny access then www-data will not be able to find the target directory.

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

PHP-Apache - Folder permissions for user uploads

I have a folder /var/www/html/images/ppic/50x or 100x (dependin on size) where user avatars are kept. When a user uploads a new avatar, it gets resized and moved to each different size folder.
I m getting the following permission errors:
Warning: move_uploaded_file(images/ppic/144231007.jpg): failed to open stream: Permission denied in /var/www/html/settings.php on line 154
Warning: move_uploaded_file(): Unable to move '/tmp/phpi3oiJp' to 'images/ppic/144231007' in /var/www/html/settings.php on line 154
not moved
How do I set permissions to these folder in a way that will allow users to upload but not delete or mess with?
try with this
sudo chmod 755 -R /directory_name
which user owns the directory?
try chown www-data:www-data /directory_name
Also placing user uploading files in a web accessible directory isn't great practice.
Placing them somewhere outside of the web root and using a script to display them in the browser is safer.

Wordpress class-wp-image-editor.php permission denied

I just changed the directory of my WordPress instance. I copied all files via FTP into the new folder and also copied the database and edited the site URL and home entries.
Now the page don't show the images, but gives me this message:
Warning: imagejpeg() [function.imagejpeg]: Unable to open
'/www/htdocs/serverID/wp-content/uploads/bfi_thumb/modell3-2xr5ffo5juzmz3rgn84d8g.jpg'
for writing: Permission denied in
/www/htdocs/w00db707/wp-includes/class-wp-image-editor.php on line 405
Everything else is looking good. Do you know whats wrong?
Two possibilities:
The file/folder permissions for /wp-content/uploads/ need to be CHMOD to 0755 or 0777
If the permissions are already set to 0755 or 0777, the owning user/group needs to match your HTTPD user (varies based on server configuration; sometimes the user is www-data, sometimes apache, if a Plesk environment it is the username you setup the subscription with)

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.

permission for my php to create a dynamic file on the server

For a project I need my php file to create a dynamic file (on remote server which I bought) which loads into a flash component. Everything is working fine on my localhost. But once I upload it to the server, it throws the following errors:
Warning: fopen(output.html) [function.fopen]: failed to open stream: Permission denied in C:\Inetpub\vhosts\x.co.in\httpdocs\blabla.php on line 25
Warning: DOMDocument::save(k_id.xml) [domdocument.save]: failed to open stream: Permission denied in C:\Inetpub\vhosts\x.co.in\httpdocs\blabla.php on line 136
where on those lines fopen is written.
I understood as there is no permission to my php file to create any dynamic files on the server. So i just wanna understand is there a way by which I can privilege my php to create that file on the server.
I've the login access, which I think I've to put somewhere in code before it tries to create such file...but i dont know how to figure this...any suggestions?
The user that runs the PHP process needs to have permissions to write new files in the target folder. On linux servers this is done using CHMOD.
chmod 777 -R /path/to/folder
777 is the permission (full permission for testing only), -R means recursive for the files/folders inside. As you are using windows just right click on the folder and look at properties and search for permissions.
Create a new directory where the PHP will write the files. For that directory set permissions to 705 (that is owner has Read, Write, Execute permission)
You can either do that using a FTP client (Filezilla) or via a Bash shell
mkdir inputfiles
chmod 705 inputfiles

Categories