用于在Array PHP中下载图像的脚本

时间:2019-03-27 18:29:34

标签: php

我有一个脚本,该脚本从数组中的URL下载图像并将图像复制到服务器上的文件夹中。 / folder /具有写权限(CHMOD 777)。 我收到错误消息(dowwload失败...),此脚本出了什么问题?

<?php
$IMAGES = 'https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/10s%20Or%20Better.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/Alice%20and%20the%20Red%20Queen.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/All%20American.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/Astro%20Roulette.png';

    $images = explode(', ',$IMAGES); 

    foreach($images as $image) {

       $name = basename($image);
       $newfile = 'https://myserver.com/folder/'.$name;

       if(copy($image, $newfile)){
          echo 'Successfully downloaded '. $image;
       }else{
          echo 'Download failed for '. $image;
       }
    }

    ?>

1 个答案:

答案 0 :(得分:0)

有些事情在这里有些不合时宜

  1. 您的网址字符串由一个逗号(,)分隔。

    $IMAGES = 'https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/10s%20Or%20Better.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/Alice%20and%20the%20Red%20Queen.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/All%20American.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/Astro%20Roulette.png';

    但是,您explode()是一个逗号,后跟一个空格,)。

    $images = explode(', ',$IMAGES);
    

    您的数组无法正确生成:

    var_dump($images);
    /* Output:
    array(1) {
      [0]=>
      string(429) "https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/10s%20Or%20Better.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/Alice%20and%20the%20Red%20Queen.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/All%20American.png,https://s3-eu-west-1.amazonaws.com/groove-marketing-assets/games-prod/1X2%20Gaming/Astro%20Roulette.png"
    }
    */
    

    更改您的explode()调用以使用正确的定界符(单个逗号)将项目分开:

    $images = explode(',',$IMAGES); 
    
  2. 您正在尝试复制到HTTP路径:

    $newfile = 'https://myserver.com/folder/'.$name;
    

    From the PHP docts on copy()

      

    如果目标是URL,则如果包装程序不支持覆盖现有文件,则复制操作可能会失败。

    相反,请参考本地系统文件路径。 (正确的路径字符串将取决于服务器的操作系统。)