Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to download a file when you know the fname but not the file ext?

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
With my feeble knowledge of php I have written a script for uploading and downloading image files. These upload to a given directory and rename the file as a codenumber attached to a patient record on the database.
My problem is that I don't know the file extension of the image file in the directory as users can upload them in various formats (bmp, jpg, jpeg, png, gif).
Is there a way of sending php header info to the user by filename only (without ext)? Or to verify the ext of the file found in a given directory before sending?
Any advice or pointers would be much appreciated.
Thanks,
K
P.S. These filenames will never be duplicated in other formats (e.g. scan.gif & scan.jpg). So only 1 file will ever be found.
 
the usual way is to detect the image type on upload and append an extension to the encoded file name.

but you can get the extension after the fact too.

Code:
$filename = 'path/to/wanted/file';
$info = getimagesize($filename);
if($info && file_exists($filename)):
    header("Content-type: {$info['mime']}");
    readfile($filename);
    exit;
endif;

to get the extension for an image

Code:
$filename = 'path/to/wanted/file';
$info = getimagesize($filename);
if($info):
   $extension = image_type_to_extension($info[2]);
endif;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top