When I try to upload an image to my website I get this error message. If anybody can help please put replies in easy to follow, step by step 1,2,3 answers as I am Computer illiterate and don't want to damage any other part of the script. Thanks in advance!!

Warning: move_uploaded_file(adverts/286ec6c9631b8d962ffaaadebd05bac3jpg) []: failed to open stream: Permission denied in /home/artscraf/public_html/upload.php on line 29

Warning: move_uploaded_file() []: Unable to move '/tmp/phpVp1Wy6' to 'adverts/286ec6c9631b8d962ffaaadebd05bac3jpg' in /home/artscraf/public_html/upload.php on line 29

Dani AI

Generated

Good call by — this was almost certainly a permissions problem, and confirmed changing the upload folder to be writable fixed the symptom. That tells two important things: the webserver process could not write the temporary file into the destination directory, and once write access was granted the move_uploaded_file call succeeded.

Two quick checks that often get missed:

  • Verify the upload directory actually exists and the script is building the destination path correctly (look for a missing dot or slash in the generated filename).
  • From PHP, test whether the directory is writable before trying to move the file. For example:
if (!is_writable($uploadDir)) {
    error_log('Upload dir not writable: ' . $uploadDir);
    // handle error, do not attempt move_uploaded_file
}

For a safer long-term fix prefer changing ownership so the webserver user owns the uploads folder, and give write permission to the owner only instead of making the folder world-writable. On shared hosting, if you cannot change ownership, use the host control panel or contact support rather than leaving the folder writable by everyone. Also harden uploads by validating file type/contents, restricting executable permissions in that directory, and serving uploads from a location that cannot run server-side scripts (see OWASP on unrestricted file upload).

For reference and troubleshooting, the PHP manual explains how move_uploaded_file behaves and common failure reasons: move_uploaded_file documentation. Follow the checks above first (existence, correct path, writable directory); if those pass and it still fails, capture server error logs and confirm which user the webserver runs as.

Recommended Answers

All 3 Replies

Looks like you might have the CHMOD the adverts file to allow write permissions from a local script.

Thanks for your help. You are the first person who has come up with that suggestion - from 3 forums. I have changed the CHMOD code on the adverts file from 755 to 777 and it all appears to upload properly now. Is this code okay or should I use a different one?
If it goes wrong again I'll definitely be back to this forum first.

yup 777 should be fine if your just keeping pictures in that folder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.