PHP-uploaded file has incorrect permissions - php

I'm using PHP to allow a user to upload a file. The file is being uploaded, but the permissions on the file are incorrect. I set up the permissions on the folder and checked the box that says apply to all children in the folder.
This works only after I make one change to the newly uploaded file. If I don't make a change to the permissions, it gets this default set that breaks the rest of my application.
For example, C:\uploads\ is set to allow users in the group "Everyone" to read and write. This however doesn't apply to the newly uploaded file until I do something to that specific file's permissions (add or remove any user or group to its permissions - this change gets overwritten by the folder's permissions).
This is on IIS6 (I believe) on Windows Server 2003

Windows upload file to temp directory and then move it with temp directory permissions. If You give good permissions to the user on this folder (upload_tmp_dir) then will be no problem.
You can also use windows system command:
CACLS or ICACLS

Do:
chmod()
On Your file after upload

Related

Cpanel Permission when uploading

When I upload files through my cpanels file manager all my php file permissions are being changed to 0666 from what I read this is not secure + it is causing my WordPress to not function correctly. Normally web hosts have the permissions set to 0644 when uploading files. It would takes ages for me to change all files manually from 0666 to 0644. How can I change the permissions when I upload in cpanel? Should I try ftp and see if that makes a difference?
EDIT
When I upload the zip file it uploads as 0644 than when I extract the files inside are all turned into 0666.
Thanks
There is no need for you to re-upload all files to simply change permission for them.
Use your FTP client to change file permissions for files, directories and sub-directories.
You can do this via command-line SSH (if you have access).

Unable to get file upload permissions for PHP file manager

My site is on a shared hosting. I've been using FTP and PHP File Manager to upload and delete files. Recently I've found a file I couldn't delete due to permissions, neither in PHPFM nor in FTP. So I've used DirectAdmin (the only option for my plan) to reset all permissions. Then I set all permissions for all files in public_html and subfolders to 777 recursively. I know only that it allows me to do more than any other permission variant.
Now the site is running in a static way, I can open PHP File Manager and it has no permission to upload files. I can upload files in DirectAdmin, however, but that feels unsafe. PHP File Manager reads: 'I/O error'. The directories look like this:
What do 1422 and 1420 mean? What can I do to upload files again? Thank you for the help.
1422 is the user_id the file belongs to and 1420 the group_id it belongs to.
Linux has a permission system, where you can give special permissions to the owner, your group and everyone else.
Permission 777 means everybody can read/write/execute, your group (1420) can read/write/execute and you (user 1422) can read/write/execute the file.
Permissions:
1 is execute file or open directory
2 is write
4 is read
Read/write permission is 2+4=6, read/execute (or open a directory) is 1+4=5
The three numbers represents [owner][group][everybody], so setting a file to 644 means user can read/write and everybody else just read a file.
Edit: The safe thing is to set all files to 644 and directories to 755. Private files should be 600 and executable files 755 (PHP files are NOT executable).
Apache is run as user apache or httpd, which is another user, therefore you must give "everybody" permission to read your PHP files and directories.
Edit2: If you need PHP to upload files, it is really done as user apache/httpd. Therefore you need to give full privileges to "everybody" to open directories and read/write 777. The file permissions should be 666.

Read permission issue with moved uploaded files on IIS

I have a PHP 5.4 served website on a Plesk-based IIS 7.5 server. The app pool identity runs as IWAM_plesk but PHP runs as IUSR_username.
When I upload files, PHP can move the file and create a thumbnail from it, but the file doesn't gain the anonymous IWAM_plesk(default) permissions for browsers to read the file and thus the image (not the generated thumbnail) "cannot be read" when viewing the site.
How can I grant the correct permissions to the file after uploading?
Only a folder "replace permissions on all child objects" fixes the issue (or adding the user permissions directly to the file).
This is a problem of the upload_tmp_dir setting in php.ini. When moving a file during the upload process, the permission don't move with it. Create a project specific upload folder, set the users/groups as you need it and set this folder as upload_tmp_dir, either in php.ini or project specific.
The uploaded file will inherit the permissions of the folder.
Had this problem with the webplatform installer, which sets the upload_tmp_dir to C:\Windows\temp by default.

Setting File Permissions in Windows with PHP

I have a script that uploads a *.csv to import into a DB table that I have which works great in linux through chmod($target, 0777); but I can't for the life of me find the solution to do exactly this but on a Windows based Apache server.
Some other posts have people responding "don't put in the 0777 and it should work" but that's not the case for me. Thanks!
Thanks to the comment left on my original post I was able to figure it out with a little more help from https://web.archive.org/web/20171121192635/http://www.howyoudo.info/index.php/how-to-fix-windows-server-upload-file-inherit-permissions-error/
The problem only happens when you use PHP to upload a file. When you upload a file, PHP sends the file to a temporary directory on the
hard drive (for me it is C:\Windows\Temp) and then copies it over to
it’s intended directory. Once the file has landed in the temporary
directory, it is assigned the permissions of that directory. The
problem is when Windows copies that file, it keeps the temporary
directory’s permissions and doesn’t inherit your web directory’s
permissions.
The easiest way to fix this problem is to add to the temporary directory your intended web directory’s permissions. There’s no need
to erase the permissions already in the temporary directory, just add
the web directory’s permissions to them. In other words, follow these
steps
To change the permissions of your temporary upload directory, find
the “upload_tmp_dir” in your php.ini file.
Set it to the directory
of your choosing (outside your web folders of course) or leave it at
default (for me it is C:\Windows\Temp).
Browse to this folder and add the permissions of your web folders to it.
While Brian Leishman's answer will work, if you do not have the ability to edit permissions on the temp folder, you can make your uploaded file inherit permissions from its new location manually with the following on the command line:
icacls "target.txt" /q /c /reset
So, using PHP's exec() function:
exec( 'icacls "target.txt" /q /c /reset' );
For details of the various icacls flags, see: https://technet.microsoft.com/en-us/library/cc753525.aspx
Tip: Using the /t flag, you can use pattern matching to process multiple files.

Why can't I download files uploaded with PHP?

I have created a custom CMS with PHP and it uploads files to a directory with 777 permissions on it. When the files are uploaded they are given 600 with apache being the owner, therefore I can't download them through FTP as the main FTP user.
Anybody have any ideas? I have tried changing the permission but don't have rights due to the owner being apache.
You cant chmod the files, but 'apache' can: after uploading an moving the file,change the permissions in the same script. As it is run by apache, it is allowed to do so.
http://php.net/manual/en/function.chmod.php
For uploading files it is important to note that uploading files without explicitly setting permissions after is a bad practice.
I would take the link Nanne provided and begin to add that to your upload script. This will ensure that the files are given the appropriate permissions for each situation you will need them.

Categories