Download file with Curl and php

Asked

Viewed 1,836 times

3

I have two servers server1 and the server2, the server2 only accept request from server1 if it is another ip it returns 404, my website is on server1 and the files for download on server2, I made the following script to return the download of server2:

    ob_start();
    set_time_limit(0);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $r = curl_exec($ch);
    curl_close($ch);
    header('Expires: 0'); // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
    header('Cache-Control: private', false);
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="' . basename($url) . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . strlen($r)); // provide file size
    header('Connection: close');
    echo $r;

However when the file is too large it displays the following memory error message which would be the solution for it?

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 139642559 bytes) in

  • Try increasing the limit with the command: ini_set("memory_limit","512M");

  • @Math the prank is not good practice if you don’t have a dedicated server. You can consume server resources with PHP.

  • @Jorgeb. thanks for the info, I’m not good at php. But so what? At least it solves?

  • Leandro anyway take a look at this answer: http://answall.com/a/15641/7210

  • Yeah, it solves @Math. But for example I have a company server with only 2G of RAM, if I use 512 only for PHP will leave the other services to bread and water ;)

  • @Jorgeb. Would have some other way to return the download ?

  • Leandro also looking for this answer. To this day I do not know how to best Download and Upload in PHP.

Show 2 more comments

2 answers

1

Not use PHP to do this, but yes, use Apache reverse proxy:

Example of configuration (part) in server1:

ProxyPass       /arquivos/  http://server2.example.com/

or you can still work with file types:

<LocationMatch \.pdf$>
  ProxyPass http://server2.example.com/
</Locationmatch>

Extra:

If you need the client’s IP ma machine, in server2, this code will help you:

<?php

echo $_SERVER['REMOTE_ADDR']; // IP do server1

if (array_key_exists('HTTP_X_FORWARDED_FOR', $server)) {
    if (strpos($server['HTTP_X_FORWARDED_FOR'],',') !== false) {
        $server['REMOTE_ADDR'] = current(explode(',', $server['HTTP_X_FORWARDED_FOR']));
    } else {
        $server['REMOTE_ADDR'] = $server['HTTP_X_FORWARDED_FOR'];
    }

    $_SERVER['REMOTE_ADDR'] = $server['REMOTE_ADDR'];
}

echo $_SERVER['REMOTE_ADDR']; // IP do Cliente
  • In this second example everything that is pdf goes to this address?

  • Yes, in the second example, everything goes to server2, but in server2, the IP of the client that will appear is the IP of Server1. You need client machine IP in server2?

  • No, the request has to be with the ip of the server1 and only return the download to client.

  • Then this will resolve. If in the future you need to increase security, implement lock rules in server1

  • Da para gerar esse Proxypass no php porque as url são dinâmica?

  • Proxypass you must configure in Apache, and do the RELOAD or RESTART of the Apache server at each change, so, no, you cannot generate in PHP

  • @Leandrocosta has decided what he will do?

  • not yet, I’m giving a studied to see the best method as soon as I find put here!

Show 3 more comments

0

What this error means is that you are trying to send bytes information above the set limit of 256 Megabytes.

Try adding this to the top of your code:
ini_set('memory_limit', '-1');

Ref: Memory Limit

If this does not work, you will need to modify your file .htaccess and increase values for parameters:
post_max_size = 100M
upload_max_filesize = 100M

  • 1

    But if the file has Gigas?

  • @Leandro can configure to the values you want, if you need to pass gigabytes of information set the limits to 2048Mfor example, or more.

  • But if two people download 2GB files it will give error?

  • not because those restrictions are for the individual file size and not for the total sum of buffer downloads.

  • I drew face value! D

  • 1

    What if two people at the same time download a 2048M file on a 2048M memory server?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.