动态选择文件的PHP文件大小

时间:2012-12-27 15:32:48

标签: php filesize

我有一个php脚本,需要在单独的php脚本操作后确定文件系统上文件的大小。

例如,存在一个zip文件,该文件具有固定大小,但会根据尝试访问它的用户获取插入其中的未知大小的其他文件。所以提供文件的页面就像getfile.php?userid = 1234。

到目前为止,我知道这一点:

filesize('getfile.php'); //returns the actual file size of the php file, not the result of script execution

readfile('getfile.php'); //same as filesize()

filesize('getfile.php?userid=1234'); //returns false, as it can't find the file matching the name with GET vars attached

readfile('getfile.php?userid=1234'); //same as filesize()

有没有办法读取php脚本的结果大小而不仅仅是php文件本身?

5 个答案:

答案 0 :(得分:1)

filesize

  

从PHP 5.0.0开始,此函数也可以与某些URL一起使用   包装

类似

filesize('http://localhost/getfile.php?userid=1234');

应该足够了

答案 1 :(得分:1)

有人发布了使用curl执行此操作的选项,但在downvote之后删除了他们的答案。太糟糕了,因为这是我开始工作的一种方式。所以这是他们的答案对我有用:

$ch = curl_init('http://localhost/getfile.php?userid=1234');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //This was not part of the poster's answer, but I needed to add it to prevent the file being read from outputting with the requesting script
curl_exec($ch);

$size = 0;

if(!curl_errno($ch))
{
    $info = curl_getinfo($ch);
    $size = $info['size_download'];
}

curl_close($ch);

echo $size;

答案 2 :(得分:0)

获得输出大小的唯一方法是运行它然后查看。根据脚本的不同,结果可能会有所不同,但在实际应用中,最好的办法是根据您的知识进行估算。即如果你有一个5MB的文件并添加另外5k个用户特定的内容,那么它最终还是大约5MB。

答案 3 :(得分:0)

扩展伊万的答案:

你的字符串是'getfile.php',有或没有GET参数,这被视为本地文件,因此检索php文件本身的文件大小。

它被视为本地文件,因为它不是以http协议开头的。有关支持的协议,请参阅http://us1.php.net/manual/en/wrappers.php

答案 4 :(得分:0)

使用filesize()时,我收到一条警告: 警告:filesize()[function.filesize]:stat for ... link ... in .. file ...第233行

而不是filesize()我找到了两个替换它的工作选项:

1) $ headers = get_headers($ pdfULR,1); $ fileSize = $ headers ['Content-Length']; echo $ fileSize;

2) echo strlen(file_get_contents($ pdfULR));

现在它工作正常。