File Extension Problem

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
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

File Extension Problem

Post by abionifade »

I have created a system which allows users to upload files to file storage via a web interface. A administrator is then able to download the file via the same interface. The details of the file are automatically entered into the database. Filename, filetype etc.

Just so that no two files of the same filename can be added to the filestorage, I am renaming the file upon submission. The new file will now consist of the module they are submitting to AND add a timestamp. For example: - builder.doc after submission is converted to computing-100312323.

My problem is that word documents and all non-html files now loose their extensions and my browser attempts to download them as text files!

Any suggestions as to how to tweak my code to stop this?

Here is an extract of the code that handles the creation and storage of the file to be submitted. If you need any more of it, please let me know. Its quite a few lines so i have pasted the most important part of it.

********************************************************

$timestamp = time();
$filedir = "/program files/apache group/apache/htdocs/sis/primary/final/files/work/";
$filenamenew = strtolower(str_replace(" ", "", $module_title)) . "-" . $timestamp;

$filestore = $filedir . $filenamenew;

*********************************************************

Thanks for your help in this

-Abi
gabe
Forum Newbie
Posts: 7
Joined: Thu Apr 18, 2002 4:04 pm
Location: Essex, Ma USA
Contact:

Append the original extension to the new filename

Post by gabe »

Either use the original filename's extension on the temporary file (may want to use a microtime() timestamp to absolutely avoid possible collisions) or create a wrapper script that would set a content-disposition header to set the filename, and then just readfile() the temp. file to output it to the browser.

btw, content-disposition looks like this (just in case you've never used it):

Code: Select all

header('Content-Disposition: attachment; filename=whatever.doc');
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Still stuck

Post by abionifade »

Hi Gabe,

really appreciate your taking the time to look at my problem. Im unsure as to what you are suggesting for me to do. I am a newbie to PHP. :D

Should i post my entire script so you can point at the particular spot where i should insert new code?

-Abi
gabe
Forum Newbie
Posts: 7
Joined: Thu Apr 18, 2002 4:04 pm
Location: Essex, Ma USA
Contact:

sure.

Post by gabe »

Go for it...
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

What he was saying is that you have to append the .ext to the end of the file while renaming it.

Code: Select all

$filenamenew = strtolower(str_replace(" ", "", $module_title)) . "-" . $timestamp . ".doc";

I would make the extention naming dynaming to refelect the type of file.

Code: Select all

$len = strlen($HTTP_POST_FILESї'userfile']ї'name']);
$ext = substr($HTTP_POST_FILESї'userfile']ї'name'],$len - 3,3);

$filenamenew = strtolower(str_replace(" ", "", $module_title)) . "-" . $timestamp . "." . $ext;
Note this will only work with files that have e char extentions. You could also do this:

Code: Select all

$block = explode(".",$HTTP_POST_FILESї'userfile']ї'name']);
$len = (count($block)-1);
$ext = $blockї$len];
Cheers Sam
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post by abionifade »

$len = strlen($HTTP_POST_FILES['userfile']['name']);
$ext = substr($HTTP_POST_FILES['userfile']['name'],$len - 3,3);

this worked perfectly, the downloads are coming down.

Thank you so much for your help guys,

Sam - can i ask you to explain what you did as im unsure of whats happening in the above lines of code and really want to get a grasp of how you acheived it.

-Abi
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Sure:

Code: Select all

$len = strlen($HTTP_POST_FILESї'userfile']ї'name']);
is getting the length of the temp file name of the uploaded file.

Code: Select all

$ext = substr($HTTP_POST_FILESї'userfile']ї'name'],$len - 3,3);
Here I cut from the end of the filename the last 3 chars which would be the extention for windows programs.

Cheers Moe
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post by abionifade »

Thanks alot for this Moe,

you really have been of help. The response time on this forum is amazing. I Hope to be of contribution to this forum as i polish my php/mysql skills.
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Well I'm sure I speak for all of us when I say we look forward to seeing you around.

Cheers Sam
Post Reply