test.php code:
$path = 'audio.mp3';
header("Content-type: audio/mpeg");
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: inline; filename="'.$path.'"');
header("Content-length: ".filesize($path));
readfile($path);
html code:
<iframe src="test.php"></iframe>
this will play the .php as a .mp3, but i will not be able to access the navigation slider in the browser media player. i click on the slider in several different places, but nothing will happen. when i change the source to "audio.mp3" i am able to control the slider again. im pretty sure that it has something to do with the headers. any idea what headers i can use to fix this?
It actually has everything to do with what browser and plugin you are using, "streaming" mp3 or media files for that matter should not be done this way. Use any of the many open source and easy to use Flash players, they handle buffering, controls, display/hidden, and everything for you, for a mp3 file on your server.
Related
I have a script that merges 2 documents and then it shows a final document in a browser.
It works in Firefox, Opera and IE. But it does not work in Chrome.
Chrome only shows loading and it stops in 1/4 of loading.
The code:
exec("pdftk A=$pdfin B=$tmpfname cat B1 A output $tmpfoutput");
$data = file_get_contents($tmpfoutput);
header("Content-type: application/pdf");
header("Content-disposition: inline;filename=GeneratedPdf.pdf");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($tmpfoutput));
header('Accept-Ranges: bytes');
echo $data;
I have been told that it also worked in Chrome before so I think there will be only a problem with the last version.
Thanks for any help.
By appending Accept-Ranges: bytes, your script tells the browser that it will accept range requests, i.e. multiple requests that request a part of the response. Your script obviously does not support range requests because it generates and provides the data at once.
To fix the error, remove header('Accept-Ranges: bytes');
If your PDF files are usually large, then a more user-friendly solution is to actually implement range requests in your script. Odds are that your server does already have an efficient routine that handles range requests, so a smart choice is to save PDF file to a publicly accessible directory, then 302-redirect the request to this URL after the PDF has been generated. Make sure that the URLs are unguessable, e.g. by using UUIDs. And remove the PDF files at some point, e.g. using a cronjob.
I'm trying to make a simple script that does two things:
Serves up a file and hide's it's destination
Has a download counter
Now, I'm doing this in the wordpress environment, but this question isn't completely wordpress-related so I figured I would ask here.
Basically, the way I have it set up, currently, is I have a link that when you click it sets a $_['GET'] which is then checked if is set. If it is set, the download file is served.
the link: Click here!'
the $_['GET'] code: http://pastebin.com/93nD43gA
There is a bit of wordpress jargon in the code, but basically it's checking a download count user_meta and if it's > 0, serveFile() is called.
The main problem I'm having here is, if I click the link, readfile() loads the actual file contents INTO the window (garbled text). If I add a target=_blank to the <a> it opens a new browser window and loads the contents INTO the window.
This approach seemed to work perfectly fine when I was doing it as stand-alone php files. My main issue is that I need to keep the wordpress space so I can call functions, etc. associated with it.
I have tried using the $_['GET'] on both the self page, another page with a custom template (the code in the pastebin above), and as a stand-alone php file. Both the first two options load the file INTO the window. The third doesn't preserve wordpress functions, even if I include blog-header.php.
Can anyone point me in to the right direction of how to get the file to force download and not load INTO the window?
You need to set the appropriate header for whatever the file type is. For example, if readfile always serves, PDFs, it should be done like this:
// disable browser caching -- the server may be doing this on its own
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/pdf');
//forces a download
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=filename.pdf');
readfile($file);
Keep in mind that header only works if you have not sent any data in the request at all including whitespace.
The 'garbled' text is what you want however besides that you have to set a mime. This can be accomplished by simply setting a header, e.g. header("Content-Type: image/png");
If the file mimes will vary (e.g. pdf, doc, png, etc) you should look into finfo extension. With it you can get the full and correct mime of the file
<?php
$finfo = new \finfo(FILEINFO_MIME);
$mime = $finfo->file('path/to/file', FILEINFO_MIME_TYPE);
header("Content-Type: $mime");
As noted - headers can be set only if no write to output has been done (no echo's, print, etc. Output buffering could help you here).
I am using php to show the pdf
#readfile($actualfilename);
header('Content-type:pdf');
header('Content-Disposition: inline');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
the problem is on the tittle bar it shows
and when I use
header('Content-type:application/pdf');
it prompts me to download the file and the same problem appears when I use
header('Content-Disposition: inline; filename='.$fakefilename.'');
This code shows pdf file in only firefox. I.E,chrome prompts me to download file?
Are you trying to change the browser's title bar text (or wherever it shows the file name)?
If so, you're out of luck here, because that won't work as it's up to the browser to decide how/where (if at all) show the file name.
However, there's some possible workaround: You could use server side tools, such as mod_rewrite in an Apache environment to redirect a request like download/readme.pdf behind the scenes to readfile.php?file=readme.pdf. In this case the browser won't know about the hidden rewrite and it will in fact display readme.pdf as the file name (even if the real file name or the script's name on the server side are different).
I have links in my web page that point to PDFs, that are in my server.
I don't want want pdfs to be viewed in the browser bur directly downlodable when a user click on the link
just set the right header info. PHP file should look something like this.
<?
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('huge_document.pdf');
?>
as depicted here
http://webdesign.about.com/od/php/ht/force_download.htm
Regards
I want to show my users PDF files. The reason why I use CGI to show the PDF is I want to track the clicks for the PDF, and cloak the real location of the saved PDF.
I've been searching on the Internet and only found how to show save dialog to the users and creating a PDF, not show the files to the users.
What I wanted for is show the users my PDF files, not creating or download the PDF.
Here is what I got form the official PHP documentation:
<?php
header('Content-type: application/pdf');
readfile('the.pdf');
?>
Also my google-search-result perl code:
open(PDF, "the.pdf") or die "could not open PDF [$!]";
binmode PDF;
my $output = do { local $/; <PDF> };
close (PDF);
print "Content-Type: application/pdf\n";
print "Content-Length: " .length($output) . "\n\n";
print $output
if you do it on ruby, please say it to me. But I'm not sure if my server support rails.
Sorry if my code is too far away from the method to show the pdf, since I don't know anything about pdf processing and how to implement this problem.
Lets assume that the users have the Adobe Reader plug-in. So, how to fix my problem?
edit : I want to show plain PDF file. My primary purpose: track my pdf files and use some fancy urls.
edit : Here's my main php code:
<?php
$file='/files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
#readfile($file);
?>
edit : Now the code is working. But the loading progress bar (on Adobe Reader X plugin) doesn't shows up. Why? Anyone can help me? Here's my main code:
<?php
$file='./files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
#readfile($file);
?>
edit : All my problems solved. Here's the final code:
<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
Thanks! :)
I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline.
Also remember that this will also be affected by browser settings - some browsers may be configured to always download PDF files or open them in a different application (e.g. Adobe Reader)
$url ="https://yourFile.pdf";
$content = file_get_contents($url);
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($content));
header('Content-Disposition: inline; filename="YourFileName.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
die($content);
Tested and works fine. If you want the file to download instead, replace
Content-Disposition: inline
with
Content-Disposition: attachment
You could modify a PDF renderer such as xpdf or evince to render into a graphics image on your server, and then deliver the image to the user. This is how Google's quick view of PDF files works, they render it locally, then deliver images to the user. No downloaded PDF file, and the source is pretty well obscured. :)
The safest way to have a PDF display instead of download seems to be embedding it using an object or iframe element. There are also 3rd party solutions like Google's PDF viewer.
See Best Way to Embed PDF in HTML for an overview.
There's also DoPDF, a Java based In-browser PDF viewer. I can't speak to its quality but it looks interesting.
You can also use fpdf class available at: http://www.fpdf.org.
It gives options for both outputting to a file and displaying on browser.
There is a simple solution using the embed tag:
<span class="fileShow">
<a href="aa.pdf" onclick="event.stopPropagation();" target="_blank">
<embed style="width:450px; height:300px; max-width:450px; max-height:300px" src="aa.pdf">
</a>
</span>