The script that is linked to has too many problems.
Why use mysql_real_escape_string() if you aren't going to connect to mySQL?
filesize( $url ) fails with x10's settings. You either have to load until you get to your size limit and then fail, or send a HEAD request to find out the size of the file.
OK, with some caveats:
1. If you use this to download copyrighted material, or with the intent to set up a phishing page, you are on your own.
2. If the site with the image has hotlink protection, no go. Talk to the guy who supplied the link.
3. If the link to the image is 'nonstandard', no go.
4. Resizing the image is up to you.
5. Sanatizing the user supplied URL is up to you.
I use cURL.
First, I check to see if the file extension is one I can use.
Second, I use cURL with a HEAD request to find the size of the file. Set your own limit in the script.
Then, I finally get the file and store it locally. Since I tested it on known files, I used the remote name. You might want to use 'username.jpg' or similar.
Up to you to figure out how to resize and convert to common format.
Save thumbnail and unlink downloaded original.
PHP Code:
$url = 'http://somesite.com/images/cuddlykoala.png' ;
// FIRST, FIND IF THE FILE TYPE (BY LOOKING AT THE EXTENSION) IS SUPPORTED
$filename = basename( parse_url($url, PHP_URL_PATH) ) ; // get just the filename
$valid_exts = '/\.(jpg|jpeg|gif|png)$/i' ; // add image extension as you want...
if( ! preg_match( $valid_exts , $filename , $match ) ) {
echo "$filename does not have a supported extension.";
exit ;
}
$extension = $match[ 1 ] ; // the matched extension, without dot
// SECOND FIND THE SIZE OF THE FILE...
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true); // we want the header...
curl_setopt($ch, CURLOPT_NOBODY, true); // NO BODY, ie do not get body of url, ie only the header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // output results as string
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) { ## this is false if cURL failed.
echo 'cURL failed to get HEAD request.';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}
if( $status != 200 ){
echo "Sorry, HEAD request returned a status of $status" ;
exit;
}
if( $contentLength == 'unknown' ){
echo "Sorry, HEAD request did not return content length";
exit ;
}
if( $contentLength > 500000 ){
echo "Sorry, a filesize of $contentLength is too large" ;
exit ;
}
// FINALLY, GET THE FILE ITSELF ...
$ch = curl_init($url);
$fp = fopen( $filename, "w"); // open a local file to write to, using their filename
// probably should use your own, like 'username.' . $extension
curl_setopt($ch, CURLOPT_FILE, $fp); // WHERE TO STORE IT
curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT WANT THE HEADER
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
if ($data === false) {
echo 'cURL failed to get image.';
exit;
}
// use GD etc to resize file and maybe change format so all thumbnails are consistent
// store thumbnail and unlink $filename
echo "<a href='/$filename' >Go To Local Copy</a>" ;