Page 1 of 1

File Extension Problem

Posted: Thu Apr 18, 2002 5:32 pm
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

Append the original extension to the new filename

Posted: Thu Apr 18, 2002 8:26 pm
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');

Still stuck

Posted: Fri Apr 19, 2002 8:26 am
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

sure.

Posted: Fri Apr 19, 2002 8:53 am
by gabe
Go for it...

Posted: Fri Apr 19, 2002 11:16 am
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

Posted: Fri Apr 19, 2002 5:15 pm
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

Posted: Fri Apr 19, 2002 6:47 pm
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

Posted: Fri Apr 19, 2002 8:27 pm
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.

Posted: Fri Apr 19, 2002 8:38 pm
by sam
Well I'm sure I speak for all of us when I say we look forward to seeing you around.

Cheers Sam