Image Download script from a third party sever using PHP

image download

Web Scraping (also termed Screen Scraping, Web Data Extraction, Web Harvesting, etc.) is a technique employed to extract large amounts of data from websites whereby the data is extracted and saved to a local file in your computer or to a database in table (spreadsheet) format.

Data displayed by most websites can only be viewed using a web browser. They do not offer the functionality to save a copy of this data for personal use. The only option then is to manually copy and paste the data – a very tedious job which can take many hours or sometimes days to complete. Web Scraping is the technique of automating this process so that instead of manually copying the data from websites, the Web Scraping software will perform the same task within a fraction of the time.

In this tutorial, we are using PHP to Download Image From web URL and Store into our local server, This functionality called Web scraping.

Generally, Web Scraping is using different languages like PHP, jQuery, Javascript or Python, If you want to know about more knowledge of web scraping follow this link Web scraping.

Now we have to start our tutorial, Just follow the steps. Create a Folder name ImagesDownloadScripts in your local directory.

Step 1: Create a file name config.php

You have to open your text editor and put the below code.

<?php
define("F:\X\htdocs\ImagesDownloadScripts","images");
define("csv_file","Download_image_Dhaval.csv");

Step 2: Create the Main file.

You have to create a main File name is ImageDownloadScript.php and put the below code.

<?php
require_once 'config.php';
class ImageDownloadScript{
    function __construct(){
        $this->folder = folder;
        if (!file_exists($this->folder)) {
            mkdir($this->folder, 0777, true);
        }
        $this->count = 0;
    }
}// CLASS END HERE

In this code, you have to give permission to create a folder in your local directory, and image store into your folder. Now we have created two methods saveImage & getFileExtension. Here the final code of our script.


<?php
require_once 'config.php';
class ImageDownloadScript{
    function __construct(){
        $this->folder = folder;
        if (!file_exists($this->folder)) {
            mkdir($this->folder, 0777, true);
        }
        $this->count = 0;
    }
    function saveImage($title,$link){
        $file_contents = file_get_contents(trim($link));
        $file_extension = $this->getFileExtension($file_contents);
        $title = implode("_",explode(" ",trim($title)));
        $file_name = $this->folder."/".$title.".".$file_extension;
        file_put_contents($file_name,$file_contents);
        $this->count++;
        echo "\n".$file_name." stored.  Total: ".$this->count;
    }
    function getFileExtension($file_contents){
        $file_info = new finfo(FILEINFO_MIME);
        return trim(explode(';',explode('/',$file_info->buffer($file_contents))[1])[0]);
    }
}// CLASS END HERE
$file = fopen(csv_file,"r");
$downloader = new ImageDownloadScript();
while (($row = fgetcsv($file)) !== FALSE) {
    if(trim($row[0]) == "title")
        continue;
    $downloader->saveImage($row[0],$row[1]);
}

Step 3: Create a .csv file in your directory.

Create a .csv file name is Download_image_Dhaval.csv this name is given in config.php file so you have just followed the config file.

Download_image_Dhaval

In this step, you have to give the name and URL of your image to download the image.

Step 4: Run the Script

In this step, we have top open your directory and run the script.


After the run the script you got this kind of result.


Now open your directory and you got new Folder name is a folder.


You can download any type of web image URL to download it.

About Dhaval Shah

My name is Dhaval Shah. I'm a Laravel developer. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress, and Bootstrap.
Follow me on Github / Linkedin

2 Comments

  1. Amazing! This blog looks just like my old one! It’s on a
    totally different subject but it has pretty much
    the same layout and design. Great choice of colors!

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!