Jump to content

Cant "download" some base64 encoded file


culhy
Go to solution Solved by culhy,

Recommended Posts

Hi,

i have problem on my site. Im trying to make simple file manager.

 

Uploading script looks like:

$data = file_get_contents($_FILES["file"]["tmp_name"]);
$data = base64_encode($data);
$type = $_FILES["file"]["type"];
$name= $_FILES["file"]["name"];

... INSERT INTO ....

On the other side is output script:

...SELECT...
$a = mysql_fetch_assoc($q);
header( 'Content-Type: '.$a["type"]);
header( 'Content-Disposition: attachment; filename='.$a["name"]);
echo base64_decode($a["data"]);
exit;

I can actualy download .pdf or .php but i cant download .jpg, .rar, .docx etc

 

I tryed like bilion ways how to edit output script, this is simplest version i have so far.

All files r in utf8 w/o BOM.

I allready used modifications like:

header('Content-Transfer-Encoding: base64'); 
echo $a["data"];

didnt worked...

i know that data in db r correct, this actualy work:

<img alt="<?php echo $a["name"]; ?>" src="data:image/jpeg;base64,<?php echo $a["data"]; ?>" />

So, why some files cant be downloaded this way?

Link to comment
Share on other sites

Its in mysql db.

 

 

Then, it should be something like that:

 $a = mysql_fetch_assoc($q);

        header('Content-Description: File Transfer');
        header("Content-type: ".$a['type']);
        header('Content-Disposition: attachment; filename='.  basename($a['name']));  
        header('Content-Length: ' . filesize($a['length']));
        readfile($a['data']);

Be careful about " header('Content-Length: ' . filesize($a['length']));" if you don't have information about the length of the file in your database, you can use the sql length function to grab the size of this binary file. So, the query string should similar like:

$sql = "SELECT `data`, `name`, `type`, length(`data`) as length FROM `db_name`.`tbl_name`  WHERE image_id=".$_GET['image_id'];
Edited by jazzman1
Link to comment
Share on other sites

ok, i made adjustments and it dont work.

 

using

readfile() 

causes that no filetype works.

and if i use 

header( 'Content-Length: ' . filesize($a['length']));

same result.

 

here is my full scrpt, it works for pdf and php files, dont work for any image or rar etc

if(isset($_GET["id"])){
  $id = $_GET["id"];
  $q = db_q("SELECT `data`, `nazev`, `typ`, length(`data`) as length FROM `mailing_files` WHERE `soubor_id` = '$id'", "databaze-kontaktucom_dataserver");
  }
else if(isset($_GET["nazev"])){
  $nazev = $_GET["nazev"];
  $q = db_q("SELECT `data`, `nazev`, `typ`, length(`data`) as length FROM `mailing_files` WHERE `nazev` = '$nazev'", "databaze-kontaktucom_dataserver");
  }


if(mysql_num_rows($q) == 1){
  $a = mysql_fetch_assoc($q);


  header( 'Content-Description: File Transfer'); // no effect but dont causes error
  header( 'Content-Type: '.$a["typ"]);
  //header('Content-Type: application/octet-stream'); // no effect
  header( 'Content-Disposition: attachment; filename='.$a["nazev"]);
  //header( 'Content-Length: ' . filesize($a['length'])); // causes error


  //header('Pragma: public'); // no effect
  //header('Expires: 0'); // no effect
  //header('Content-Transfer-Encoding: binary'); // no effect
  //header('Content-Transfer-Encoding: base64');  // no effect
  //ob_clean(); // no effect
  //flush(); // no effect

//readfile($a['data']); // causes error

echo base64_decode($a["data"]); // works best so far

//echo $a["data"]; // just print  :)

}

exit;

 
 
 

 

Link to comment
Share on other sites

btw file fata r good.

most important for me is attaching files to email and it works (like this):

$mail->AddStringAttachment(
  base64_decode($a['data']),
  $a['nazev'],
  'base64',
  $a['typ']
  );

files from email r correct so problem is definitly somwhere here

Link to comment
Share on other sites

So, the best solution for me is, when you get a data (binary file) from a database to save it on the real server, then zip(compress) the file and attach it to email.

I am working right now, but I will try to provide you a solution later on. 

Edited by jazzman1
Link to comment
Share on other sites

  • Solution

SOLVED:

 

I rechecked system (like 200 files) and found one that was saved with BOM so there were 1 empty line.

that doesnt matter in php or pdf files but its fatal in all others :)

 

thx for advice.

 

this is sufficient for downloading:

$a = mysql_fetch_assoc($q);
header( 'Content-Description: File Transfer');
header( 'Content-Type: '.$a["type"]);
header( 'Content-Disposition: attachment; filename='.$a["name"]);
echo base64_decode($a["data"]); 
Link to comment
Share on other sites

Um....that solution doesn't work for me! Check if the file size is correct. I still think you should have to use Content-Length, but gives me an error if the file comes from a database as binary string. 

I will play around that later on because this is interesting topic for me ;)

Link to comment
Share on other sites

Um....that solution doesn't work for me! Check if the file size is correct. I still think you should have to use Content-Length, but gives me an error if the file comes from a database as binary string. 

I will play around that later on because this is interesting topic for me ;)

The Content-Length would have to be the length of the data being sent. Since he is decoding the base64 data from the database, the Content-Length, is not the length of the encoded data. So taking the length of the data in the database will be too high. You would have to do something like:

$out = base64_decode($a['data']);
header('Content-Length: ' . str_len($out));
echo $out;
Note: I personally do not recommend storing files in a database. I would put the file in the filesystem (with a unique name), and store a path to the file in the database. Databases are designed for storing, manipulating and retrieving (structured) data.
Link to comment
Share on other sites

 

Note: I personally do not recommend storing files in a database. I would put the file in the filesystem (with a unique name), and store a path to the file in the database. Databases are designed for storing, manipulating and retrieving (structured) data.

 

Agreed! Store all images in the filesystem and let the pain go away :)  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.