3

I get files by their urls by this code

file_get_contents($_POST['url'];

Then I do something with them.

But I don't want to operate with big files, how do I limit size of received file?

It should throw an error if file is bigger than 500kb.

James
  • 42,081
  • 53
  • 136
  • 161

2 Answers2

8

See my answer to this question. You need to have the cURL extension, with which you can make a HEAD HTTP request to the remote server. The response will let you know how big the file is, and you can then decide accordingly.

You are interested specifically in this line:

$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
5

Agree with @Jon

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_URL, $url); //specify the url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $head = curl_exec($ch);

    $size = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD);

    if(<limit the $size>){
    file_get_contents($url);
    }
Arun David
  • 2,714
  • 3
  • 18
  • 18