PutterPlace Posted April 19, 2008 Share Posted April 19, 2008 I am trying to figure out a way to remotely download files using PHP (and cURL if needed). For example: Remote File to Download: http://www.domain2.com/file.jpg Script To Download The File: http://www.domain1.com/download.php?file=http%3A%2F%2Fwww.domain2.com%2Ffile.jpg I want this script to be able to force the user to download the file instead of opening the file in the browser. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/101881-solved-downloading-with-php/ Share on other sites More sharing options...
unidox Posted April 19, 2008 Share Posted April 19, 2008 All that will do is basically redirect the user to where the download will be. Are you trying to hide the download url? Quote Link to comment https://forums.phpfreaks.com/topic/101881-solved-downloading-with-php/#findComment-521415 Share on other sites More sharing options...
PutterPlace Posted April 19, 2008 Author Share Posted April 19, 2008 Yea I would like to hide the download URL. I was hoping to use cURL so that it appears all traffic is coming my own site. Quote Link to comment https://forums.phpfreaks.com/topic/101881-solved-downloading-with-php/#findComment-521421 Share on other sites More sharing options...
thebadbad Posted April 19, 2008 Share Posted April 19, 2008 Simple solution: download.php <?php if (!$_GET) {echo 'No download id set.'; exit;} //URLs can't contain spaces $allowed = array('http://www.phpfreaks.com/images/logo_main.jpg', 'http://download.microsoft.com/download/a/8/7/a87b3d05-cd04-4743-a23b-b16645e075ac/readme.txt'); $id = $_GET['id']; $url = $allowed[$id]; $filename = basename($url); header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.$filename.'"'); echo file_get_contents($url); exit(); ?> download.php?id=0 will now prompt the user to download the first file in the $allowed array. The script works in recent Firefox and IE versions; not sure about older browsers. Look up some other force download scripts if you want better compatibility (i.e. find out which other headers to send). But remember that file_get_contents() probably will time out on large files. Quote Link to comment https://forums.phpfreaks.com/topic/101881-solved-downloading-with-php/#findComment-521476 Share on other sites More sharing options...
Northern Flame Posted April 19, 2008 Share Posted April 19, 2008 or you can try: <?php $saveName = stripslashes($_GET["name"]); $savePath = stripslashes($_GET["path"]); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=$saveName"); header("Content-Transfer-Encoding: binary"); header("Content-length: " . filesize($savePath)); readfile($savePath); ?> save that file as download.php then just type http://www.mywebsite.com/download.php?name=File.jpg&path=file.jpg name = the name its going to be downloaded as path = the url of the file EDIT: oh sorry, I didnt notice that you were trying to get the file off a different server. This script only works for downloading off of your server. Quote Link to comment https://forums.phpfreaks.com/topic/101881-solved-downloading-with-php/#findComment-521480 Share on other sites More sharing options...
thebadbad Posted April 19, 2008 Share Posted April 19, 2008 But that wont hide the path of the actual files. I've added a bit of error checking to my script above: <?php if (!$_GET['id']) {echo 'No download id set.'; exit;} //URLs can't contain spaces $allowed = array('http://www.phpfreaks.com/images/logo_main.jpg', 'http://download.microsoft.com/download/a/8/7/a87b3d05-cd04-4743-a23b-b16645e075ac/readme.txt'); $id = $_GET['id']; if (!ctype_digit($id) || $id >= count($allowed)) {echo 'Wrong download id.'; exit;} $url = $allowed[$id]; $filename = basename($url); header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.$filename.'"'); echo file_get_contents($url); exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/101881-solved-downloading-with-php/#findComment-521487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.