Page 1 of 1
allow uploading 2 file extensions and change the file name
Posted: Tue Sep 12, 2006 5:02 am
by ansa
Hi,
how to allow uploading two different extension files?
I've used the following code to check if the extension is not GIF nor JPG
Code: Select all
if ($_FILES['picuploaded']['type'] !="image/gif" or $_FILES['picuploaded']['type'] !="image/jpg") {
$ok=0;
}
But I received $ok = 0 when I uploaded the right extensions (which is GIF or JPG). If I just use one segment, so like this:
Code: Select all
if ($_FILES['picuploaded']['type'] !="image/gif") {
then, it is okay. What did I do wrong at the first code?
And second question, I want to change the file name once it is uploaded, Lets say to: $serial_number.jpg or $serial_number.gif (the extension stays the same as the original).
Does anyone know?
Posted: Tue Sep 12, 2006 5:30 am
by bokehman
Should be and not or. Any file name may be allocated as the second argument to move_uploaded_file() as long as the file system allows it.
Posted: Tue Sep 12, 2006 6:27 am
by Rovas
Use this kind of structure for check of the extension
Code: Select all
if (jpg)...
else if (gif)
else {error message}
I don' t understand the question: do you want to change the extension after uploading i.e transform a jpeg file into a gif. If this is what you want you can use PHP/GD some intro tutorials are here:
http://www.goodphptutorials.com/track/15
http://www.goodphptutorials.com/track/176
Posted: Tue Sep 12, 2006 7:00 am
by feyd
Stay away from checking the type given by the submission and stay away from checking the extension. Both can easily be falsified.
getimagesize()..
Posted: Tue Sep 12, 2006 7:39 am
by ansa
Answering Rovas' question:
I don't want to change the extension, I just want to change the name of the uploaded file.
For example: I'm uploading a file named mypic.GIF from my hard drive. After it is uploaded, the name should be changed to 123.GIF on the webserver. As you see, the extension stays the same, only the file name is changed.
The reason I'm doing this, because, I'm building a website where users can upload their photos. It is likely that, several users have the same file name. If it is true, the old photo will be overwritten by the newest one with the same name. Changing the file name into a unique serial number will avoid this.
I just wonder how I can do that...
Posted: Tue Sep 12, 2006 8:12 am
by bokehman
Either keep details of the photos in the DB and get an ID from the DB for each photo or use something like time() as the file name.
Posted: Tue Sep 12, 2006 8:55 am
by volka
I wouldn't use anything from the "original" filename but build it from that serial number and the info of getimagesize/exif_imagetype only.
Code: Select all
<?php
$imageinfo = getimagesize();
// missing: check wether it's an image at all
switch( $imageinfo[2] ) {
case IMAGETYPE_GIF:
$postfix = '.gif';
break;
case IMAGETYPE_JPEG:
$postfix = '.jpeg';
break;
default:
// missing: error handling
break;
}
$newFilename = $serial . $postfix;
?>
Posted: Tue Sep 12, 2006 12:42 pm
by ansa
Thanks. That enlighted me a little bit.
One more question, though. You wrote: check if it's an image at all.
How can I do that? Checking the extension is not enough?
Posted: Wed Sep 13, 2006 1:50 am
by jmut
ansa wrote:Thanks. That enlighted me a little bit.
One more question, though. You wrote: check if it's an image at all.
How can I do that? Checking the extension is not enough?
http://php.net/function.getimagesize
If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will return FALSE and generate an error of level E_WARNING.