PHP:创建.tar文件极其缓慢
问题描述:
我想使用下面的代码将10.000个文件打包到.tar文件中。没有太大的档案大小不会超过几个100MB。我的问题是这需要很长时间。其实我现在正在打包9000个文件一个小时。在shell中这是在2分钟内完成的,它是一个运行apache2的强大的linux服务器。那么这里有什么问题?我怎样才能提高PHP脚本的速度?PHP:创建.tar文件极其缓慢
$Path = str_replace('\\', '/', realpath($Path));
if (is_dir($Path) === true) {
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($Path), \RecursiveIteratorIterator::SELF_FIRST);
$archive = new \PharData('/var/www/test.tar');
foreach ($files as $file) {
$filePath = $file->getRealPath();
$archive->addFile($filePath);
}
答
RecursiveIteratorIterator非常慢...但问题是为什么你使用它?尝试Phardata buildFromDirectory功能:
<?php
...
if (is_dir($Path) === true) {
$archive = new PharData('/var/www/test.tar');
$archive->buildFromDirectory($Path);
}
...
?>
希望是帮助
+0
完美谢谢。 – 2014-10-06 12:06:12
是否有所作为,如果你使用'PharData :: buildFromIterator()'? – 2014-10-06 11:36:50
运行一些简单的性能分析:在getRealPath和addFile调用之后添加一个时间戳。 – 2014-10-06 11:37:10
如果命令行非常快,那么为什么不从PHP调用命令行呢? – 2014-10-06 11:45:45