Search code examples
phpxamppinvalid-argument

PHP copy() failed to open stream: Invalid argument


I am trying to upload a file from my brower->PHP script->Server.

My browser and PHP script lie on the same server. I am using xampp on this. The server where I have to upload this file is different.

Here is my code:

$fdir = "http:\\\\myip\\D:\\errors\\";
$ffile = $fdir.basename($_FILES['myfile']['name']);
if (copy($ffile,$_FILES['myfile']['tmp_name'])) {
    echo "<br />"."File uploaded successfully";
} else {
    echo "<br />"."Error in uploading file";
}

I am getting the following error:

copy(http:\myip\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54

Any help would be appreciated.

EDIT:

I edited my code to remove http:// and use the name of the server. Also I am now using move_uploaded_file instead of copy

$fdir="\\\\TESTSRV\\D:\\errors\\";
$ffile=$fdir.basename($_FILES['myfile']['name']);
move_Uploaded_file($_FILES['myfile']['tmp_name'],$ffile);

But Still it gives me the error

move_uploaded_file(\TESTSRV\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54


Solution

  • This is not the way to copy uploaded files - you need to use the function move_uploaded_file:

    if (move_uploaded_file($_FILES['myfile']['tmp_name'], "c:\\path\\to\\file")) {
        echo "<br />"."File uploaded successfully";
    } else {
        echo "<br />"."Error in uploading file";
    }
    

    Also I don't think your $fdir variable is a valid path in Windows. Basically the second argument of move_uploaded_file should be the target path, where you'd like to move the file.