Jump to content

help with downloading a file from another site


koolaid11

Recommended Posts

I want to be able to have a script on mysite(http://mysite.com) to download a file from another site(http://downloadsite.com).  I wrote a script where you enter the location of a file (http://downloadsite.com/song.mp3) and click "fletch file" it well get the file and allow you to download it.  But it takes forever it seems like it transfers to my websever first before the dialog pops up so i can download it.  Heres the code:

 

<?
if (!isset($_POST['Fetch'])){
?>
<html>
<head>
<title>File Fetcher</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table border="1">
<tr>
<td colspan="2"><center><b>File Fetcher</b></center></td>
</tr>
  <tr>
  <form method="post" action="<?php echo $PHP_SELF;?>">
    <td><input name="fpath" type="text" size="60"></td>
    <td><input name="Fetch" type="submit" value="Fetch File"></td>
</form>
  </tr>
</table>

</body>
</html>
<?
}else{
$fpath= $_POST["fpath"];
$fpath= pathinfo($fpath);
$path= $fpath['dirname'];
$file= $fpath['basename'];
send_file($path, $file);
}
# Send (download) file via pass thru
#-------------------------------------
function send_file($path, $file){

# Make sure the file exists before sending headers
#-------------------------------------------------
$mainpath = "$path/$file";
if(!$fdl=@fopen($mainpath,'r')){
	#include ("$header");
	print "<p><center><font class=\"changed\">ERROR - Invalid Request (Downloadable file Missing or Unreadable)</font></center><br><br>";
	die;
}else{
	set_time_limit(0);
	# Send the headers then send the file
	#------------------------------------
	header("Cache-Control: public, must-revalidate");
	header("Pragma: hack");
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=\"".$file."\"");
	header("Content-length:".(string)(filesize($mainpath)));
	header("Content-Transfer-Encoding: binary\n");
	sleep(1);
	fpassthru($fdl);
}
return;
}

?>

 

I basicly want it to be as fast as if it was already a link that you could just right click and 'save target as' and you would have it downloaded.

 

Belows jsut a simple script that makes what you put in a link that you can right click and download if u wanted to see it

<html>
<head>
<title>File Fetcher</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?
$path = $_POST["fpath"];
if (!isset($_POST['Fetch'])){
?>
<table border="1">
<tr>
<td colspan="2"><center><b>File Fetcher</b></center></td>
</tr>
  <tr>
  <form method="post" action="<?php echo $PHP_SELF;?>">
    <td><input name="fpath" type="text" size="60"></td>
    <td><input name="Fetch" type="submit" value="Fetch File"></td>
</form>
  </tr>
</table>
<?
}else{
echo "<a href=\"$path\">Right Click to download</a>";
echo "<br>";
echo "<a href=\"$PHP_SELF\">Go Back to do another</a>";
}
?>
</html>

 

Link to comment
Share on other sites

Have you tried just making an input button and wrapping that in a link?

 

Like

 

<a href="<?php echo $path; ?>"><input type="button" name="Fetch" value="Fetch File"></a>

 

I know it's a crude solution, but if you have dreamweaver you can use it to create a flash button then just insert your path there. Hope that helps a little.

 

Sq

Link to comment
Share on other sites

<?php
if (!isset($_POST['Fetch'])){
?>
<html>
<head>
<title>File Fetcher</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table border="1">
<tr>
<td colspan="2"><center><b>File Fetcher</b></center></td>
</tr>
  <tr>
  <form method="post" action="<?php echo $PHP_SELF;?>">
    <td><input name="fpath" type="text" size="60"></td>
    <td><input name="Fetch" type="submit" value="Fetch File"></td>
</form>
  </tr>
</table>

</body>
</html>
<?php
}
else
{
	$fpath = $_POST["fpath"];
	echo '<a href="'.$fpath.'"><input type="button" name="Fetch" value="Fetch File"></a>';
}
?>

 

That worked for me.. aside from the link I put in.. because it wasnt really a link lol.. but it echoed in the right spot and make the button clickable.. you didnt just copy & paste what I wrote out earlier did you?

Link to comment
Share on other sites

But it takes forever it seems like it transfers to my websever first before the dialog pops up so i can download it.

 

Yup, that's exactly what's happening.  Your PHP script is calling fopen() on the file, which means it needs the entire file before it can continue.

 

I've never tried, but perhaps you can accomplish what you're after with cURL.

Link to comment
Share on other sites

Have you tried just making an input button and wrapping that in a link?

 

Like

 

<a href="<?php echo $path; ?>"><input type="button" name="Fetch" value="Fetch File"></a>

 

I know it's a crude solution, but if you have dreamweaver you can use it to create a flash button then just insert your path there. Hope that helps a little.

 

Sq

 

 

ya i know i got that to work...  are you trying to say make a little flash thing to pop up a download dialog??

 

and ill look into cURL

Link to comment
Share on other sites

But it takes forever it seems like it transfers to my websever first before the dialog pops up so i can download it.

 

Yup, that's exactly what's happening.  Your PHP script is calling fopen() on the file, which means it needs the entire file before it can continue.

 

I've never tried, but perhaps you can accomplish what you're after with cURL.

 

 

curl will only make the time of transfering it to myserver faster... i want to be able to download it directly from the downloasite server instead of transfering to mysite than to my computer

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.