Question

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!

Was it helpful?

Solution

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:\n";

?>

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

<?php

echo "Images downloaded:\n";

$files = scandir('imgfolder/');
foreach($files as $file) {
  // Do something
}

?>

Finally, now you just show each image:

<?php

echo "Images downloaded:\n";

$files = scandir('imgfolder/');
foreach($files as $file) {
  echo $file . "\n";
}

?>

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:\n";

$files = glob("imgfolder/*.jpg");
foreach($files as $file){
  echo $file . "\n";
}

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top