search

Home  >  Q&A  >  body text

How to download images to local in PHP?

The result of this question on Baidu seems to be one and then copied and pasted in various ways
Upload the image to the server

function dlfile($file_url, $save_to)
{
    $in=    fopen($file_url, "rb");
    $out=   fopen($save_to, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}
阿神阿神2751 days ago866

reply all(3)I'll reply

  • 三叔

    三叔2017-06-21 10:12:36

    It can be simpler like this

    $url = "http://xxxxx";
    $save_file = "xxx.xx";
    file_put_contents($save_file, file_get_contents($url));

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-21 10:12:36

    I just happened to write an article: /a/11...

    reply
    0
  • 怪我咯

    怪我咯2017-06-21 10:12:36

    fopen or file_get_contents are simple, but they do not support many features, such as connection timeout and other operations. In actual operation, the script may block for a long time until the PHP timeout setting is reached. If the concurrency is high, it may even bring down the server. Currently, curl is basically used instead

    reply
    0
  • Cancelreply