Question

PHP Upload Script not Working

Hi,

I have installed PHP8.2 and Apache and uploaded a simple PHP script to upload images. It’s a very simple php script to upload files.

The issue is that I’m getting this message “Failed to open stream: No such file or directory in”. Which means that the temp file was not uploaded to /tmp folder.

The print_r($_FILES) displays this:

Array ( [name] => 14955828_1517805064899902_6497754076576078705_n.jpg [full_path] => 14955828_1517805064899902_6497754076576078705_n.jpg [type] => image/jpeg [tmp_name] => /tmp/phpxuSNXU [error] => 0 [size] => 72072 )

The error result is 0, which means that the file was uploaded. However, when I check the /tmp folder, the uploaded file is not there.

It looks like the file is not even uploaded to the /tmp file.

This is a new fresh installation.

Does anybody experience this before?

Thanks for your help.

FranZ


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 18, 2023

Hi FranZ,

It sounds like that the upload path is not correctly defined in your script. A simple way to keep track of the path is just to define the absolute path in your PHP app. If this is not the case, would it be possible for you to share your upload script here so I could further review it?

Besides that, there are a few general things that I could suggest checking:

  1. The most common issue with file uploads in PHP is related to permissions. Ensure that the /tmp directory (or whichever directory is specified in your php.ini for upload_tmp_dir) has the correct permissions and ownership. The directory should be writable by the Apache user (usually www-data on Ubuntu). You can adjust permissions using chmod and ownership using chown.

  2. Double-check your php.ini file. Sometimes, the upload_tmp_dir setting is altered, and PHP tries to use a different directory for temporary files. If this directory doesn’t exist or isn’t writable, it could lead to the error you’re seeing. Also, verify the post_max_size and upload_max_filesize settings to ensure they’re large enough to handle the files you’re trying to upload.

  3. Make sure your PHP script uses the move_uploaded_file() function to move the uploaded file from its temporary location to the desired directory. This function not only moves the file but also provides a security check to ensure the file was uploaded via a PHP script. If you’re doing any additional processing or renaming of the file, ensure that those steps are correctly implemented.

Good luck, and I hope this helps!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
November 14, 2023

Heya @e13a2e98219c4801b536a0e92597b9,

I would assume it’s a permissions and ownership issue. I don’t think Apache has the permissions to write to the /tmp file. Try to create a folder where your script is, set it with the Apache ownership and proper permissions and see if that would help.

More often than not when uploads don’t work it is because of permissions and ownership issues. The thing is they do not get logged in your PHP logs.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel