move_uploaded_file中的错误

I'm trying upload an image using:

$sourcePath = $_FILES['file']['tmp_name'];
$targetPath = "upload/".$_FILES['file']['name'];

move_uploaded_file($sourcePath,$targetPath) ;

but there is always an error like this:

Warning:move_uploaded_file(upload/13709_965665460118582_8676485775333023105_n.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\AjaX\ajax_php_file.php on line 23

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php3C97.tmp' to 'upload/13709_965665460118582_8676485775333023105_n.jpg' in C:\xampp\htdocs\AjaX\ajax_php_file.php on line 23

What am I gonna do with this thing?

Make sure that

  • the file was uploaded without an error (php manual)
  • the destination directory exists and is writable

The anwser is right in front of you:

Warning:move_uploaded_file(upload/13709_965665460118582_8676485775333023105_n.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\AjaX\ajax_php_file.php on line

The warning clearly states "No such file or directory", this is your issue,

the directory <script location>/upload does not exist, what you may want to do is use $_SERVER['DOCUMENT_ROOT'] to get an absolute file path for your destination location.

Typically the DOCUMENT_ROOT value is the root of your domain as a filepath string, such that $_SERVER['DOCUMENT_ROOT'] is the same location in the file structure as http://www.yourwebdomain.com/<here>.