PHP - Issue with move_uploaded_file - php

I've used move_uploaded_file successfully in the past, however I'm having an issue with permissions I believe with a new server I'm testing this on. I've created a folder named "uploads" for the uploads to be moved to via FTP. Here's a screenshot showing the permissions:
Everyone has read/write access, but when I upload a file I'm getting an error on the line that moves the file to the "uploads" directory:
if (move_uploaded_file($_FILES['photo1']['tmp_name'], $upload_directory.$uploadedFile)) {
The file is not moved to the "uploads" folder and looking at the server log I can see entries like this:
php[1344]
PHP Warning: move_uploaded_file(uploads/2.jpg): failed to open stream: Permission denied in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\test_upload\index.php on line 206
PHP Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php2D93.tmp' to 'uploads/2.jpg' in C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\ test_upload\index.php on line 206
I can't see how it can be a permissions issue if all users have read/write/execute on the "uploads" folder? Here are the variables:
$upload_directory='uploads/';
$uploadedFile= $_FILES['photo1']['name'];

It appears that uploads/2.jpg is $_FILES['photo1']['tmp_name'] and should therefore have been uploaded to the server's /tmp/ directory. On most systems, because /tmp is a system dir, it has restricted permissions on it.
See if you can alter the perms on /tmp or find out exactly which dir the tmp uploaded file is being moved to first, before being "uploaded" (and then alter the perms on that dir).
Regardless, the path to the file as passed to move_uploaded_file() doesn't look right. It needs to be a full-path. Possibly the server-error you're seeing is not happening at precisely the same time as the upload operation takes place.

I wasn't able to fix this via FTP settings. I had to adjust the settings by logging in to the Windows Server and changing the security settings on the folder I was trying to move files into so that the "Users" had read/write access.

Related

Running a PHP program in Ubuntu - fopen("filename", "w"): Failed to open stream: Permission denied

I am using Ubuntu as my OS, and I have XAMPP in it. In the htdocs folder, I have created a project folder in which I have index.php file. Now inside the index.php, a part of the code does this:
fopen("colors_new.csv", "w");
From the manual,
'w' Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
But instead of the file being created, I am getting
Warning: fopen(colors_new.csv): failed to open stream: Permission denied...
And when I look into the project folder, the colors_new.csv file is NOT created.
When I run this program in another computer running Windows OS, it runs successfully. The file is successfully created.
How do I fix this problem in Ubuntu?
WHAT I TRIED:
I tried to run sudo chmod 777 colors_new.csv but I get chmod: can not access colors_new.csv. No such file or directory.. I tried this with just the file name (as shown) while the terminal WAS in the project directory; as well as by giving the full absolute path of the file. Got the same error both times.
NOTE: I have seen other questions, and as indicated above, I tried to give permissions to the file. The other questions did not solve the problem, the accepted answer below did solve it.
This means that you don't have the rights to write a file in the directory where your index.php is. A couple of things you can do to make it work
give filewriting permissions to the directory.
Go with 755 on that and 644 on files, if settings are correct this is sufficient. Never go full 777, which is security risk.
create a subdirectory inside your directory and give that sufficient rights (755) to do what you want.
F.i. Fopen('./files/colors_new.csv)
Provide the write permission on the folder in which you want to write the file. Your problem will be solved.

Laravel 5.1, failed to open stream: Permission denied

I am having a challenge uploading a file into app/storage. Upon uploading the file I get this error:
file_put_contents(C:\xampp\htdocs\ProjectName\storage\app\folderName): failed to open stream: Permission denied.
I tried using chmod -R 777 storage/app, but still have the same problem.
Would really like know how to solve this problem.
Make sure apache has write access to PHP's temp folder (C:\xampp\tmp\ usually) as well as all of storage (not just storage/app). Also, be sure the uploads folder actually exists (C:\xampp\htdocs\ProjectName\storage\app\folderName).
PHP first uploads files to the tmp folder, and then moves them into storage.
Here's how to change permissions in Windows 7.

Error Uploading images in drupal

I was trying to upload a custom logo to my drupal site which runs on IIS 8 with PHP.
I got the following upload error :
" Warning: move_uploaded_file(temporary://logo.png): failed to open stream: "DrupalTemporaryStreamWrapper::stream_open" call failed in drupal_move_uploaded_file() (line 1642 of C:\inetpub\wwwroot\drupalfull\includes\file.inc).
Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php25C3.tmp' to 'temporary://logo.png' in drupal_move_uploaded_file() (line 1642 of C:\inetpub\wwwroot\drupalfull\includes\file.inc).
File upload error. Could not move uploaded file."
Please help me. I have set both read and write permissions on IIS but still it does not work.
Create sites/default/files/tmp folder and give full permissions (777) and change 'Temporary directory' at admin/config/media/file-system.
I think it fixes yout problem.
Regards.
If you are running ISS8 (web server) then the file path is different then the default for Unix.
Make sure you have the temporary folder setup correctly on your settings in Drupal and the folder has write permissions.
You can follow this article at step 5 and 6.
Create the folder:
Drupal uses the Sites\Default\Files directory to store temporary files and therefore must be able to write and modify files in this folder.
Create the folder:
C:\inetpub\wwwroot\Drupal>md sites\default\files
http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/install-drupal-on-iis
Your problem is most likely the permission problem inside drupal. Since you are using windows system, you need to make sure the folder sites/default/files have writable permission for the IIS server. Config the permission to be writable for all user and see if it is working and then limit the permission step by step until you feel comfortable.
I'm going to put another answer in here because I guarantee I'm going to forget how I fixed it!
Basically, on Windows, the default upload folder for PHP is C:\Windows\Temp. As part of the Drupal upload, it moves files from the PHP upload folder to the Drupal Temporary//: folder. There is an issue however in PHP whereby the PHP executable needs read permission to the full path to the uploaded files, so C:\Windows\Temp. If this is not given, it will error out and the file will not be moved.
Details are at http://php.net/manual/en/function.realpath.php which is a function call made deep in Drupal's upload file code.
My fix was to create a folder elsewhere, say C:\PHPTemp, and modify PHP's settings (session.save_path and upload_tmp_dir) to point to this folder instead. After giving IIS_IUSRS full control (overkill? probably!), I was then able to upload images!

PHP File Uploading Issues

I'm trying to implement script that will allow a file to be uploaded, and then moved to a designated directory. This is running on a Windows server & IIS. I'm having 2 issues in doing so.
First, I get an error when trying to move the file.
Warning: move_uploaded_file(reports/ff.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\inetpub\wwwroot\betterinsight\betterinsight\upload_file.php on line 29
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\Temp\php1EAB.tmp' to 'reports/ff.jpg' in C:\inetpub\wwwroot\betterinsight\betterinsight\upload_file.php on line 29
Stored in: reports/ff.jpg
When searching, almost everything says it's a permission problem. I have added full rights (will pare down later) to the user: IIS_IUSRS. Even did it to the parent directory as one site recommended.
Second: As a possible solution to the above issue, I tried changing the directory in which files are saved. But the files are still uploaded into C:\windows\temp.
I've run phpinfo, and it says that uploads should be in (as defined by upload_tmp_dir):
Again, this is on a Windows Server 2008. Thanks.
Permission denied
You need to set the folder permissions to 777 or enable read write to the folder. I think the permissions are messing...
OK, the answer was simple. I was adding permissions for user: IIS_USRS, when it should have been just user: USRS.

How to write a text file

test.php opens test.txt and writes to it. It works fine on one hosting server.
After migrating the code to another host, test.php now can't write out test.txt. The error shows:
Warning: fopen(/home/username/public_html/test.txt)
[function.fopen]: failed to open stream: Permission denied
It will only work when the test.txt file is set to 777. On the original host, test.txt was set to 755.
What am I missing to give permission for test.php to write to the file without 777 on this new host?
Also, file upload PHP scripts aren't working either.
It is not just the permission that is important, but also who owns the files. 755 is enough if the folder/file is owned by the same user that is used for running the web server.
Writing directly to the public_html directory is a huge security risk. I suggest creating a subdirectory (probably best outside the public_html) folder and give it the appropriate permissions / ownership.
It is safer to have the folder owned by the user that runs apache (or whichever web server is being used) - usually something like apache or www-data instead of giving world write permissions.

Categories