-2

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

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76

1 Answers1

0

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.

laurent
  • 88,262
  • 77
  • 290
  • 428
  • I tried with `move_uploaded_file` But I am getting this error `move_uploaded_file(http:\\myip\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54` AND `move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1C50.tmp' to 'http:\\myipd\D:\errors\IMG-20150424-WA0004.jpg' in C:\xampp\htdocs\BS\myphp1.php on line 54` – Babaji Shivram Sep 01 '15 at 08:40
  • The path should be a path on your computer, without "http:\\myipd" in front of it. So if you want to put the files in D:\errors, just call `move_uploaded_file($_FILES['myfile']['tmp_name'], "D:\errors\" . $_FILES['myfile']['name'])` – laurent Sep 01 '15 at 08:46
  • Please read the question carefully.I do not want to save the file on my computer, I want to save it on another server. – Babaji Shivram Sep 01 '15 at 08:51
  • Ok in this case you still need to save it to your computer. Then from there you can send it to another computer. How you send it depends on how your computers are connected. You might want to provide more details about this. – laurent Sep 01 '15 at 08:52
  • I am successfully saving the file on my computer. The two computers are connected through the intranet in our organisation. – Babaji Shivram Sep 01 '15 at 09:00
  • The http wrapper will simply not allow you to do that; it has no write support. What kind of "interfaces" does the other server (the one that is supposed to receive, accept and store the files) provide? FTP, SCP, WebDAV, ... ? – VolkerK Sep 01 '15 at 10:06