1

I have a simple upload image function where user gets to choose what image to be uploaded for the profile pic. The image function allows every type of image. Once the user uploads the image file, it will save in a directory specified by me in the public folder where the name will change to something like this.

if the userId = 32
then the image will be stored as 32.ext

where "ext" is the image's extension.

My question is when retrieving the image when the user finished uploading and goes to the profile page. I can only do one type of filetype say jpg not png or any extension.

<img src="<?php echo "/public/images/event/".$userDetails->id.".jpg";?>" />

My question is how to check for extension and depending upon the extension it will show up like that itself. Say the user uploaded a png file, the output will be 32.png , but right now I am hardcoding the file extension to be jpg.

Pranaya Behera
  • 545
  • 1
  • 9
  • 24

4 Answers4

1

You can find the file extension in php like this:

$file_parts = pathinfo($filename);

$file_extension = $file_parts['extension'];

Or if you don't know the extension, you have tot do something like this:

$dirname  = "./somedir/"; 
$filename = "myfile"; 
$iterator = new RegexIterator(new DirectoryIterator($dirname), '/^' . preg_quote($filename) . '\.[^.]+$/iD'); 

// Any matching files? 
if (iterator_count($iterator) > 0) { 
    echo "Matches found:\n"; 
} else { 
    echo "No matches found.\n"; 
} 

// Get extensions of matching files 
foreach ($iterator as $fileinfo) { 
    printf("%s -> %s\n", $fileinfo, pathinfo($fileinfo, PATHINFO_EXTENSION)); 
}

As seen on http://www.sitepoint.com/forums/showthread.php?631513-possible-check-if-file-filename-exists-w-o-knowing-its-extension

veelen
  • 1,916
  • 19
  • 26
0

You can use $_FILES or pathinfo() in server side

$extension = $_FILES["file"]["type"]; 

or

$extension = pathinfo($filename, PATHINFO_EXTENSION);
curious_coder
  • 2,392
  • 4
  • 25
  • 44
0
<?php

$directory_path="/public/images/event/";

$matches = glob($directory_path.$userDetails->id.".*");

//assuming there is only one user id expect only one file in the array 
$image_file=$matches[0];

$extension=pathinfo($image_file, PATHINFO_EXTENSION);
echo "<img src='".$directory_path.$userDetails->id.$extension."' />";
Bere
  • 1,627
  • 2
  • 16
  • 22
0

You have several solutions at hand.

  1. Convert every image to the one format you support, even if it seems to be in that format already. This has the benefit of added security, as the conversion will hopefully not be affected by malicious payload inside the image, but only transform or copy the pixel info, but otherwise create an entirely new image.

  2. Ignoring the security implications of offering user uploaded content, you could also just get the file type by using getimagesize() on the file when it is uploaded and store the detected file type in your database as well as use this info for the filename extension itself. Note that browsers might have fallback mechanism implemented that treat a JPG as a JPG even if the server says that the extension is ".gif" and the mime type is "image/png". Users can and will fail uploading images with the correct extension matching the real content. Expect to get all kinds of strange formats.

  3. You could also change the way you get to know the image name completely. Just store it in the database - this is only slightly more overhead than storing only the extension. You cannot however simply create the image name just by knowing the user id then.

  4. There is no need for an extension in the URL alltogether. The only requirement from a browser is that there should be a mimetype matching the content. But autodetection is implemented, you might end up getting an image displayed even with a wrong mimetype, but also the Apache webserver might have mime-magic detection implemented and will send the correct mimetype for every image served without having a file extension.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • As of now I have disabled uploading other than jpeg files. But will try to come up with an ans. Thanks for the info though. Helpful but not implemented as of now in my code base. – Pranaya Behera Jun 07 '13 at 11:03