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
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
?>

 

Link to comment
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
?>

 

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Yeah, I'm completely lost.

 

So the download.php gets the data....then what? Another page? What would be in that page?

 

 

What exactly are you trying to do?

 

1. Server downloads file

Then: Server pipes file to user to download?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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.