how do i make a download file script? I plan to download podcasts (mp3s) and zips mostly. maybe video files. How do i script it to download them?
Printable View
how do i make a download file script? I plan to download podcasts (mp3s) and zips mostly. maybe video files. How do i script it to download them?
this works:
PHP Code:file_put_contents('c:/path/to/filename_to_save.txt', file_get_contents('http://url.to/filename_to_grab.txt'));
but i want the "Save File" dialog to show? how do i do that?Quote:
Originally Posted by kows
PHP Code:header('Cache-Control: no-cache, must-revalidate');
header("Content-type: $contentType");
header("Content-disposition: attachment; filename=\"$filename\"");
header('Content-transfer-encoding: binary');
header("Content-Length: $fileLength");
echo $file;
for the content type could i put like "zip archive" and "mp3" or are there certain names i have to use?
also does the file length have to be exact?
file length is the file size. it can be defined with filesize(). $contentType needs to be a correct mime type. for an mp3, you can use "audio/mp3" and for zip files you can use "application/zip".
oh ok.
is $filename the name of the file, or the location?
i tryed the code, and set it all up and i get the following errors:
here is my code:Code:Warning: filesize() [function.filesize]: stat failed for http://dylan.rapidfriends.com/projects/urlsnipper.zip in /home/jphendr/public_html/dylan/download.php on line 69
Warning: Cannot modify header information - headers already sent by (output started at /home/jphendr/public_html/dylan/download.php:3) in /home/jphendr/public_html/dylan/download.php on line 70
Warning: Cannot modify header information - headers already sent by (output started at /home/jphendr/public_html/dylan/download.php:3) in /home/jphendr/public_html/dylan/download.php on line 71
Warning: Cannot modify header information - headers already sent by (output started at /home/jphendr/public_html/dylan/download.php:3) in /home/jphendr/public_html/dylan/download.php on line 72
Warning: Cannot modify header information - headers already sent by (output started at /home/jphendr/public_html/dylan/download.php:3) in /home/jphendr/public_html/dylan/download.php on line 73
Warning: Cannot modify header information - headers already sent by (output started at /home/jphendr/public_html/dylan/download.php:3) in /home/jphendr/public_html/dylan/download.php on line 74
http://dylan.rapidfriends.com/projects/urlsnipper.zip
PHP Code:<?PHP
if ($_GET['id'] != "") {
$id = $_GET['id'];
$sql = "SELECT * FROM `files` WHERE file_id='$id' LIMIT 1";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
$array = mysql_fetch_array($query);
if ($num != "0") {
$contentType = $array['content_type'];
$filename = $array['filename'];
$file = $array['location'];
$fileLength = filesize($file);
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: $contentType");
header("Content-disposition: attachment; filename=\"$filename\"");
header('Content-transfer-encoding: binary');
header("Content-Length: $fileLength");
echo $file;
echo "You are currently downloading <b>$filename</b>. If your download does not start, click <a href='$file'>here</a>. If you expirenece difficulty download, please contact the admin.<br />";
} else {
echo "The file you requested was not found in the database. Please try again.";
}
} else {
echo "An error has occured, please press the Back button on your browser to try again!";
} ?>
You cannot send headers once you have generated any output. Ensure there is no white space preceding the opening PHP tag or any echo statements that produce output.
you're not echoing the location. you're echoing the file's contents. and if you echo anything else on that page, they're going to be saving it to that file, too. if you don't understand what that means -- basically, if you echo "hi" after echoing the file's contents, you're going to be saving the file's contents and the string "hi"; so if it was a text file, it would have the string "hi" appended, and if it was a binary file.. it would be corrupt. you can't do what you're doing, basically (at least not the way you want to).
below, $file should be a relative or full path to the file you're wanting to download. I usually do something like this (left out additional headers like cache time and whatever), and store content types in a database:
if you want to display a "you are now downloading" page before the file actually starts downloading, then you'll need to use a refresh header to redirect to the download script a few seconds after loading the download page (notice: download page is the page to download the file, download script is the script that actually loads the file and lets the user download). it shouldn't actually load that page, though, because your actual download script doesn't actually output anything. it should just prompt to save.PHP Code:header("Content-Type: " . $content);
header("Content-Length: " . (string)(filesize($file)));
header('Content-Disposition: attachment; filename="' . $name . '"');
readfile($file);
exit;
There is no need to use a download script if you are simply sending a file that already exists on the server. In those cases you can simply make a page that says "You are now downloading xyz.zip" and use a Refresh header to initiate the download.
Download scripts are really only useful when you are generating the content of the file dynamically or retrieving it from a database; i.e. in cases when the file does not physically exist on the server.
Also, the path you pass to filesize() must be a qualified or relative local path, not a HTTP URL. filesize() uses the stat() command to retrieve the length of the file, which does not work over HTTP (and would be a waste of time anyway). It appears that you have stored the location as a qualified URL when you should be storing a relative path. Storing URLs to resources on your site will break if you ever need to change the URI schema or your domain name.
here is what i have now:
download.php (a download screen)
and download1.php (the actual download)PHP Code:<?PHP
if ($_GET['id'] != "") {
$id = $_GET['id'];
echo "Downloading file.... Please wait. If download doesnt start in 5 seconds,<a href='download1.php?id=$id' target='_blank'>Click Here</a>";
?>
<SCRIPT LANGUAGE="javascript">
<!--
window.open ('download1.php?id=<?=$id?>', 'newwindow', config='height=50,
width=100, toolbar=no, menubar=no, scrollbars=no, resizable=no,
location=no, directories=no, status=no')
-->
</SCRIPT>
<?PHP
} else {
echo "An error has occured, please press the Back button on your browser to try again!";
}
?>
PHP Code:<?PHP
$id = $_GET['id'];
$sql = "SELECT * FROM `files` WHERE file_id='$id' LIMIT 1";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
$array = mysql_fetch_array($query);
if ($num != "0") {
$content = $array['content_type']; //What file type it is.
$filename = $array['filename']; //The name of the file ex: "CoinCounter".
$file = $array['location']; //The actual file ex: http://dylan.rapidfriends.com/projects/test.zip
header('Cache-Control: no-cache, must-revalidate');
header('Content-transfer-encoding: binary');
header("Content-Type: " . $content);
header("Content-Length: " . (string)(filesize($file)));
header('Content-Disposition: attachment; filename="' . $name . '"');
readfile($file);
exit;
} else {
echo "The file you requested doesnt exsist on the site. Please try again."
}
?>
Using a popup is a bad idea. Most browser now block them. as suggested use the refresh header:
PHP Code:<?php
if ($_GET['id'] != "") {
$id = $_GET['id'];
$url = 'http://' . $_SERVER['SERVER_NAME'] . '/download.php?id=' . $id;
header("Refresh: 5; url=$url");
}
?>
<http>
<body>
<?php if (isset($id)): ?>
<p>Downloading file.... Please wait. If download doesnt start in 5
seconds,<a href='download1.php?id=$id' target='_blank'>Click
here</a>
</p>
<?php else: ?>
<p>An error has occured, please press the Back
button on your browser to try again!</p>
<?php endif; ?>
</body>
</html>