IE (HTTPS): generating pdf from php file doesn't work - php

Here is my issue. I am trying to call a page: foo.php?docID=bar and return a PDF to the screen which is stored as a BLOB in the DB.
Here is the portion of my code which actually returns the PDF:
$docID = isset($_REQUEST['docID']) ? $_REQUEST['docID'] : null;
if ($docID == null){
die("Document ID was not given.");
}
$results = getDocumentResults($docID);
if (verifyUser($user, $results['ProductId'])){
header('Content-type: application/pdf');
// this is the BLOB data from the results.
print $results[1];
}
else{
die('You are not allowed to view this document.');
}
This works perfectly fine in Firefox.
However, in IE, it doesn't show anything at all. If i'm on another page (i.e. google.com), and I type in the URL to go to this page, it will say it's done, but I will still have google.com on my screen.
I checked the headers for the responses from both firefox and IE. They are identical.
Does anyone have any suggestions? Need more information?
EDIT: If it helps at all, here's the response header and the first line of the content:
HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 349930
Content-Type: application/pdf
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: PHP/5.1.2
Set-Cookie: PHPSESSID=cql3n3oc13crv3r46h2q04dvq4; path=/; domain=.example.com
Content-Disposition: inline; filename='downloadedFile.pdf'
X-Powered-By: ASP.NET
Date: Tue, 21 Apr 2009 16:35:59 GMT
%PDF-1.4
EDIT: Also, the page which pulls out the pdf file actually uses HTTPS instead of HTTP.
Thanks in advance,
~Zack

I figured out what the issue was. It's an IE bug dealing with IE, HTTPS and addons. (See here)
It was a caching issue. When I set:
header("Cache-Control: max-age=1");
header("Pragma: public");
(see here), the PDF was in cache long enough for the adobe reader add-on to grab it.

I had this issue too, i used the following which seems to work fine
header("Content-type: application/pdf");
header("Content-Length: $length");
header("Content-Disposition: inline; filename='$filename'");

Try this:
header("Content-Type: application/pdf");
header("Content-Disposition: inline; filename=foo.pdf");
header("Accept-Ranges: bytes");
header("Content-Length: $len");
header("Expires: 0");
header("Cache-Control: private");
Also, if you are using sessions, you can try setting
session_cache_limiter("none");
or
session_cache_limiter("private");

if ( USR_BROWSER_AGENT == 'IE' ) {
header( 'Content-Disposition: inline; filename="' . $name . '"');
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
} else {
header( 'Content-Disposition: attachment; filename="' . $name . '"' );
header( 'Expires: 0' );
header( 'Pragma: no-cache' );
}

This was the only header I needed to change:
header("Pragma: public");

I think you need to add more headers.
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=THEFILENAME.pdf;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($results[1]));

Related

PHP readfile not working correctly with Safari

I am loading an mp3 into an html <audio> tag. The source of that tag is really a php script which returns a song that is not hosted in the public directory.
Omitting most of validation and other code, the headers used to output the mp3 are:
header( 'Content-type: {$mime_type}' );
header( 'Content-length: ' . filesize( $file ));
header( 'Content-Disposition: inline;filename="'.$filename.'"' );
header( 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: no-cache' );
header( 'Content-Transfer-Encoding: binary');
header( 'Expires: 0');
readfile( $file );
This works on Firefox, Chrome (and mobile), Edge (and mobile) and Opera. However I can't seem to get Safari to use the generated url as an audio source. In fact, no audio source appears in the DOM after the page has rendered:
The url might look like, www.website.com/song.php?sid=234234234234
I have tried tweaking this file plenty of times but can't see to figure out why this is happening on Safari. My intuition is the way the browser deals with mp3s and the like, which should be dealt with in the header.
Any guidance, hints or help would be appreciated.
Thanks.
update:
Added the network, shows that it loads the mp3 but then the second request doesn't?
response headers:
Name Value
Server Apache
Content-Type audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3
Date Wed, 28 Dec 2016 17:24:48 GMT
Cache-Control no-cache
X-Powered-By PHP/5.3.29
Content-Disposition inline;filename="148_2793d1c49976a3689147634359577ec1aa5619f1.mp3"
Content-Length 834312
Expires 0
Connection Keep-Alive
Content-Transfer-Encoding binary
Accept-Ranges bytes
Keep-Alive timeout=5, max=100
Pragma no-cache
You need to pass the Multiple ranges header, because it's a partial content: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
header("Accept-Ranges: 0-filesize( $file )");
EDIT
Take a look here:
http://www.techstruggles.com/mp3-streaming-for-apple-iphone-with-php-readfile-file_get_contents-fail/
You need to define some headers for safari, even for the mobile version.
I had this issue sometime ago, sorry if I don't remember correctly how I fixed that, but is a header issue (its the main part I can remember, and I don't use comments for adding the answer because its too long)
Try with this..
header( 'Accept-Ranges: bytes');
header("Pragma: public");
header("Expires: 0");
header('Cache-Control: no-cache, no-store');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: inline; filename="'.$filename.'"');
header('Content-Length: '.$fsize);
header('Content-Type: audio/'.$t);
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Content-Range: bytes 0-'.$shortlen.'/'.$fsize);
header('X-Pad: avoid browser bug');
header('Etag: '.$etag);
Where:
$filename = "myaudio.mp3";
$path = 'music/'.$filename;
$fsize = filesize($path);
$shortlen = $fsize - 1;
$fp = fopen($path, 'r');
$etag = md5(serialize(fstat($fp)));
fclose($fp);
$t = "mpeg";
Hope this helps, but the 2 main headers you need is "Etag" and "Accept-Ranges" if I'm not wrong.

How to load mp3 into JW Player using php

I'm trying to load an mp3 into JW Player 5 using php to retrieve the actual file. This is the javascript code for the player:
jwplayer('mediaplayer').setup({
'flashplayer': 'player.swf',
'id': 'playerID',
'type': 'mp3',
'width': '600',
'height': '49',
'file': '/get_mp3/<?php echo $filename; ?>',
});
The file attribute has the URL to the PHP function (I use CakepHP, the part after the last / is the variable that the function gets passed).
This is the PHP function:
function get_mp3($filename) {
$file_path = '/path/to/files/' .$filename. '.mp3';
header("pragma : no-cache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: audio/mpeg3");
header("Content-Disposition: inline; filename=" .$filename. ".mp3");
header("Content-Location: " .$filename. ".mp3");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file_path));
readfile($file_path);
}
If I call that function via web browser (for example mydomain.com/get_mp3/test_file) it prompts to download the right file, which seems to show that the PHP code is working.
However, when I use it on JW PLayer's file attribute it doesn't load anything or shows any kind of error.
I've tried adapting what's shown in this SO question but I couldn't make it work, I don't know if it's because for video it's different or because that mentoins JW PLayer 6 and mine is 5.
EDIT: test if it's an issue with CakePHP
So test if the issue is that for some reason CakePHP isn't working with JW Player I've put the following PHP code in an external PHP file:
$filename= 'my_filename';
$file = $_SERVER['DOCUMENT_ROOT']. '/path/to/files/' .$filename. '.mp3';
header("pragma : no-cache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: audio/x-mp3");
header("Content-Disposition: inline; filename=" .$filename. ".mp3");
header("Content-Location: " .$filename. ".mp3");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
So now, the file line in the JW Player declaration reads like:
'file': '/get_mp3.php',
With that it still doesn't work. If I access the get_mp3.php page directly I get prompted to download the file, so again looks like the PHP code works...
EDIT 2
SO I've found the culprit of the issue: the path of the audio file. If I put the file in the root folder of the website, and I use the file path variable like $file_path = $filename. '.mp3'; it works fine.
The problem is that the audio files are in a different folder in the same server, and I can't move them... How can I change the PHP script to find the files in their current path? I've already tried with $file = '/path/to/files/' .$filename. '.mp3'; and $file = $_SERVER['DOCUMENT_ROOT']. '/path/to/files/' .$filename. '.mp3'; but it doesn't work...
Change your content type to ("Content-Type: audio/x-mp3");
So it turns out my problem was in the path to the file in the PHP script. I was using $_SERVER['DOCUMENT_ROOT'] and just / for the paths and it wasn't working, but then I realized that moving the file to the same folder as the script would work. In my case I still needed to find a way to access those files from a different folder, and using a relative path did the trick. Something like:
$filename= 'my_filename';
$file = '../../../path/to/files/' .$filename. '.mp3';
header("pragma : no-cache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: audio/x-mp3");
header("Content-Disposition: inline; filename=" .$filename. ".mp3");
header("Content-Location: " .$filename. ".mp3");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
Since you are using JW5.
Under:
'file': '/get_mp3/<?php echo $filename; ?>',
Add:
'provider': 'sound',

download file with php

i want to create a link to download an excel file from the root in server computer, using php. so i wrote a simple code as below,
<?php
header("Content-disposition: attachment; filename=example.xls");
header("Content-type: application/vnd.ms-excel");
readfile("example.xls");
?>
it then can be downloaded however when i want to open it, i got the error saying the file i downloaded is in a different format than specified by the file extension. i also tried the same method with jpeg file and didnt get the same error but when i click it, it shows nothing. can someone help me? im not very good with programming. thank you in advance!
Try this
$file='example.xls'; $filesize=filesize($file);
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: inline; filename="'.basename($file).'"');
header("Content-Length: " . $filesize);
$fh = fopen("$file, "rb");
// output file
while(!feof($fh))
{
# output file without bandwidth limiting
print(fread($fh, filesize($file)));
}
fclose($fh);

PHPExcel can't save the file or download it

I'm trying to code a function in PHP to export some data to Excel file.
The problem is if I save it to the server it does work, but if I try to send to the browser using php://output it just doesn't work. It doesn't even show the download window. I' ve been getting these as response:
PK����a�B%���a��������[Content_Types].xml͔]K�0���%��f�
"�v��R���kX����׿�m��+����4�<�'��2�jgs6�,+��v����Sz���a�����tr5^�=Bb�9+c����,��9��.T"�kXr/�J,���[.��`ck6�?h�\��,���ܠ}3�c�C+��9�-E��|c�j�BKPN�+�d��u��O1�
o��Ba +���G�
The headers are:
Response Headers
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection Keep-Alive
Content-Disposition attachment;filename="Report.xlsx"
Content-Type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date Wed, 29 May 2013 10:08:10 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive timeout=15, max=78
Last-Modified Wed, 29 May 2013 10:08:11 GMT
Pragma no-cache
Server Apache/2.2.14 (Ubuntu)
Transfer-Encoding chunked
X-Powered-By PHP/5.3.2-1ubuntu4.19
Request Headers
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cookie PHPSESSID=075r4aaqrvcnbca5sshjvm0jq7; 87293ba76812d31889be0901b086dd73=5h4jriq5c7r9vdt3m2u2u9up43; d82d00149fafbe651c9ba75a9804bbc9=en-GB
Host 150.145.139.3:8889
Referer
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With XMLHttpRequest
Here's my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/Rome');
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';
$target ='templates/';
$fileType = 'Excel2007';
$InputFileName = $target.'richiesta.xlsx';
$OutputFileName = $target .'Richiesta_'.$_SESSION['User'].'_'.$_SESSION['Last'].'_'.$dat.'.xlsx';
//Read the file (including chart template)
$objReader = PHPExcel_IOFactory::createReader($fileType);
//$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($InputFileName);
//Change the file
$objPHPExcel->setActiveSheetIndex(0)
// Add data
->setCellValue('C3','10' )
->setCellValue('C4','20' )
->setCellValue('C5','30')
->setCellValue('C5','40' );
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
//$objWriter->save($OutputFileName); //This one WORKS FINE!!!
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Report.xlsx"');
$objWriter->save('php://output'); //NOT WORKING :-(
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
exit;
I'm getting depressed about this problem once I have to finish the project this week.
I really appreciate any kind of help!
That's just from my code, as a hint, the problem seems to be with the Content-Type HTTP header:
if (strtolower($type) == 'excel2003') {
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $outFileName . '"');
header('Cache-Control: max-age=0');
} else {
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
header('Content-Type: application/xlsx');
header('Content-Disposition: attachment;filename="' . $outFileName . '"');
header('Cache-Control: max-age=0');
}
Try using these header() calls:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=Report.xlsx");
header("Content-Transfer-Encoding: binary ");
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = '1111';
ob_end_clean();
header("Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet");
header('Content-Disposition:attachment;filename="'.$filename.'.xlsx"');
header("Cache-Control: max-age=0");
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit();
I had the same problem, and it seems that a download must be opened in a different tab instead of just downloading in the same tab. Meaning you need to redirect into a new tab before initializing the download.

Changing CRC of a file through readfile()

I have this code set up that lets a user download a file through my server from a URL they specify. The file streams through using readfile() so it only uses my bandwidth.
<?php
set_time_limit(0);
$urlParts = explode("/", $_SERVER['PHP_SELF']);
$file = $urlParts[3];
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . $file);
header("Content-Transfer-Encoding: binary\n");
readfile($file);
?>
This script works, but it does not change the CRC hash of the downloaded file. What I want it to do is append some random bits to the end of the file so it can change the hash without corrupting it. I have tried adding something like echo md5(rand() . time()); to the end of the script but it doesn't work.
If this is possible with something like cURL I'd appreciate if someone could put up some code samples, because i'd switch to cURL if this was possible.
Thanks for your help.
Hmm, your code works for me:
test.php:
set_time_limit(0);
$urlParts = explode("/", $_SERVER['PHP_SELF']);
//$file = $urlParts[3];
$file = 'toread.txt';
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . $file);
header("Content-Transfer-Encoding: binary\n");
readfile($file);
echo md5(rand() . time());
?>
toread.txt:
This is the content of toread.txt
Now using curl, I get the following results:
>curl -i http://example.com/test.php
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 07:09:39 GMT
Server: Apache
Cache-Control: public, must-revalidate
Pragma: hack
Content-Disposition: attachment; filename=toread.txt
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Type: application/force-download
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0
This is the content of toread.txt38d8a8009fad7315bdf5e823a06018e7
And the second one:
>curl -i http://example.com/test.php
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 07:09:57 GMT
Server: Apache
Cache-Control: public, must-revalidate
Pragma: hack
Content-Disposition: attachment; filename=toread.txt
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Type: application/force-download
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0
This is the content of toread.txt3b87356ea9ee007b70cfd619e31da950

Categories