I have a very basic question illustrated by the code snippet below.
This is the relevant part of a much bigger program in which I wish to download a zip file. The code as shown, with the comments in place, produce the expected browser output "Download the file now."
When I un-comment the code, the zip file is correctly downloaded to my browser!
However, the browser output is not produced. How do I regain control? I would like for the user to then have other options. (By the way, un-commenting the single "Content-disposition" line is sufficient to cause the loss of control.)
I have tried including the code, putting it in a function, many possible combinations of ob_start, ob_end flush, etc., all to no avail. I am sure I am overlooking something very fundamental and would appreciate some suggestions.
Thanks.
<?php
$sZipFN = 'file.zip';
// header("Content-type: application/zip");
// header("Content-disposition: attachment; filename=$sZipFN");
// readfile($sZipFN);
$sMsg = "Download the file now.";
?>
<html>
<body>
<p> <?php echo $sMsg; ?> </p>
</body>
</html>
You can't - you have to redirect to the options page and then start the download via javascript redirect (document.location = 'http://download.url/';)
This will start the download and leave the user on the options page like you desire.
Don't forget to include a 'Click here if the download fails to start' link somewhere near the top of the page (just in case javascript is disabled)
Related
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.
for the download forced, when I save the file it has some extra html test.
my code
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="abc.txt"');
echo "test";
?>
when I save this is what I have
<script language="javascript">
// some garbage
//-->
</script>
test
I want only test.
Don't include the include file.
At the risk of stating the obvious, something is already outputting some code/including a file, etc. before you're trying to set the headers.
Whilst the cause of this is likely to be specific to your web app (you'll need to check precisely what's being output from the ground up), the requirement is that the headers need to be the very first (before any HTML, etc.) things output.
Based on your comment, do you mean that the JavaScript being outputted is in the PHP file that's doing the outputting, and you want it to not output that?
The first thought would be to remove the JavaScript. But beyond that, you may find some use from something like ob_start() to capture your output buffer and manipulate it before sending it to the client.
I was wondering how to accomplish an effect I've seen on several websites, where I click a download now link, it takes me to another page, and the download starts automatically. What is the best way to accomplish this?
Redirect to a page which emits the following headers:
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $length");
See this post about the restrictions on $filename.
edit in response to andr's answer, the php equivalent of redirect-after-x-seconds would be:
header("Refresh: 2; url=start_download.php");
(although you should officially specify a complete URL, I think) where start_download.php would contain the two lines above.
First you show the page with some content (please wait, blah blah) and then redirect to the file itself or to the script which outputs the file.
The redirect is done either via meta tag or javascript:
html: <meta http-equiv="refresh" content="5;url=http://example.com/foo.zip" /> where «5» is in seconds
js on page load: setTimeout("location.href=http://example.com/foo.zip", 5000) where «5000» is in milliseconds.
If you choose to output the file via php script, follow mvds's answer.