0

I am using php to get an image from url and copy it to my server but got an error saying there is no such file

Image example:

http://www.google.co.in/intl/en_com/images/srpr/logo1w.png

Here is the solution I am using:

//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);

I am getting the following error. Did i do anything wrong?

fopen(/location/to/save/image.jpg): failed to open stream: No such file or directory
Community
  • 1
  • 1
James Zhao
  • 671
  • 1
  • 8
  • 17

3 Answers3

1

✓ This worked for me.


Try it without the /location/to/save/

The file will be saved in the same folder you run the script in.

Such as:

<?php

//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("image_google.jpg", "w");
fwrite($fp, $content);
fclose($fp);

?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks for your answer @Fred , I tried your solution and it works now. However I still want to know why /location/to/save/ doesn't work – James Zhao Aug 15 '13 at 20:52
  • @JamesZhao You're welcome. It's because `/location/to/save/` stands to be an invalid address, unless you do have the folders `location` and sub-folders called `to` and `save` on your server, but many servers are configured that you need relative paths instead of absolute. If you wish to save to a different folder, you will need to use something to the affect of `$fp = fopen("../folder/image_google.jpg", "w");` – Funk Forty Niner Aug 15 '13 at 20:57
  • @JamesZhao Plus, if you feel that I have answered your question and that it is now resolved, you can click on the checkmark underneath the arrows to the left, and mark it as being "answered". Otherwise, your question will remain in the "unanswered" category, cheers. – Funk Forty Niner Aug 15 '13 at 20:58
  • @JamesZhao Or an `UPVOTE` would be nice. Plus, notice my comment in regards to the answer you accepted: http://stackoverflow.com/questions/18243467/copy-img-from-url-to-server-no-such-file-or-directory#comment26753256_18243505 – Funk Forty Niner Aug 15 '13 at 21:05
  • Hi Fred, I am new here. I only have 7 reputation and can't upvote your answer yet. Thank you very much for your help. Let me know if I can do anything else. – James Zhao Aug 16 '13 at 00:19
0

The folder (/location/to/save in here) should exist. You also need write permissions in it.

Michiel Dral
  • 3,932
  • 1
  • 19
  • 21
0
function download_image($url,$destination_path = '')
{  
    // CHECKS IF CURL DOES EXISTS. SOMETIMES WEB HOSTING DISABLES FILE GET CONTENTS
    if (function_exists('curl_version'))
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

    } else
    {
        $content = file_get_contents($url);
    }
    // CHECKS IF DIRECTORY DOESNT EXISTS AND DESTINATION PATH IS NOT EMPTY
    if(!file_exists($destination_path) && $destination_path != ''){
        mkdir($destination_path, 0755, true);  
    }
    // ATTEMPT TO CREATE THE FILE
    $fp = fopen($destination_path.'/'.date('YmdHis').".jpg", "a+");
    fwrite($fp, $content);
    fclose($fp); 
}

download_image('http://davidwalsh.name/wp-content/themes/jack/images/treehouse-1.png','images');
Roseann Solano
  • 762
  • 2
  • 8
  • 13