I am uploading a file to a mysql database table. Now let me say that this works perfectly fine for PDF, XLS, and DOC files. However if I upload an XLSX (office 2007 file), it uploads fine, however when it is downloaded, it cannot be read.
I can open both the original uploaded file and the one I downloaded in wordpad and they look EXACTLY the same.
HELP!
Additionally I am noticing that with Office 2007 files, the mime type is being truncated at 50 in my PHP (verified with a simple echo)
The mime type of "applicatio n/vnd.openxmlform ats-officedocument. spreadsheetml.s heet"
is truncated to "applicatio n/vnd.openxmlform ats-officedocument. spre"
However if I manually set the mime type in the database to the correct value, it still cannot be opened correctly.
Any advice?
Thank you in advance for your time!
(Please be aware that these are not the full scripts. I know theres more to it and I do some validation here and there. I thought I could skip the common fluff)
Insert.php
Download.php:
I can open both the original uploaded file and the one I downloaded in wordpad and they look EXACTLY the same.
HELP!
Additionally I am noticing that with Office 2007 files, the mime type is being truncated at 50 in my PHP (verified with a simple echo)
The mime type of "applicatio n/vnd.openxmlform ats-officedocument. spreadsheetml.s heet"
is truncated to "applicatio n/vnd.openxmlform ats-officedocument. spre"
However if I manually set the mime type in the database to the correct value, it still cannot be opened correctly.
Any advice?
Thank you in advance for your time!
(Please be aware that these are not the full scripts. I know theres more to it and I do some validation here and there. I thought I could skip the common fluff)
Insert.php
Code:
$name = mysql_real_escape_string($_FILES['c_filename']['name']);
$mime = mysql_real_escape_string($_FILES['c_filename']['type']);
$data = mysql_real_escape_string(file_get_contents($_FILES['c_filename']['tmp_name']);
$size = intval($_FILES['c_filename']['size']);
// I do some validation between then and now... Not going to bore you with that part
$query = "INSERT INTO `$filetable`
(filename, filetype, filesize,bin_data)
VALUES
('{$name}', '{$mime}', {$size}, '{$data}')";
$result = mysql_query($query)
or die ("Invalid query!!!<br><br>".$query);
Code:
//sql query was executed that selected that record.
$name = mysql_result($result,0,"filename");
$size = mysql_result($result,0,"filesize");
$type = mysql_result($result,0,"filetype");
$content= mysql_result($result,0,"bin_data");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
echo $content;
Comment