Saturday, June 2, 2012

copy image using wget PHP

Hello everyone,
I have just written a PHP script to wget any image.
All you need to do is get the full path of your image e.g.
http://www.google.co.in/images/srpr/logo3w.png is path to google logo.
So now lets wget google's logo.


<?php


$mainimage = "http://www.google.co.in/images/srpr/logo3w.png"; //this would be the path of your live image which you want to download

$thebasename =  basename($mainimage); //here we will get logo3w.png i.e. name of file


$filename = explode(".", $thebasename); //explode it with extention
$i=0;
do {
if($i > 0) $filename[0]++;
else $filename[0] = $filename[0].$i;
$i++;
    }while(file_exists("tmp_cover/".$filename[0].".".$filename[1])); //typical do while to check for unique file name
 $image_dir = "tmp_cover/"; //this will be your directory where you want to save your image
 $fresh_file = $filename[0].".".$filename[1]; //this is our renamed file if file already exists
 $image_drs = $image_dir.$fresh_file; //i have concatinated the path so it becomes easier to use in wget function
 system("wget -q \"$mainimage\" -O $image_drs");  //image uploaded with wget -q


 /*
 Notes:
 1)Please check if your wget version is wget v1.12 and php is php v5.3.1 or greater.
 2)Give full permissions to your files (chmod 777 your_dir) or use filezilla client to give full permission to your folder.
 3)I have created tmp_cover folder at /opt/lampp/htdocs with 777 permissions.
/*

?>