Download PDF from Binary SQL SERVER With PHP - php

I need help with this script. I'm trying to get this binary file stored in a sql server DB . The main problem is that , each time im trying to show it in my browser or to download it, the file is corrupted . Heres my code:
$binary = $row['PDF_FILE_STORED'];
file_put_contents('my.pdf', $binary);
header('Content-type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($binary));
header("Content-Disposition: attachment;filename=my.pdf");
ob_clean();
flush();
echo $binary;
Is there a problem with the encoding aspect? I just got this warning in my brower's console : "Resource interpreted as Document but transferred with MIME type application/pdf" . Any advices ?

To just download the pdf file, you don't need to save it locally on the server. You don't need to send content-length as that should be done automatically and I would skip the content-transfer-encoding as well.
If there was no output yet, you can also skip dealing with the output buffer.
Try this:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=my.pdf');
echo $row['PDF_FILE_STORED'];

Related

Return .mp3 from a separate server as audio file, not raw text

I am developing a very humble web app which routes all the requests to get an audio file toward a third party server, where the actual files are stored.
In order to do this, I am using the following statement in my PHP code:
echo file_get_contents('https://3rdpartyserver.com/' . $filename);
By testing it, it seems to work properly only for text files. While trying to get an .mp3 file, instead, its content as text is actually displayed (instead of the default HTML audio player, which is displayed if I connect directly to the third party server).
I have also tried to add some headers to the response:
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
But the resulting behavior is still not what I am looking for.
Might you please suggest me how to solve this issue?
You need to set the Content-Type to audio/mpeg, then write the file to the output buffer.
header("Content-Type: audio/mpeg");
header('Content-Disposition: attachment; filename=' . $filename);
readfile('https://3rdpartyserver.com/' . $filename);
exit;

Php File Size header()

$file_name = $_GET['name'];
$file_url = $_GET['file-url'] . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
I'm using this code to download files in my site fetching from another web servers.
It works if my url looks like this:-
https://www.example.com/file_download.php?name=video_song.mp4&file-url=http://www.googlevideo.com/video/play/221589
So, it starts downloading by fetching the file from http://www.googlevideo.com/video/play/221589 in my site. Now, though it downloads the file correctly, it does allow the downloader to see the actual size of the file. So, downloaders having problems with it (e.g. Time Remaining, Download Percentage etc.).
So what header should I use to solve this thing? Please explain it by coding.
You may try this:
header("Content-Length: " . $filesize);

pdf php download produces empty file

<?php
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
readfile('http://mysite.com/Booking.pdf');
?>
why is the Booking.pdf file downloaded empty!??
mac and windows both say:
The file “Booking.pdf” could not be opened because it is empty.
checked google and stackoverflow, can't find relative info... has anyone experienced this before?
ps: I only found this forum post:'The online issue is a bit off topic I think, but is generally due to loading the PDF to a server in the ASCII mode of FTP rather than binary. That creates a corrupt file. Be sure to turn on binary transmission', but this is not true in this case as i can display the same pdf file in an iframe and it is not blank/empty.
You need to change
readfile('http://mysite.com/Booking.pdf');
To
readfile(__DIR__ . '/Booking.pdf');
Example
$file = __DIR__ . '/test.pdf' ;
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
header("Content-length: ".filesize($file));
readfile($file);

file_get_contents over https displays garbage

I am trying to download uploaded files over https and, while the files themselves download, they cannot be viewed.
I have tried JPG, DOC and XLS files and all give the same problem and, in all cases, if I download via FTP they open perfectly and they open fine in the browser pre-download using the script.
Here is a subset of the script showing the code I am trying to use? Any idea why it downloads garbage?
$_file = sanitiseData($_GET['doc']);
$filename = '/doc_uploads/'.$_file;
if (file_exists($filename)) {
header('Content-type:image/jpg');
header('Content-Disposition: attachment; filename="'.$_file.'"');
echo file_get_contents($filename);
} else {
echo "The file $_file does not exist";
}
Here is a sample of the garbage when trying to view a downloaded JPG via browser:
����JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 90 ��C ��C ��R�"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?�P��q\�O�^�-�C�z�z����o�N��P;��.i�~k+Զ���|�7`�'e����G�>+���_�6�%�Ԓ��Y�w���P�~.�����2E�� ��"��ڗȌ��ms����[���?��%|"�R5�s�c������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=?V��>���IG�=?V��>���I_Q#w����o���������o����������=GU��>���� �N�v������%|!E~�xO� �ỹx_P����j(�z����_
Your best bet is to use readfile(...). PHP's website has a nice example that should help you. I use it on my website and it works like a charm:
if (file_exists($file)) {
// Inform browser that this is a force-download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
// Inform browser that data can be binary in addition to text
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
// Inform browser that this page expires immediately so that an update to the file will still work.
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
// Push actual file.
ob_clean();
flush();
readfile($file);
exit();
}
Single quotes are causing you to output the string "$filename" instead of the variable value $filename.
echo file_get_contents($filename);
Though we don't see the function sanitiseData(), we assume it is properly filtering out strings that could be used for path injection like ../.
Addendum:
I'll note that the correct MIME type for a jpeg is image/jpeg, rather than image/jpg. That is likely going to cause you problems too.

headers to force the download of a tar archive

i have a tar archive on the server that must be downloadable through php. This is the code that i've used:
$content=file_get_contents($tar);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$tar");
header("Content-Length: ".strlen($content));
unlink($name);
die($content);
The file is downloaded but it's broken and it can't be open. I think that there's something wrong with headers because the file on the server can be open without problems. Do you know how can i solve this problem?
UPDATE
I've tried to print an iframe like this:
<iframe src="<?php echo $tar?>"></iframe>
And the download works, so i'm sure that there's something missing in headers.
I have used this code when I have had to do it:
function _Download($f_location, $f_name){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($f_location));
header('Content-Disposition: attachment; filename=' . basename($f_name));
readfile($f_location);
}
_Download("../directory/to/tar/raj.tar", "raj.tar");
//or
_Download("/var/www/vhost/domain.com/httpdocs/directory/to/tar/raj.tar", "raj.tar");
Try that.
Don't use file_get_contents() and then echo or print to output the file. That loads the full contents of the file into memory. A large file can/will exceed your script's memory_limit and kill the script.
For dumping a file's contents to the client, it's best to use readfile() - it will properly slurp up file chunks and spit them out at the client without exceeding available memory. Just remember to turn off output buffering before you do so, otherwise you're essentially just doing file_get_contents() again
So, you end up with this:
$tar = 'somefile.tar';
$tar_path = '/the/full/path/to/where/the/file/is' . $tar;
$size = filesize($tar_path);
header("Content-Type: application/x-tar");
header("Content-Disposition: attachment; filename='".$tar."'");
header("Content-Length: $size");
header("Content-Transfer-Encoding: binary");
readfile($tar_path);
If your tar file is actually gzipped, then use "application/x-gtar" instead.
If the file still comes out corrupted after download, do some checking on the client side:
Is the downloaded file 0 bytes, but the download process seemed to take much longer than it would take for 0 bytes to transfer, then it's something client-side preventing the download. Virus scanner? Trojan?
Is the downloaded file partially present, but smaller than the original? Something killed the transfer prematurely. Overeager firewall? Download manager having a bad day? Output buffering active on the server and the last buffer bucket not being flushed properly?
Is the downloaded file the same size as the original? Do an md5/sha1/crc checksum on both copies. If those are the same, then something's wrong with the app opening the file, not the file itself
Is the downloaded file bigger than the original? Open the file in notepad (or something better like notepad++ which doesn't take years to open big fils) and see if any PHP warnings messages, or some invisible whitespace you can't see in your script got inserted into the download at the start or end of the file.
Try something like the following:
$s_filePath = 'somefile.ext';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. s_filePath.'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Content-Length: ".filesize($s_filePath));
$r_fh = fopen($s_filePath,'r');
while(feof($r_fh) === false) {
$s_part = fread($r_fh,10240);
echo $s_part;
}
fclose($r_fh);
exit();
Use Content-Type: application/octet-stream or Content-Type: application/x-gtar
Make sure you aren't echoing anything that isn't the file output. Call ob_clean() before the headers

Categories