My intention is to convert a total php page into html, and subsequently convert the html to pdf and render it through the browser.Which is done , apart from that while showing it on the browser , it will simultaneously download the pdf automatically which is not happening.
Its with PHP.
Can to tell me the basic concept ..as to how to do this.
Thanks in advance
You already render the page in the browser. Before displaying the page, header() the user to the location which will serve the same page as an attachment, but do not exit. This will allow them to download the file, but it will still load the file on the page. Not 100% sure that this will work, but it's worth a shot.
BTW different browsers will handle pdfs differently and depending on settings, plugins, etc. For instance, some might try to download the file anyway instead of showing it in the browser.
I think you need to look over the question again. As I read it, you're asking how to display something that hasn't yet been downloaded (while it's downloading), and that is obviously not possible and so cannot be what you mean.
Try creating a header, which tells the browser what to do when it receives the file.
<?php
header("Content-Type: $filedatatype" );
header("Content-Disposition: attachment; filename=\"" . $FileObject->name . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize);
?>
Related
My website contains a section that allows users to access restricted PDF files if they have access to them. Basically, they arrive at a page which has a nav bar at the top, allowing them to cycle between PDF files. Below this, there is an iframe which is linked to a PHP page that does the retrieval of the file, and displays it. This file is called falr_pdf.php, and depending on how it is accessed, the PDF file is either displayed inline, or downloaded as an attachment.
Here is the navbar page, along with the falr_pdf.php IFRAME embedded, to give you a clear idea.
As you can see, the PDF should be displayed inline, and then if the user clicks "Download File", the same page is opened in a new tab, but to download the file as an attachment instead of displaying it inline.
My problem is that this works flawlessly for small PDF files, but anything larger than about 1.6MB will take an incredibly long time to display inline. However, if they are downloaded using the direct link instead, they download at normal speed, very quickly. Here is the code I am using...
$path = $falr_filesbasedir . "/" . $folder . "_pdf/" . $file . ".pdf";
$public_name = basename($path);
header('Content-Length:'.filesize($path));
header('Content-type: application/pdf');
header('Content-Disposition: ' . $method . '; filename="' . $public_name . '"');
readfile($path);
exit;
Now I know that the issue is not in any of my variables or links, since it works fine for every type of small file, and the download works fine for large files. It's only that large files don't display inline correctly.
$method is only ever "inline" or "attachment"
Is this a bug with readfile()? Or is it something to do with my PHP settings?
I am at a complete loss here.
Have you tried looking at your performance waterfall? Have you tried accessing the file served directly from the webserver as a static document? Have you tried measuring timings in your php code and comparing that with what's happening in the browser? What is the latency and bandwidth between client and server? Whatt happens when you download the file using a client running on the same host as the server? Are there any signs of stress on the server host? These are the basic questions you should start by investigating.
At a guess, the most likely cause is buffering in php or on the webserver. If you unroll the readfile into read/write operations with flushes ever 200kb you can test this. But you need to start measuring properly.
We are trying to create a webpage in laravel where people are going to be able upload their codefiles to our server, so that other users can watch the code and download it in codefiles if they like it. We however can't figure out the best way to make this happen.
I tried to just let php get a file and echo out the content. this worked well fot html and css, but with php nothing got displayed what so ever. someone mentioned using eval(), however i've read that it is a really bad idea to do so. Another idea would be to stash the code in a database and fetch it from there, which we have tried before, but it sort of over complicated, and avoiding to do so would be prefereable, and instead go directly to i file.
So my question is, do anybody have an idea that might work safely, both for us and our server and for the users.
Something like this:
<?php
// read Codefile
$TheCode = file_get_contents($codefile);
// Print it...
echo htmlentities($TheCode);
?>
Save the php code in a flat file like one with a .dat extension.
then read the file.
$toechp = file(static.dat);
echo $toecho;
You can allow .dat files to be downloaded on browser using headers.
<?php
$file = "http://example.com/static.dat";
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile ($file);
?>
and you are done.
I'm sure this is a simple task, but on my wordpress site I want to create a download button that forces an .mp3 download, without opening a player (when left clicked), or the user having to right-click 'save target as'. I just need a straight forward button, that when left-clicked causes a file to be downloaded (as well as being easily trackable by Google Analytics).
Is a .php script required for this? You'd think this would be a very common function, and easy to solve....but I have spent hours on this and have been unable to get anything to work.
*if it's not obvious my coding skills are nearly non-existent.
I really appreciate anybody's time who can help me figure this out. Thanks!
***EDIT
Just found this on another post, but no comments if it would work or not. It was for a .pdf file though...
<?php
if (isset($_GET['file'])) {
$file = $_GET['file'] ;
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
?>
Save the above as download.php
Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.
You can use it like so:
Add the following link to your HTML file.
Download the cool PDF.
Well, this is possible, but you need to write a script to do it. This is a pretty poor (security and basic coding wise) from http://youngdigitalgroup.com.au/tutorial-force-download-mp3-file-streaming/
file: downloadit.php
<?php
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header ("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
you would then place it into a publicly accessible folder and build your links as such:
http://www.yoursite.com/downloadit.php?file=/uploads/dir/file.mp3
what this does is tells the browser to treat the file as a stream of bytes, rather than a particular MIME type which the browser would ordinarily do based on the file extension.
well I'm just wondering how I can get an mp3 download to start instantly, as oppose to it simply starting to play in the browser when you directly go to it.
Preferably using php headers.
So essentially when you click the file, I want a download box to appear saving save etc. Right now it just opens and starts playing in the browser.
Thanks
You'll need to create a PHP file that "redirects" to the MP3 file, and point your links to that PHP file.
Code as below:
<?php
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="fileName.mp3"');
readfile('originalFile.mp3');
?>
Note: The line that sets the Content-Disposition header is the critical one.
THE EXAMPLE
1) User enters in a playlist in a <textarea>
C:/music/foo.mp3
C:/music/bar.mp3
C:/music/hello.mp3
2) They click a save button. I send the user's playlist to the server with AJAX.
3) The server formats the text with PHP in this fashion:
<playlist>
<item>C:/music/foo.mp3</item>
<item>C:/music/bar.mp3</item>
<item>C:/music/hello.mp3</item>
</playlist>
4) A file save dialog pops up asking the user to save this formatted text as playlist.m3u on their own harddrive.
QUESTIONS
A) Is it possible to not write a file to the harddrive on the server when generating this m3u file? I don't want millions of files clogging up my server. I suppose PHP can echo out the formatted text and set headers to masquerade as a file.
B) How do I get the file save dialog to pop up for this on-the-fly file? If it were a real file, I would just have the PHP respond back with the location of the file. Then I would have JS insert a new iFrame with that location. But I don't want to write a file on the server, so I can't do this.
new Ajax.Request(
'generateM3u.php',
onSuccess: function(transport) {
$$('body').first().appendChild(
new Element(
'iframe', {
src: transport.responseText
}
)
);
}
);
You should take a look at http://php.net/manual/en/function.header.php from the PHP manual. There are a lot of user contributions at the bottom of the page regarding forcing the browser to show a download prompt rather than printing to screen.
Here is one from that page (By phpnet at holodyn dot com 31-Jan-2011 09:01) which I have edited slightly. I think it answers both questions A and B. Just send the textbox's contents to the PHP file through an iframe, allow it to format the text appropriately and send it back to the browser with the following headers.
$contents = '<playlist>etc....</playlist>';
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-Type: audio/x-mpegurl");
header("Content-Disposition: attachment; filename=\"playlist.m3u\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($contents));
ob_clean();
flush();
echo $contents;
Edit: If what you want is an all Javascript solution, then I personally don't know, and after a little google-ing, it looks like others don't either. Most seem to solve this with an invisible iframe that directs to a server-side file.
Edit 2: I've changed the content type so that it matches the m3u file type.
How about creating a form on your parent DOM, and post it to the IFRAME/pop-up that you created?
The POST action URL will be your generateMu3.php
To answer your questions,
A & B) I assume so... as long as generateM3u.php sets the correct MIMEType for the .m3u file...
I'm not familiar with syntax in PHP, but in both Java & .NET, you can set the response's MIMEType in the header to, say, a Word document, and the browser will read the header, and if it's a file that is "Save-able", it'll prompt the client to save the page as a file.
If I read this correctly there's a machine creating the .m3u files. In that case, perhaps just write the files to a temporary directory, /tmp on unix machines andC:\Windows\Temp on Windows machines. Those files are cleared on boot, which should allow you to handle B) without all the A).