简体   繁体   English

从网络下载图像

[英]Download images from web

First of all I have tried combination of file_put_contens with fopen, fopen with frwrite without any sucess. 首先,我尝试了将file_put_contens与fopen结合使用,将fopen与frwrite结合使用而没有任何成功。 I have now found this where Im a step further, but not at the finish line. 我现在发现这是Im进一步发展的目标,但还没有达到终点。 Saving image from PHP URL 从PHP URL保存图像

I have a url - domain.tld/images/ and the structure is 1.jpg, 2.jpg and so on. 我有一个URL-domain.tld / images /,结构是1.jpg,2.jpg等。 With this I can get the last pictures saved 15.jpg, but I dont seem to the the other pictures. 这样我可以获得保存为15.jpg的最后一张图片,但是其他图片似乎没有。 Not sure if I should use curl_multi, or putting the curl in a loop or whats the best way to do this? 不知道我应该使用curl_multi还是将curl放入循环中或执行此操作的最佳方法是什么?

<?php
$base_url = "domain.tld/images/";
$sufix = ".jpg";
$id_start= "1";
$id_end = "15";
$path_to_save = "files/";

foreach(range($id_start,$id_end) as $id)

$ch = curl_init();
$fp = fopen('filer/'.$id.$sufix, 'wb');
curl_setopt($ch, CURLOPT_URL, $base_url.$id.$sufix);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>

You didn't state what the problem was with file_get_contents() , so if your host is configured to allow it ( allow_url_fopen = on in php.ini), this should work: 您没有说明file_get_contents()的问题所在,因此,如果将主机配置为允许它(php.ini中的allow_url_fopen = on ),则该方法可以正常工作:

$base_url = "http://domain.tld/images/";

foreach(range($id_start, $id_end) as $id) {
    file_put_contents($path_to_save.$id.$sufix, file_get_contents($base_url.$id.$sufix));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM