doujuanqi2909 2014-04-22 10:32
浏览 29
已采纳

如何直接从一个网站下载到另一个网站

I have a piece of a PHP code:

<?php 
$image_cdn = "http://ddragon.leagueoflegends.com/cdn/4.2.6/img/champion/"; 
$championsJson = "http://ddragon.leagueoflegends.com/cdn/4.2.6/data/en_GB/champion.json";
$ch = curl_init();`
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $championsJson);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

$json = curl_exec($ch);
curl_close($ch);

$json_array = json_decode($json, true);

$champions =  $json_array["data"];

foreach ($champions as $championdata) {
    $image_url = $image_cdn.$championdata["image"]["full"];
    $image = file_get_contents($image_url);
    file_put_contents("imgfolder/".$championdata["image"]["full"], $image);
}

?>

So the idea of the code is to basically to decode a JSON and to download the images from the following website: http://gameinfo.na.leagueoflegends.com/en/game-info/champions/

The pictures download perfectly fine and are stored into a folder I have created on my hard drive. The next step is, is it possible for me to use this same piece of code, to download/display the images onto a website I have recently created:

www.lolguides4you.co.uk the website is basically a day old and I have literally been messing around with some HTML coding. I am new to both HTML and PHP so if someone could point me into the right direction, that would be great!

  • 写回答

1条回答 默认 最新

  • dongzhuan1185 2014-04-22 10:43
    关注

    Assuming that all you want to do it insert the images into a page on your website, then this is quite simple.

    However, it may be illegal for you to use an automated scraping tool to save/replicate/duplicate any of the images. Look into that before doing anything on the internet.

    The first step is to upload your previous PHP script to your website. That way, rather than downloading the images to your computer and then trying to upload them to your site it allows you to just save the files straight into a directory on the website.

    Next, you can create a basic PHP page anywhere on your site:

    <?php
    
    echo "Images downloaded:
    ";
    
    ?>
    

    Next you can use PHP's scandir() function to find every file in the download directory:

    <?php
    
    echo "Images downloaded:
    ";
    
    $files = scandir('imgfolder/');
    foreach($files as $file) {
      // Do something
    }
    
    ?>
    

    Finally, now you just show each image:

    <?php
    
    echo "Images downloaded:
    ";
    
    $files = scandir('imgfolder/');
    foreach($files as $file) {
      echo $file . "
    ";
    }
    
    ?>
    

    You could also use Glob, which allows you to ignore any files which aren't a certain type (in case you have other files in the same directory:

    <?php
    
    echo "Images downloaded:
    ";
    
    $files = glob("imgfolder/*.jpg");
    foreach($files as $file){
      echo $file . "
    ";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错