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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名