Possible Duplicate:
best way to determine if a URL is an image in PHP
Edited:
I have a script that copies remote files to my server using file_get_contents (which my host allows) to let users share pictures. To avoid abuse, however, I need code to check files for size, validity (and malicious code before allowing them on my server. So far, I've been unable to find a robust solution. Old articles that others have pointed to such as this one:
https://stackoverflow.com/questions/676949/best-way-to-determine-if-a-url-is-an-image-in-php
suggest possible approaches--checking file extension, mime type and using GD on files after they are on the server, but caution that none are foolproof. Yet countless sites allow remote uploading of files, suggesting there must be some better way to handle security.
File_get_upload is bare bones. Curl is gnarly but may have more functionality. GD's get_image_size function will return false if a file is not an image as in:
$imginfo_array = getimagesize($tempFile);
However, my understanding is GD cannot process a file remotely.
Here is simple code using file_get_contents t this stage:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Here is curl version:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Does anyone know the best practice to check file size and validity before they are on your server?