Save remote img-file to server, with php

后端 未结 2 677
礼貌的吻别
礼貌的吻别 2021-02-05 17:23

I want to save a remote img-file to my server, but I don\'t know how to do.

The image url is http://img.youtube.com/vi/Rz8KW4Tveps/1.jpg and 1.jpg

相关标签:
2条回答
  • 2021-02-05 17:37

    You can use file_get_contents() to load the remote image to a binary string inside your PHP script (file access in PHP often accepts URLs to access remote resources - this is very handy), then store that file somewhere where you have write access. Here is a very simple example:

    $image = file_get_contents("http://img.youtube.com/vi/Rz8KW4Tveps/1.jpg");
    file_put_contents("imgfolder/imgID.jpg", $image);
    

    Tada!

    0 讨论(0)
  • 2021-02-05 17:42

    If the URL stream wrappers are allowed, you can do it in 1 line rather than having to load it into a var:

    copy('http://img.youtube.com/vi/Rz8KW4Tveps/1.jpg', 'imgfolder/imgID.jpg');
    

    This is much less likely to cause a problem with PHP running out of memory.

    0 讨论(0)
提交回复
热议问题