tags:

views:

24

answers:

3

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");
header("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

A: 

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.

Mark Byers
so, how i can rezolve this issue?
@user273386: First you need to figure out a) in what encoding the file is stored in the database and b) in what encoding you are displaying it. If those two are different then you need a conversion function. See for example here: http://stackoverflow.com/questions/1196838/php-utf-16-to-ascii-conversion
Mark Byers
A: 

Try to:

header("Content-Disposition: attachment; filename = $filename . '.' . $ext");

instead of:

header("Content-Disposition: attachment; filename=$filename.$ext");
Alexander.Plutov
A: 

3 things :

  1. 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.
  2. 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.
  3. 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.

Chouchenos