Web Hosting Talk







View Full Version : Downloading File


CChard
09-08-2002, 12:40 AM
How do you get PHP to start a download of a file (lets say a .zip)?

Here is my file:


<?php
include("functions.php");
if (!db_connect())
{
echo "Error: Could not connect to database. Please Contact Admin";
exit;
}
$result = mysql_query("select * from files WHERE id='$id' AND status='0'");
$row = mysql_fetch_array($result);

if(mysql_num_rows($result)==0)
{
echo "Error: Could not find file in database. Please Contact Admin";
exit;
}

$file = stripslashes($row['url']);
$downloads = $row['downloads']+1;
$fp = fopen($file, "r");

if(!fp)
{
echo "Error: Could not download file. Please Contact Admin";
exit;
}
else
{
$query = "update files set downloads='".$downloads."' where id='".$id."'";
$result = mysql_query($query);
echo "You may close this window.";
fclose($fp);
exit;
}

?>

The program finds the URL (Ex: http://www.atlanticrail.com/files/ac0001.zip) and then I need it to start a download.

I don't get any errors, just the "You may close this window" message. Meaning it opened the file.

mind21_98
09-08-2002, 12:46 AM
You're just in luck:

http://www.webhostingtalk.com/showthread.php?s=&threadid=70867

There should be enough info in that thread to help you. :D

CChard
09-08-2002, 12:53 AM
I saw that... I didn't find much help there. :) Thanks, though.

Anyway... in the few minutes, I found another soultion to my problem. So my script works now.

However, if anyone has any other ideas I would like to hear them. :cool:

OldOne
09-09-2002, 11:26 AM
www.hotscripts.com/PHP/Scripts_and_Programs/ File_Manipulation/Download_Systems/

You can watch at the above link.

Rich2k
09-09-2002, 06:46 PM
That linked thread gives you the PHP code to force a file download (rather than opening it in the browser)