Skip to content

Instantly share code, notes, and snippets.

@mayurah
Last active August 29, 2015 14:06
Show Gist options
  • Save mayurah/bb5bbe0a9fe30f5cfaa4 to your computer and use it in GitHub Desktop.
Save mayurah/bb5bbe0a9fe30f5cfaa4 to your computer and use it in GitHub Desktop.
fetching images from URL and saving it into folder.
<?php
/*
* title: fetching images from URL and saving it into folder.
* curator: Mayur Pipaliya [ mayur {at} pipaliya {dot} com ]
* demo : http://pentest.0x10.info/temp_demo/parse_img.php
* copyleft (ɔ) distribute, alter and use at your own risk.
* apply necessary alternation accordingly.
*/
// TURN ON/OFF Debugging
define('DEBUG', false);
set_time_limit(0);
if(DEBUG == true)
{ error_reporting(-1); ini_set('display_errors', 'On'); echo '<pre>';}
else
{ error_reporting(0); ini_set('display_errors', 'off'); }
// Executing environment: Web / Command-line
if (isset($_SERVER['argc'])) {
define('CLI', true);
} else {
define('CLI', false);
//
$_url = $_REQUEST['url'];
$_folder = $_REQUEST['folder'];
}
if(CLI == true)
{
$_url = "http://www.desidime.com/";
$_folder = './desidime';
}
if($_url != null)
{
$html = file_get_contents($_url);
preg_match_all('/<img[^>]+>/i',$html, $result);
// var_dump($result); // DEBUG
$img = array();
foreach( $result[0] as $img_tag)
preg_match_all('/(alt|title|src)=("[^"]*")/i',$img_tag, $img[$img_tag]);
foreach($img As $img_url=>$imgs)
{
$url = str_ireplace(array('src="','"',""),"",$imgs[0][1]);
$imageName = array_shift(explode('?', basename($url)));
fetch_writeImage($url, $imageName );
if(DEBUG == true) { echo $url . " | " . $imageName . "\n"; } // DEBUG
}
}
else
echo "$_url is null.";
function fetch_writeImage($url, $imageName )
{
global $_folder;
if(!is_dir($_folder)) mkdir($_folder);
$fp = fopen($_folder . "/" . $imageName, 'w');
fwrite($fp, file_get_contents($url));
fclose($fp);
if(DEBUG == true) { echo $_folder . "/" . $imageName ."\n"; } // DEBUG
}
?>
<center>
<form action="">
Fetch Images<br><br>
URL: <input type="text" name="url" placeholder="http://desidime.com" style="width:500px;"/><br />
Direcoty: <input type="text" name="folder" placeholder="desidime" style="width:250px" /><br />
<input type="submit" name="submit" value="Fetch" />
</form>
</center>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment