0

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
user273386
  • 35
  • 6
  • Sorry but I don't think that a RDB is the right place to store files at all. But this is just my opinion based on my experience. – Yves M. Sep 28 '10 at 07:37
  • can you give me some reasons? please. it will be helpful – user273386 Sep 28 '10 at 07:43
  • RDBs are for storing relatively small fields of data that you want to index or search or perform other operations on. It is not set up for bulk data of variable length; a filesystem is much better with those. IIRC databases have a BLOB type that can handle such things, but their performance is generally much worse than for normal fields because their storage cannot be optimized. BLOBs are really for things like avatars. For your application, a database would usually be designed to store the path to the file in the record, and then you would have a separate filesystem for the actual files. – Mike DeSimone Sep 28 '10 at 07:49

3 Answers3

0

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
  • 811,555
  • 193
  • 1,581
  • 1,452
  • so, how i can rezolve this issue? – user273386 Sep 28 '10 at 07:42
  • @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 Sep 28 '10 at 08:08
0

Try to:

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

instead of:

header("Content-Disposition: attachment; filename=$filename.$ext");
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
0

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.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75