简体   繁体   中英

move_uploaded_file() not working

I am writing a script in php for the file upload on the server. And the code is as follows:

$target_path = "uploaded_images/";
$target_path = $target_path . basename( $_FILES['image']['name']); 
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {

  echo "The file ".  basename( $_FILES['image']['name']). 
        " has been uploaded";
  } else{
  echo "There was an error uploading the file, please try again!";
}

But the function move_uploaded_file() is not working and it gives the following error:

Warning: move_uploaded_file(uploaded_images/Mordent.jpg) [function.move-uploaded-file]:  failed to open stream: Permission denied 


Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpOWVz7o' to 'uploaded_images/Mordent.jpg'

I have checked all the permissions to the folders and even the safe mode in php.ini is off.

Don't use a relative pathname ( uploaded_images/Mordent.jpg ) here. It's not always obvious what PHP's working directory (which it will use to turn a relative path into an absolute one) is.

If the directory you're trying to move the images to is in the same directory as your script, define $target_path like:

$target_path = __DIR__ . '/uploaded_images/';

The __DIR__ part gives you the absolute path to the directory your current script is in, then you append the "relative" part to that.

Your code is perfectly fine...just change the permissions of the directory where you are moving the uploaded files

Like 0777 for all permissions of read and write you can do it manually or change it via php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM