<?
error_reporting(E_ALL);
// define the filename extensions
define('ALLOWED_FILENAMES', 'jpg|jpeg|gif|png');
// define a directory
define('IMAGE_DIR', 'image');
// check against a regexp for an actual http url and for a valid filename,
$url = "http://www.philwebcreative.com/philweb_logo.jpg";
if(!preg_match('#^http://.*([^/]+\.('.ALLOWED_FILENAMES.'))$#', $url, $m)) {
die('Invalid url given');
}
// try getting the image
if(!$img = file_get_contents($url)) {
die('Getting that file failed');
}
// try writing the file with the original filename
if(!$f = fopen(IMAGE_DIR.'/'.$m[1], 'w')) {
die('Opening file for writing failed');
}
if (fwrite($f, $img) === FALSE) {
die('Could not write to the file');
}
fclose($f);
?>
article
Thursday, July 15, 2010
php - Upload image from url
php - Upload image from url