在服务器上上传大文件的最佳方法


Best method of uploading large files on server

我进行了一次基于ajax的多文件上传,效果很好。但问题是,我必须上传非常大的文件,即每个文件最多200 MB,同时可能是20-25个相同大小的文件。文件正在上传,但上传需要很长时间。

我在php.ini设置中更改了几件事

post_max_size 10G
upload_max_size 10G
max_execution_time 3600
memory_limit -1

那么,处理这种执行速度快的文件上传的最佳解决方案是什么呢。

我的互联网连接是100 MB/S,上传速度是20 MB/S。

请给我一些好的解决方案。

根据上传的文件类型,您可以尝试将压缩文件发送到服务器,并在上传完成后将其解压缩。

您可以在php中看到更多关于zip函数的信息。

压缩一个文件:来源:http://davidwalsh.name/create-zip-php

    /* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        //close the zip -- done!
        $zip->close();
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}

解压缩服务器中的文件:

<?php
$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
  $zip->extractTo('/myzips/extract_path/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}
?>

从应用程序的角度来看,您唯一能做的就是限制同时上传文件。扩展ajax多文件上传脚本,一次只上传一个文件。这可能会稍微加快速度。

但是,您的问题可能不是由应用程序本身引起的,而是由服务器的网络速度或磁盘写入速度引起的。一些VPS提供程序还限制了每秒磁盘写入操作的次数。因此,最好的办法可能是将您的应用程序迁移到另一台具有更好网络速度和性能的服务器。:)

最好的方法是使用旧的好FTP和一些FTP客户端,例如FileZilla