检查外部文件的大小,php

时间:2011-03-27 02:23:08

标签: php html external filesize

我通过此代码获取文件的文件

file_get_contents($_POST['url'];

然后我跟他们做点什么。

但我不想使用大文件操作,如何限制接收文件的大小?

如果文件大于500kb,则应该抛出错误。

2 个答案:

答案 0 :(得分:8)

请参阅此问题的answer。您需要具有cURL扩展名,您可以使用该扩展名向远程服务器发出HEAD HTTP请求。响应将告诉您文件的大小,然后您可以相应地做出决定。

您对此专栏感兴趣:

$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

答案 1 :(得分:3)

同意@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);
    }