开发者

Check size of external files, php

开发者 https://www.devze.com 2023-02-20 05:14 出处:网络
I get files by their urls by this code file_get_contents($_POST[\'url\']; Then 开发者_开发知识库I do something with them.

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.


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


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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消