简体   繁体   English

passthru(“ cat文件”)的性能

[英]performance of passthru(“cat file”)

I'm using passthru("cat filepath") in my download script. 我在下载脚本中使用passthru(“ cat filepath”)。 My concern is that it might use a lot of server resource. 我担心的是,它可能会使用大量服务器资源。

What is the difference between directly link a file in a public directory and download a file using passthru("cat filepath") in php? 直接链接公共目录中的文件和使用php中的passthru(“ cat filepath”)下载文件有什么区别?

What is the difference between directly link a file in a public directory and download a file using passthru("cat filepath") in php? 直接链接公共目录中的文件和使用php中的passthru(“ cat filepath”)下载文件有什么区别?

The difference is that linking directly to a file does not invoke PHP, while running a PHP script which in turn runs cat causes, well, both PHP and cat to be invoked. 不同之处在于,直接链接到文件不会调用PHP,而运行PHP脚本又会运行cat脚本,导致PHP和cat都被调用。 This will take up a moderate amount of extra memory, but won't cause server load under most circumstances. 这将占用适度的额外内存,但在大多数情况下不会导致服务器负载

I was using readfile(), but this function can't be used for files larger than 2gb 我正在使用readfile(),但是此函数不能用于大于2GB的文件

You might want to find a better solution than passing all of the file contents through PHP, in that case. 在这种情况下,您可能想要找到一种比通过PHP传递所有文件内容更好的解决方案。 Look into X-Sendfile support in your web server software of choice. 在您选择的Web服务器软件中查看X-Sendfile支持。

Don't use passthru() for that, you're opening yourself to CLI Injection and performance is terrible. 请勿为此使用passthru() ,因为您正向CLI注入敞开大门,而性能却很糟糕。 readfile() exists just for that. readfile()仅用readfile()目的。

readfile($filepath);

There is a small overhead when passing through PHP compared to a direct link but we are usually talking of milliseconds. 与直接链接相比,通过PHP传递的开销很小,但是通常所说的是毫秒。 However, the browser will not be able to request a 206 Partial when using readfile() unless you code support for it or use something like PEAR::HTTP_Download . 但是,使用readfile()时,浏览器将无法请求206 Partial ,除非您对此代码进行支持或使用类似PEAR::HTTP_Download

EDIT: Seems you are using passthru() because apparently readfile() doesn't handle >2GB files properly (I never had that problem with readfile() , in fact I just tested it with a 7.2 GB file and it worked fine). 编辑:似乎您使用的是passthru()因为显然readfile()无法正确处理> 2GB的文件(我从来没有readfile()问题,实际上我只是用7.2 GB的文件对其进行了测试,并且工作正常)。 In which case, at least escape your parameters. 在这种情况下,至少请转义您的参数。

function readfile_ext($filepath) {
  if(!file_exists($filepath))
    return false;

   passthru('cat ' . escapeshellarg($filepath));
   return true;
}

Instead of passthru('cat filepath') , use the PHP native readfile('filepath') , which has better performance. 代替passthru('cat filepath') ,使用具有更好性能的PHP本机readfile('filepath')

Both methods will be slower than simply directly linking to the file though, since PHP has a certain overhead. 但是,这两种方法都比直接直接链接到文件要慢,因为PHP具有一定的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM