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



我有一个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文件本身?

filesize

从PHP 5.0.0开始,这个函数也可以与一些URL一起使用包装器。

比如

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

应该足够

有人发布了一个使用curl的选项,但在被否决后删除了他们的答案。太糟糕了,因为这是我唯一能成功的方法。下面是他们给我的答案:

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

获取输出大小的唯一方法是运行它,然后查看。根据脚本的不同,结果可能会有所不同,但对于实际使用,最好的方法是根据您的知识进行估计。例如,如果你有一个5MB的文件,并添加了另外5k的用户特定内容,那么最终它仍然是5MB左右。

详述Ivan的回答:

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

它被视为本地文件,因为它不是从http协议开始的。支持的协议请参见http://us1.php.net/manual/en/wrappers.php

当使用filesize()时,我得到了一个警告:警告:filesize() [function.]Filesize]: stat failed for…link…在文件. . …第233行

代替filesize(),我找到了两个工作选项来替换它:

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

2)回声strlen (file_get_contents (pdfULR美元));

最新更新