saket
saket

Reputation: 114

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.

Upvotes: 0

Views: 16100

Answers (2)

John Flatness
John Flatness

Reputation: 33749

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.

Upvotes: 3

Sushil
Sushil

Reputation: 21

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

Upvotes: 1

Related Questions