3. Now lets examine the changes in the PHP coding. First up where we tested for MIME types instead of file extensions:
if(strlen($_POST['srcurl']) > 0){ $acceptedfiletypes = array('.jpg', '.gif', '.png', '.jpeg'); $filetype = strtolower(strrchr($_POST['srcurl'], '.')); } else { $acceptedfiletypes = array('image/jpeg', 'image/gif', 'image/png'); $filetype = $_FILES['srcimage']['type']; }
As you can see this section is much fatter than before. Unfortunately, as described above, unless you have some PHP extensions installed (which if you are like me and are on shared hosting won’t happen) you will need to determine the file type by trusting the file extension.
The first line if(strlen($_POST['srcurl']) > 0){ looks to see if the user is using the address field to upload an image, or whether they are uploading a file with the form, the address field has taken priority and is tested first, meaning, if the user has entered both text and selected a file locally then the address bar image will be selected. You can remove this issue via a radio button selection tool.
The script then sees if the entries are in the given arrays, if it is it proceeds.
4. The next change occurs when the file needs to be moved. In this case the file must be fetched, named, moved, saved. This happens here:
if(strlen($_POST['srcurl']) > 0){ $grab = curl_init(); curl_setopt($grab, CURLOPT_URL, $_POST['srcurl']); curl_setopt($grab, CURLOPT_HEADER, false); curl_setopt($grab, CURLOPT_RETURNTRANSFER, true); $grabbed = curl_exec($grab); curl_close($grab); $filename = strtolower(md5((rand() * time()).$_SERVER['REMOTE_ADDR']).$filetype); $file = fopen($tmppath.$filename, 'w'); fwrite($file, $grabbed); fclose($file); } else { $filename = strtolower(md5((rand() * time()).$_SERVER['REMOTE_ADDR']).strrchr($_FILES['srcimage']['name'], '.')); move_uploaded_file($_FILES['srcimage']['tmp_name'], $tmppath.$filename); }
It first checks if the file upload method is using an external file. If it is it handles the file then we come to the only cURL code we need:
$grab = curl_init(); curl_setopt($grab, CURLOPT_URL, $_POST['srcurl']); curl_setopt($grab, CURLOPT_HEADER, false); curl_setopt($grab, CURLOPT_RETURNTRANSFER, true); $grabbed = curl_exec($grab); curl_close($grab);
The first line initialises a cURL object. The second line curl_setopt($grab, CURLOPT_URL, $_POST['srcurl']); sets the URL option of the cURL object to whatever was entered into form. The third line sets the cURL output to not include the HTML header file, which would complicate way we handle the image. The 4th line does something similar, but sets the output to be stored into a string as the default is to be printed to the screen. The 5th line tells cURL to start the transfer, and store the fetched data into the $grabbed variable. Once done close the cURL object. Easy huh?
So back to the code above, if the upload method isn’t via a link, then use the method we had in the previous tutorial, the upload method.
5. The only other change is where we choose what canvas to begin from.
if(strlen($_POST['srcurl']) > 0){ switch ($filetype){ case '.jpg': $srcimage = imagecreatefromjpeg($tmppath.$filename); break; case '.jpeg': $srcimage = imagecreatefromjpeg($tmppath.$filename); break; case '.gif': $srcimage = imagecreatefromgif($tmppath.$filename); break; case '.png': $srcimage = imagecreatefrompng($tmppath.$filename); break; default: $srcimage = imagecreatefromjpeg($tmppath.$filename); } } else { switch ($filetype){ case 'image/jpeg': $srcimage = imagecreatefromjpeg($tmppath.$filename); break; case 'image/gif': $srcimage = imagecreatefromgif($tmppath.$filename); break; case 'image/png': $srcimage = imagecreatefrompng($tmppath.$filename); break; default: $srcimage = imagecreatefromjpeg($tmppath.$filename); } }
As you might already recognise the first line tests what method of upload we are using. Then it executes the case statement according to the method. The key difference here is that if we are using the cURL method, we are determining which canvas to use on the basis of file extensions. Whereas the fileupload method is based on MIME types. I don’t need to repeat myself again as to why we are using this method.
From there our script just continues to handle the files stored on the server, so it doesn’t really make any difference what method you are using beyond this point. That’s it! now run along and play!




Comments About Tutorial: How to make an Image Upload and Thumbnailer Script with cURL
// 3 comments so far.
ghprod // August 17th 2008
Hi … thnx 4 great script
Sebastian // August 20th 2009
Havent watched your code, I’ve tried the demo though. Extensions like JPG, PNG, GIF and all other uppercases, are not accepted. You should use uclower on the relative image path to get it all lower case before you perform the extension check.
VoiDeT // August 20th 2009
Hi Sebastian.
Not too sure what you’re talking about here.
The uppercased filenames are working fine.
There is no such function uclower also.
Take a look at this line:
$filetype = strtolower(strrchr($_POST['srcurl'], ‘.’));
That lowers the file name.
You can follow any responses to this entry via its RSS comments feed. You may also leave a trackback by clicking this link.