合并文件块PHP

时间:2019-05-07 15:07:36

标签: php

我已经实现了一个库(Dropzone.js),可以将大文件从我的应用程序上传到服务器(分为5 Mb的片段),并且运行良好。

如果我想从服务器下载文件。如何使用PHP进行拼凑?

enter image description here

(上传的文件并非总是.rar,它可以是任何类型的文件)

我尝试这样的事情。

<?php

$target_path = 'upload/';
$directory = opendir($target_path); //get all files in the path
$files = array() ;
$c =0;
while ($archivo = readdir($directory)) //
{
    if (is_dir($archivo))//check whether or not it is a directory
    {

    }
    else
    {
        $files= $target_path.$archivo;
        $c++;
    }
}

$final_file_path =$target_path;
$catCmd = "cat " . implode(" ", $files) . " > " . $final_file_path;
exec($catCmd);
?>

1 个答案:

答案 0 :(得分:3)

您的主要问题是您需要构建一个数组,但是每次迭代都覆盖$files,所以:

$files[] = $target_path.$archivo;

但是,您可以使其更短:

$target_path = 'upload';
$files = array_filter(glob("$target_path/*"), 'is_file');
$catCmd = "cat " . implode(" ", $files) . " > $target_path/NEW";
exec($catCmd);
  • glob用于目录中的所有文件
  • 仅过滤is_file
  • 的条目
  • implode并照常执行
  • 指定新文件的名称,我使用了NEW