Finding a file where file extension is variable

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
csingsaas
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 9:34 pm
Location: Edina, MN

Finding a file where file extension is variable

Post by csingsaas »

I am developing a classified ads site where users are allowed to upload up to four photographs of the product they are selling. I have limited the uploads to gif's and jpeg's. Currently, I am working on a method for users to login to their account and delete a photo or replace an old photo with a new upload.

Initially, when the client uploads photos - the image files are renamed to the 7 digit ad id number with a 1,2,3 or 4 appended to it.

Example where advertisement id is 100293

1002931.jpg
1002932.GIF
1002933.gif
1002934.JPEG

Should the user choose to delete the image in the 1st spot (1002931.jpg), I will renumber all of the other images so that they move up one spot. Below is what the file names would look like if the client deleted image #1:

1002931.GIF
1002932.gif
1002923.JPEG

My problem is that I am having problems making the script fool proof. Is there a way to search for a file NOT by the file name and extension, rather by simply the filename prior to the extension?

Can I search to see if file 1002923 exists and then determine what the extension is?

I don't want to have to create an array which stores all of the possible file extension names (i.e. jpg, jpeg, JPG, JPEG, gif, GIF, Jpg, Jpeg, Gif, etc.)

Hopefully it is clear what I am asking, it is a bit confusing for me to put into words. Thanks.

Regards,
Cody Singsaas
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Should the user choose to delete the image in the 1st spot (1002931.jpg), I will renumber all of the other images so that they move up one spot. Below is what the file names would look like if the client deleted image #1:

1002931.GIF
1002932.gif
1002923.JPEG
You're saying your application allows two images with different extensions but with the same filename e.g. 1002931.jpg and 1002931.GIF? Or is that a typo?
csingsaas
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 9:34 pm
Location: Edina, MN

Post by csingsaas »

The file name will never be the same. The user is allowed to upload up to 4 images (gif or jpeg's). They must upload at least one image but do not have to upload all four of them.

As an example, if the user uploaded all 4 photos as:

#1 image = 1002931.jpg
#2 image = 1002932.GIF
#3 image = 1002933.gif
#4 image = 1002924.JPEG

Then, the user wants to delete image #1 for whatever reason. Since we must have an image in the #1 position, I must renumber the other 3 (essentially bumping them up). The result would be:

#1 image = 1002931.GIF
#2 image = 1002932.gif
#3 image = 1002923.JPEG

Does this help?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

use glob()

Code: Select all

<?php

$files = glob("/path/to/images/{$userid}*.{gif,jpg,GIF,JPG,jpeg,JPEG}", GLOB_BRACE);

?>
csingsaas
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 9:34 pm
Location: Edina, MN

Post by csingsaas »

Right on, that is exactly what I was looking for. Thanks a ton.

Regards,
Cody Singsaas
IE Internet Solutions
http://www.ieinternetsolutions.com
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

What if the file extension were to be .jPg?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Personally, I'd lowercase everything right when the upload happens. Then all you have to do is build an array of accepted image types: jpg and gif (I wouldn't even accept jpeg: rename it to jpg).

Make sure you're checking that the file is what it really is, otherwise you could be vulnerable to a WMF masquerading as a jpg.
csingsaas
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 9:34 pm
Location: Edina, MN

Post by csingsaas »

Since I do not know exactly what the file extension is going to be (.jpg, .jpeg, .Jpeg, .Jpg, etc.) because the user is responsible for uploading their own files - here is what I've done:

Assumptions:
$hid = the unique id of the ad
$pid = the picture number. Remember they can have up to four photographs so this will carry a value of 1,2,3, or 4.

Code: Select all

$hid = $HTTP_GET_VARS['horseid'];
$pid = $HTTP_GET_VARS['picid'];

$picname = $hid.$pid;
$err_msg = 0;

$pic1array = glob($hid."1.*");
$pic1arraysize = sizeof($pic1array);
if ($pic1arraysize == 1) {
	$pic1name = $pic1array[0];
} else {
	$err_msg = 1;
}

unlink($pic1name);

switch ($pid) {
	case 1:
		$pic2array = glob($hid."2.*");
		$pic2arraysize = sizeof($pic2array);
		if ($pic2arraysize == 1) {
			$pic2name = $pic2array[0];
			$pic2ext = substr(strstr($pic2name, '.'),1);
			rename($pic2name, $hid."1.".$pic2ext);
		} else {
			$err_msg = 1;
		}
	case 2:
		$pic3array = glob($hid."3.*");
		$pic3arraysize = sizeof($pic3array);
		if ($pic3arraysize == 1) {
			$pic3name = $pic3array[0];
			$pic3ext = substr(strstr($pic3name, '.'),1);
			rename($pic3name, $hid."2.".$pic3ext);
		} else {
			$err_msg = 1;
		}
	case 3:
		$pic4array = glob($hid."4.*");
		$pic4arraysize = sizeof($pic4array);
		if ($pic4arraysize == 1) {
			$pic4name = $pic4array[0];
			$pic4ext = substr(strstr($pic4name, '.'),1);
			rename($pic4name, $hid."3.".$pic4ext);
		} else {
			$err_msg = 1;
		}
	case 4:
}
Regards,
Cody Singsaas
IE Internet Solutions
http://www.ieinternetsolutions.com
Post Reply