Save remote img-file to server, with php

狂风中的少年 提交于 2019-12-20 12:43:12

问题


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 is to be saved and renamed to imgfolder/imgID.jpg


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/1371966/save-remote-img-file-to-server-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!