What is wrong in this code?

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>");
    ?>

have a look at $_FILES['file']['tmp_name'] and the hint for move_uploaded_file

1 Like

I tried both of them but it is giving me the error (different)

Notice: Undefined index: name in C:\xampp\htdocs\learn_php\upload_img\Code.php on line 5
Could not copy the file!

Even, though I have set the right name in form.

Here is the form code :

<html>
    <body>
        <form action="code.php" enctype="multipart/form-data" method="POST">
            Select image to upload: <br> 
            <input type="file" name="file" id="fileToUpload" type="image"><br>
                <input type="submit" value="Upload Image">    
        </form>
    </body>
</html>

@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.

I’ve changed the code as per suggested by you guys, but it is still showing errors. As replied to @chorn

Does the folder tmp exist in your XAMPP folder?

Yes it exists.

Chances are that tmp folder is already being used to store the upload as well. What if you provide a specific filename for it to be stored as?

 <?php
    if ( $_FILES['file']['name'] != "") {
        move_uploaded_file($_FILES['file']['tmp_name'], 'C:\xampp\tmp\myfile.txt') or 
            die("Could not copy the file!");
    }
    else {
        die("No file specified");
    }
    echo("<li>Sent file:" . $_FILES['file']['name']. "</li>");
    ?>
1 Like

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.

1 Like
        copy($_FILES['file']['name'], 'C:\xampp\tmp') or 

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.

2 Likes

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

Sent file:C:\xampp\tmp\php81DE.tmp
2 Likes

Thanks, Guys It worked!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.