【问题标题】:Download images from specific array of urls with php使用 php 从特定的 url 数组下载图像
【发布时间】:2014-01-15 19:34:35
【问题描述】:

我需要从特定的 url 数组中下载一些图片,例如:

<?php
  $images = array('http://url1.com/img1.png','http://url1.com/img2.png'); 
   // download this images from this paths

我在这里看到了一些脚本,但没有准确了解如何将它们链接到这个数组。

预期的输出将是:当我运行脚本以将这些图像从该数组下载到我的服务器的特定文件夹时:home/user/public_html/images。非常感谢任何帮助我的人,我正在尝试但无法建立联系,目前是新手。

【问题讨论】:

  • foreach() 循环,然后一次处理一个代码
  • 我们可以看看你的剧本吗?
  • @Dagon 更具体地说?
  • @MrHunter It;s not duplicate 指的是特定的东西(从 SPECIFIC 数组下载)。在发布问题之前我进行了很多搜索。

标签: php arrays


【解决方案1】:

可能是这样的。

$images = array('http://ecx.images-amazon.com/images/I/214RgVjsvTL.jpg','http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg'); 

foreach($images as $name=>$image) {

    //get image
    $imageData = file_get_contents($image); //$image variable is the url from your array

    $name = explode("/", $image);

    $handle = fopen("images/".$name[5],"x+");     

    fwrite($handle,$imageData);

    fclose($handle);
}

【讨论】:

  • 我收到file_get_contents(http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg ) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
  • testingcom.tk/test.php 这里是测试和内容:codepad.org/e7zp7UdC。希望这不是亚马逊禁止的错误。
  • @user3140607 您在代码中的 URL 末尾有一个换行符。删除这些,然后重试。
  • 我可以看到它会生成一些空文件$handle = fopen("images/image".$count.".png","x+"); 事实上我需要来自指定 url 的图像的确切名称。示例:网址是 amazon.com/imagexyz.png 以使用该名称下载:imagexyz.png,因此不需要 contor。
  • 它完美地下载了它们,除了名称,我怎样才能得到确切的名称,而不是 image1...
猜你喜欢
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 2022-09-30
  • 2020-08-02
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多