File Extension Problem
Moderator: General Moderators
- abionifade
- Forum Commoner
- Posts: 34
- Joined: Thu Apr 18, 2002 5:32 pm
File Extension Problem
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
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
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):
btw, content-disposition looks like this (just in case you've never used it):
Code: Select all
header('Content-Disposition: attachment; filename=whatever.doc');- abionifade
- Forum Commoner
- Posts: 34
- Joined: Thu Apr 18, 2002 5:32 pm
Still stuck
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.
Should i post my entire script so you can point at the particular spot where i should insert new code?
-Abi
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.
Should i post my entire script so you can point at the particular spot where i should insert new code?
-Abi
sure.
Go for it...
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
What he was saying is that you have to append the .ext to the end of the file while renaming it.
I would make the extention naming dynaming to refelect the type of file.
Note this will only work with files that have e char extentions. You could also do this:
Cheers Sam
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;Code: Select all
$block = explode(".",$HTTP_POST_FILESї'userfile']ї'name']);
$len = (count($block)-1);
$ext = $blockї$len];- abionifade
- Forum Commoner
- Posts: 34
- Joined: Thu Apr 18, 2002 5:32 pm
$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
$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
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
Sure:
is getting the length of the temp file name of the uploaded file.
Here I cut from the end of the filename the last 3 chars which would be the extention for windows programs.
Cheers Moe
Code: Select all
$len = strlen($HTTP_POST_FILESї'userfile']ї'name']);Code: Select all
$ext = substr($HTTP_POST_FILESї'userfile']ї'name'],$len - 3,3);Cheers Moe
- abionifade
- Forum Commoner
- Posts: 34
- Joined: Thu Apr 18, 2002 5:32 pm