Reputation: 545
I have a MySQL database where I store various file types. If the file extension is the standard three character (.doc, .xls, .pdf), then the content type is stored as application/msword, application/ms-excel, application/pdf, etc. If it's .docx or .xlsx, then the content type is application/vnd.openxmlformats-officedocument.
Until recently, this has never been a problem, but within the past few weeks, it's become a problem in Firefox. Firefox will not download files of type application/vnd.openxlmformats-officedocument in their correct formats. Instead, it downloads the file without an extension and the user has to add it manually. Furthermore, if there are spaces in the filename, then Firefox only picks up the first word in it and that's how the file is saved.
Here is the code I use to upload files:
if($_FILES['Budget']['size'] > 0)
{
$fileName = $_FILES['Budget']['name'];
$tmpName = $_FILES['Budget']['tmp_name'];
$fileSize = $_FILES['Budget']['size'];
$fileType = $_FILES['Budget']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
fclose($fp);
$fileUp = $con->prepare("INSERT INTO ptfs.upload (ProposalNo, name, size, type, content) VALUES(:proposalno,:name,:size,:type,:content)");
$fileData=array('proposalno'=>$proposalNo,'name'=>$fileName,'size'=>$fileSize,'type'=>$fileType,'content'=>$content);
$fileUp->execute($fileData);
}
And here is the code for presenting the file link to the user:
if(isset($_GET['ProposalNo']) && isset($_GET['UID']))
{
$fileget = $con->prepare("SELECT name, type, size, content FROM upload WHERE ProposalNo = :proposalno AND UID = :uid");
$data = array('proposalno'=>$_GET['ProposalNo'],'uid'=>$_GET['UID']);
$fileget->execute($data);
list($name, $type, $size, $content) = $fileget->fetch(PDO::FETCH_BOTH);
header("Content-Disposition: attachment; filename=$name");
header("Content-type: $type");
header("Content-length: $size");
echo $content;
exit;
}
This works fine in every browser except Firefox, and as I said, it's a recent problem. My users started reporting it within the last couple of weeks. Can I modify either my code or my database to make sure that FF downloads these file types correctly again?
Upvotes: 2
Views: 2162
Reputation: 74217
"Furthermore, if there are spaces in the filename, then Firefox only picks up the first word in it and that's how the file is saved."
It's always best to catch the problem right away (before the file is uploaded and entered into DB) and replace spaces with underscores, then let PHP do its thing afterwards.
Consider the following which is the logic I use for my uploaded files, which will transform:
This is a line
into:
This_is_a_line
<?php
$string = "This is a line";
$arr = explode(" ",$string);
$string = implode("_",$arr);
echo $string;
?>
This taken from my own experiences with the same issue that resolved it.
Upvotes: 2