Friday 20 April 2012

Download image file using PHP


Hi friends,
 today my discussion is about how to download an image from an URL using php.OR download picture from the given URL.OR save image file using php.

 Here have two functions used for this:-
     file_get_contents() and file_put_contents()
   
    Code
<?php
set_time_limit(300) ;
$lnk='http://1.bp.blogspot.com/-SZgenQisfQc/TVPnClZOn5I/AAAAAAAABO8/zLFsX4UoFzg/s1600/The-best-top-desktop-dolphin-wallpapers-hd-dolphins-wallpaper-3.jpg';
file_put_contents('image.jpg',
    file_get_contents($lnk));   
?>

   




Code Description
 set_time_limit(300) ;   This statement is used to set the maximum execution time for the php script.It's default value  is 60 seconds.  Set this to 300 seconds allow the time to download the image.
 $lnk holds the URL of a wallpaper image to be download.

     file_get_contents() is used for get the contents from URL. Where file_put_contents() is used
 to write these contents to a file.
           

                 The lnk is given to the parameter to   file_get_contents().  The return data from this and
the name of file to be saved is given to file_put_contents().  Here all these are written in single step.
When you run this script the image will downloaded  to the location where your php page is saved
with the name 'image.jpg'.  To change the download location give it to file_put_contents() parameter
like this file_put_contents( 'imgfolder/img/image.jpg' , '...'  );

Also read this :- Include a page from url in php

Hope that this post is helpful to you.
Thanks By Sukesh B R

1 comment:

  1. Excellent job....
    Expecting more from you..
    Gud luck fr a better 2mrw

    ReplyDelete

Search This Blog