allow uploading 2 file extensions and change the file name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

allow uploading 2 file extensions and change the file name

Post 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?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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()..
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

Post 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...
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
?>
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

Post 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?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
Post Reply