|
-
May 5th, 2007, 10:19 PM
#1
Thread Starter
WiggleWiggle
Download file
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?
My usual boring signature: Something
-
May 7th, 2007, 05:19 AM
#2
Re: Download file
this works:
PHP Code:
file_put_contents('c:/path/to/filename_to_save.txt', file_get_contents('http://url.to/filename_to_grab.txt'));
-
May 7th, 2007, 07:19 PM
#3
Thread Starter
WiggleWiggle
Re: Download file
 Originally Posted by kows
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?
My usual boring signature: Something
-
May 8th, 2007, 01:51 AM
#4
Re: Download file
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;
-
May 8th, 2007, 06:08 PM
#5
Thread Starter
WiggleWiggle
Re: Download 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?
My usual boring signature: Something
-
May 8th, 2007, 06:38 PM
#6
Re: Download file
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".
-
May 8th, 2007, 06:51 PM
#7
Thread Starter
WiggleWiggle
My usual boring signature: Something
-
May 8th, 2007, 06:54 PM
#8
Thread Starter
WiggleWiggle
Re: Download file
is $filename the name of the file, or the location?
My usual boring signature: Something
-
May 8th, 2007, 07:01 PM
#9
Thread Starter
WiggleWiggle
Re: Download file
i tryed the code, and set it all up and i get the following errors:
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
here is my code:
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!";
} ?>
My usual boring signature: Something
-
May 9th, 2007, 12:59 AM
#10
Re: Download file
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.
-
May 9th, 2007, 01:59 AM
#11
Re: Download file
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:
PHP Code:
header("Content-Type: " . $content);
header("Content-Length: " . (string)(filesize($file)));
header('Content-Disposition: attachment; filename="' . $name . '"');
readfile($file);
exit;
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.
Last edited by kows; May 9th, 2007 at 02:07 AM.
-
May 9th, 2007, 05:52 AM
#12
Re: Download file
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.
Last edited by penagate; May 9th, 2007 at 05:56 AM.
-
May 9th, 2007, 06:09 PM
#13
Thread Starter
WiggleWiggle
Re: Download file
here is what i have now:
download.php (a download screen)
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!";
}
?>
and download1.php (the actual download)
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."
}
?>
My usual boring signature: Something
-
May 9th, 2007, 06:44 PM
#14
Re: Download file
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>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|