culhy Posted August 20, 2013 Share Posted August 20, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/ Share on other sites More sharing options...
jazzman1 Posted August 21, 2013 Share Posted August 21, 2013 Where did you save the images on the server or database? And why, are you using base64 if the images are saved on the server? Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446048 Share on other sites More sharing options...
culhy Posted August 21, 2013 Author Share Posted August 21, 2013 Its in mysql db. Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446070 Share on other sites More sharing options...
jazzman1 Posted August 21, 2013 Share Posted August 21, 2013 (edited) 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 August 21, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446123 Share on other sites More sharing options...
jazzman1 Posted August 21, 2013 Share Posted August 21, 2013 No, there is something wrong when the image comes from the database. I should make some tests. Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446127 Share on other sites More sharing options...
culhy Posted August 21, 2013 Author Share Posted August 21, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446144 Share on other sites More sharing options...
culhy Posted August 21, 2013 Author Share Posted August 21, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446148 Share on other sites More sharing options...
jazzman1 Posted August 21, 2013 Share Posted August 21, 2013 (edited) 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 August 21, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446154 Share on other sites More sharing options...
Solution culhy Posted August 21, 2013 Author Solution Share Posted August 21, 2013 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"]); Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446162 Share on other sites More sharing options...
jazzman1 Posted August 21, 2013 Share Posted August 21, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446163 Share on other sites More sharing options...
DavidAM Posted August 21, 2013 Share Posted August 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446186 Share on other sites More sharing options...
jazzman1 Posted August 22, 2013 Share Posted August 22, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281408-cant-download-some-base64-encoded-file/#findComment-1446207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.