Jump to content

Download file through php


marklarah

Recommended Posts

I've been trying now for ~3 hours  :'( :'( :'( - I am stuck and fed up :(

 

So I need to download a remote file, though my server. So piping the download, or tunneling it though my server.

 

How would I do this? I've been at it ages with download.php's an headers and whatnot, but I just can't seem to figure it out.

 

The file url is stored in a string, I just need for the file to be downloaded though the server.

 

Any idea how to do this?

Link to comment
https://forums.phpfreaks.com/topic/150135-download-file-through-php/
Share on other sites

You should check out your favorite friend, Google

 

Anyways, found this script here.

 

<?php
$url = 'http://www.assistprogramming/image.jpg';
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $url);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); //return the transfer in a binary format .

$data = curl_exec($curl_handler);
curl_close($curl_handler);

// Do whatever you want to with $data - it holds the binary content
?>

 

You should check out your favorite friend, Google

 

Anyways, found this script here.

 

<?php
$url = 'http://www.assistprogramming/image.jpg';
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $url);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); //return the transfer in a binary format .

$data = curl_exec($curl_handler);
curl_close($curl_handler);

// Do whatever you want to with $data - it holds the binary content
?>

 

 

Awesome...but how I do I get the browser to download $data?

Specify the content-type in a HTTP header. (You'll have to have an array of extensions mapped to content-types.)

Then readfile($data);

 

You will need extra headers to make the save file dialog box come up.

You can google that too: Download file headers cross-browser

okay, it doesn't work.

 

So I have this as my download.php:

 

<?php
$url = $_GET['url'];
$name = $_GET['name'];
header("Content-Type: application/octet-stream;");
header('Content-Disposition: attachment; filename="'.$name.'"');
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $url);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); 
$data = curl_exec($curl_handler);
curl_close($curl_handler);
echo $data;      
exit;
?>

 

It works fine when I did a text file

It worked fine on a rar file. It seemed to be going okay, but then it stopped: it only downloaded 0.2kb of the file (although it had correct file name and such)

 

I think when I tried it before, when it just echoed the text onto the page, and I tired this file, the server timed out, it failed to allocate the data or something.

 

Do you know a way around this?

Your memory usage is going over 32Mb, update the php.ini file to increase the memory

 

why not just do this

<?php
$url = 'http://www.dynamixlabs.com/images/largeimage.bmp';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($url));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($url);
exit;
?>

I didn't see the reply  :-X But anyway.

 

Warning: readfile() [function.readfile]: URL file-access is disabled in the server configuration in ******* on line 14

 

Warning: readfile(***) [function.readfile]: failed to open stream: no suitable wrapper could be found in *** on line 14

 

Thats what I get. It just downloads the filename.ext.html, and that was what was in it.

fopen wrappers have been disabled.. that sucks!

if its your server just update allow_url_include in the php.ini

if not then get a better server i guess your stuck with curl (not a bad thing) but

i would update to this

 

<?php
$url = $_GET['url'];
$name = $_GET['name'];
header("Content-Type: application/octet-stream;");
header('Content-Disposition: attachment; filename="'.$name.'"');
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $url);
curl_setopt($curl_handler, CURLOPT_HEADER, false); //added
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 0); //pointless having this on!
curl_setopt($curl_handler, CURLOPT_BINARYTRANSFER, 1); 
curl_exec($curl_handler);
curl_close($curl_handler);
exit;
?>

 

EDIT: turned off header

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.