Sign in to follow this  

[web] automatic file download in PHP or HTML

This topic is 4070 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. When I call somepage.php, and in page I load externaly download file path, how can I get to automaticly download that file without clicking anywhere? What is the command that starts downloading process? I put in file echo '<meta http-equiv="Refresh" content=0;url="'.$link.'">'; and that starts downloading, but on the next line I call header() function, to reexecute a page and that interupts downloading. If I don't call header() function, in source code of php stay a link to file, and I don't want that.

Share this post


Link to post
Share on other sites
I don't know what exactly is your problem but I can see that the HTML isn't formatted properly.
Your code:
echo '<meta http-equiv="Refresh" content=0;url="'.$link.'">';

Suppose $link is "http://ww.gamedev.net" then you will be echoing:
<meta http-equiv="Refresh" content=0;url="http://www.gamedev.net">

Notice the error in the content attribute (the quotes). The correct way is
<meta http-equiv="Refresh" content="0;url=http://www.gamedev.net">

The attribute is content and its value is "[time];url=[link]"
Maybe changing the echo statement will refresh the page without requiring a call to header()

Share this post


Link to post
Share on other sites
you can try this, an adapt as you need
this is my template i use for opening a file in php, and sending it to the user as downloadable, with another name. i suppose this is what you want.

adapt the way you need it. but do remind to put it before the DTD, or anything else in html, since these are headers.


<?php

if ($_GET['id']==1) {
$filename = 'chei.php';
$shortname = 'buhu.txt';
}

if (1==1)
{
$size = filesize($filename);
header("Content-Type: application/save");
header("Content-Disposition: attachment; filename=$shortname");
$fh = readfile("$filename"); // I use this instead of fopen because when fopen is used, it only reads 1KB of data
//fseek($fp, 0);
//fpassthru($fh);

exit;
} else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Download Error</title>
<style type="text/css">
<!--
body {background-image:url(include/background.gif);
font-family:helvetica,arial,sans-serif}
a:hover {text-decoration:none; border-width:thin; border-style:dotted;
background-color:#f2f2ff; color:#000000}
a:focus {text-decoration:none; background-color:#dadae6; color:#000000}
a:active {text-decoration:none; background-color:#ffffff; color:#000000}
-->
</style>
</head>
<body>
<h1>File <?php echo($filename); ?> not available</h1>
<p>
Either the file you requested does not exist or you are not permitted to
download it using this page.
</p>
</body>
</html>
<?php
}
?>





edit: filename is the physical file to be read, shortname is the default filename under which the user will save it (the one that will appear in the save dialog by default)

Share this post


Link to post
Share on other sites
Quote:
Original post by izua
you can try this, an adapt as you need
this is my template i use for opening a file in php, and sending it to the user as downloadable, with another name. i suppose this is what you want.

adapt the way you need it. but do remind to put it before the DTD, or anything else in html, since these are headers.

*** Source Snippet Removed ***

edit: filename is the physical file to be read, shortname is the default filename under which the user will save it (the one that will appear in the save dialog by default)


yeah that is what I want to achieve.
I use this:

<?php
ob_start();
$page = $_SERVER['PHP_SELF'];
$ip = $_SERVER["REMOTE_ADDR"];
$dotip = str_replace (".", "", $ip);
$hash = md5 ($dotip);
$id = $_GET['dl'];
$key = '';
$path = '../';
$xml = new DomDocument('1.0');
$dl_link ='';

if($id == 'true'):
$key = $_GET['game'];
if($_GET['hash'] == $hash):
$xmlpath = $key.'.xml';
if (file_exists($xmlpath)):
if($xml->load($xmlpath)):
$dl_link=$xml->getElementsByTagName('dl')->item(0)->firstChild->nodeValue;
if($dl_link)
{
echo '<meta http-equiv="Refresh" content=0;url="'.$path.$dl_link.'">';
// header ("Location: " . $page ."?".$key."=read");
}
endif;
endif;
else:
header ("Location: " . $page ."?".$key."=read");
endif;
endif;
ob_end_flush();
?>;



and after that it loads a page normaly.
so if page is something like
mypage.php?dl=true&game=mygame&hash=5321adf7fca98789fc
it will first get from mygame.xml a dl path, then it will check if md5 hash is equal of hash of ip adress and then it will eventualy start a download.
if not it will show a page link in adress bar like: mypage.php?mygame=view and show desrciption of game.
So I have a link provided from xml file, and what I wanted is to download to start.
Mine solution is less elegant, but thats because I'm using php for 24 hours, and my logic is c++ish (actually php is very similar to c++, thats why I assimiliated is so quickly).

There is one rem-ed line. I wanted to start a download, then change pages link to mypage.php?mygame=read, but that wont work.

I'll try your script.

Share this post


Link to post
Share on other sites
you'll have to work around with the variables and conditions, since i just copy-pasted it from my stuff, but overall, the headers are fine.


simply put, the download is just this code

$shortname="myfile.html";
header("Content-Type: application/save");
header("Content-Disposition: attachment; filename=$shortname");
$fh = readfile("$page");



i suppose $page is where the file name actually resides.

i use the previous piece of code when i need a file list (stored in a database, for example) and i request a file via get (like file.php?id=13) but i want it to be sent under another name.

this is also a good method if you want to have your files downloadable by keys (like some kind of one-use only, or licenses), or, if you want statistics on file hits. this will assume a basic download, if the client uses some download manager that splits the download into several pieces, you might get higher results, you'll need a workaround before considering a hit valid, after an IP for example, or a cookie.

applications are endless :)

izua

Share this post


Link to post
Share on other sites

This topic is 4070 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this