I’m trying to create upload script, but I’m getting this error :
Warning: copy(Capture.JPG): failed to open stream: No such file or directory in C:\xampp\htdocs\learn_php\upload_img\Code.php on line 3
Could not copy the file!
<?php
if ( $_FILES['file']['name'] != "") {
copy($_FILES['file']['name'], 'C:\xampp\tmp') or
die("Could not copy the file!");
}
else {
die("No file specified");
}
echo("<li>Sent file:" . $_FILES['file']['name']. "</li>");
?>
@chorn is correct, you need to reference $_FILES['file']['tmp_name'], not $_FILES['file']['name']. And you really should use move_uploaded_file instead of copy.
Failed to open stream usually means that you don’t have permissions, but I highly doubt that is the problem as Windows normally doesn’t have that kind of permission hierarchy like Linux does. Everything in Windows is normally executable by anyone if you are an admin.
But with the error message you have posted, it is saying that the file or folder doesn’t exist. Let me go test your code out.
@cpradio could be right. tmp folder could be in use by another process.
Even with copy, this part is wrong as you’re not specifying the filename that you want to copy the uploaded file to, just the folder name. Note on the PHP doc for copy suggests you need to specify the complete destination filename.
It has to do with the second param when using move_uploaded_file. The second param should be for specifying the name. I just checked my source and that’s how I had mine. So I removed C:\xampp\tmp\ and got