error in download a file from mysql
hello i have tryed a lot of source codes for this but i have the same error in all of them
include 'functii.php';
starts();
opendb();
$query = "SELECT content,`title_real`,`size`,`ext` FROM file WHERE file_id = '3'";
$result = mysql_query($query) or die('Error, query failed');
list($content,$filename,$size,$ext) = mysql_fetch_array($result);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=$filename.$ext");
header("Content-Transfer-Encoding: binary");
hea开发者_开发知识库der("Content-Length: ".$size);
echo $content;
exit;
the problem is that in the downloaded file i have a lot of "\0" that came from nowhere. the file is well stored in the database. i tested that. thank you
It sounds like an encoding issue. It could be that the file is encoded in UTF-16 and you are displaying it as if it were ASCII.
Try to:
header("Content-Disposition: attachment; filename = $filename . '.' . $ext");
instead of:
header("Content-Disposition: attachment; filename=$filename.$ext");
3 things :
- As Alexander.Plutov said, you got an error in the filename, it should be $filename.'.'.$ext You're missing the "." between the filename and its extension.
- As Mark Byers said, it may seem like an encoding issue, you should check your PHP file encoding as well as your DB's one.
- Hope you're only stocking text based file, because, any other file content definitely shouldn't be stored in a database! And even for text based file, it shouldn't be used for that, it's not its purpose at all.
In any other case than file generation, files should be stored in your server and may be called from your DB thanks to file path and name.
精彩评论