Jump to content

[SOLVED] Downloading with PHP


PutterPlace

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
?>

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.