0

I have multiple jpg image files on our server that I am trying to download to my pc. They are each listed in our database with a path, a title, id, type location found (among other identifiers). I am trying to download them to directories/sub-directories of location and type for organization purposes.

file_get_contents($path) works, but file_put_contents($destination) puts the images in the server directory the webpage is in, not the local pc path that I pass to it.
Can someone tell me what I am doing wrong (I'm sure that list is long!).

Here is my code:
// $Image is the array of images with the associated data

$TempGet = file_get_contents($absolutepath);  //this seems to work
$TempPut  = "C:\Images\\" . $Image["il_name"] . "\'" . $Image["it_name"];
$TempPut .= "'\'" . $Image["title"] . "-" . $Image["pictureid"] . ".jpg'";
$Result = file_put_contents($TempPut, $TempGet);

$absolutepath is "var/www/images/32456.jpg"
$TempPut ends up as "C:\Images\A\'Red Zone'\'3rd one on left-32456.jpg'

Why is it putting the image on the server and not on my pc? Thank you.

EDIT: I don't want the pop-up window asking where to save the file; I wanted to be able to download 1000's of images in an organized manner automatically. Sorry to have left that out. Thanks

SammyG
  • 25
  • 5
  • _I don't want the pop-up window asking where to save the file_ So if this was possible, I could download all existing virus software onto your PC just because you looked at my homepage. That would be fun woudn't it – RiggsFolly Jan 11 '16 at 22:51
  • Unreal. I was so focused on getting these 4000 images off the server as fast as possible, I was completely oblivious to how wrong this "path" was. Thanks for pointing it out. – SammyG Jan 12 '16 at 00:57
  • Also, while searching for examples from the suggestions below, I found this one: zip and stream – SammyG Jan 12 '16 at 01:19

1 Answers1

2

Because file_put_contents() will write data to a file on the server.

If you want to force the browser to download a file, see this question or many more.

By the way, you cannot specify the client file path as the destination, that's up to the browser. All you can do is tell it to download the image/file.

EDIT: as per your edit, you cannot achieve a download from web server to client without the dialog box. This is obviously a security feature implemented into any web browser to prevent websites from filling up your computer with spam, viruses, etc.

It's possible you may be able to achieve it using a Java applet.

The only way you could achieve this with PHP is if your client machine runs a web server too and your server posts a CURL request of some sort to your client's web server.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 2
    And of course the limitation of only being able to download 1 file at once [is trivial to overcome](http://php.net/manual/en/book.zip.php). – Niels Keurentjes Jan 11 '16 at 22:42
  • As I said above, I cannot believe how miopically focused I was to not think about these other avenues - like zipping the files. Duh! Thank you both for your responses and links (I could not find the answer I was looking for and didn't stop to rethink it). Thanks again. – SammyG Jan 12 '16 at 01:00