上传的文件太大php [英] uploaded file is too big php

查看:64
本文介绍了上传的文件太大php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将视频上传到服务器并相应地更新数据库.当我用一堆图像和/或小视频运行它时,此代码可以正常工作.请参见下面的代码:

  for($ i = 0; $ i< count($ _ FILES ['images'] ['error']); $ i ++){如果($ _FILES ['images'] ['error'] [$ i] == UPLOAD_ERR_OK){$ tmpName = $ _FILES ['images'] ['tmp_name'] [$ i];$ name = $ _FILES ['images'] ['name'] [$ i];$ type = $ _FILES ['images'] ['type'] [$ i];如果(strpos($ type,'image')!== false){$ type ="img";} elseif(strpos($ type,'video')!== false){$ type ="vid";}别的{出口();}move_uploaded_file(($ tmpName),$ dir.$ name);$ upload =数组('name'=> $ name,'type'=> $ type);$ uploads [] = $ upload;}} 

但是,当我的客户端尝试上传大于64mb的视频时,程序没有上传该视频...我已经尝试更改max_file_size和其他参数以允许更大的文件.但是我的客户托管服务提供商不允许这样做.

还有其他方法可以通过自定义cms将大文件上传到服务器吗?

托马斯

解决方案

正如评论中所述.参考资料是下面的代码示例.技巧是将文件切成小于上传限制的块.此方法可以扩展到中断文件上载时可以继续到最后一个已知部分的程度.:-)

用于帮助上传文件的基本JavaScript类,确定要发送到PHP服务器的块.

  function fileUploader(){//选择文件时调用this.onFileSelected = function(){//读取文件输入(输入类型=文件")this.file = this.fileInput.files [0];this.file_size = this.file.size;this.chunk_size =(1024 * 1000);this.range_start = 0;this.range_end = this.chunk_size;this.slice_method ='slice';this.request = new XMLHttpRequest();//开始上传this.upload();};this.upload = function(){var self = this,大块//到达最后一部分如果(this.range_end> this.file_size){this.range_end = this.file_size;}//使用slice方法分块文件块= this.file [this.slice_method](this.range_start,this.range_end);//打开XMLHttpRequestvar Endpoint ="/url/to/php/server/for/processing";this.request.open('PUT',(endpoint));this.request.overrideMimeType('application/octet-stream');this.request.send(chunk);//确保我们同步进行以防止数据损坏this.request.onreadystatechange = function(){如果(self.request.readyState == XMLHttpRequest.DONE&& self.request.status == 200){self.onChunkComplete();}}};this.onChunkComplete = function(){如果(this.range_end === this.file_size){//我们完成了,停止上传返回;}this.range_start = this.range_end;this.range_end = this.range_start + this.chunk_size;this.upload();};} 

对于PHP位:

  ...$ out = fopen("{$ filePath} .partial","a +");fwrite($ out,file_get_contents("php://input"));fclose($ out);... 

此处有个重大警告,请确保正确验证并采取安全措施,以确保客户上传功能的安全.您正在将原始PHP输入写入文件.

上传完成后,您可以将文件重命名为其原始名称,包括正确的扩展名.

参考资料:
http://creativejs.com/tutorials/advanced-uploading-Techniques-part-1/index.html
https://secure.php.net/manual/en/wrappers.php.php

简而言之,它使用处理器将文件分成小块,使用常规方法上载文件(就像您通常上载文件一样),然后将输入追加到临时文件中.我遇到的一些陷阱是向端点发送了额外的参数等,请避免将这些参数附加到文件中,否则会损坏您的文件.

Hi i have the following code that uploads videos to a server and updates the database accordingly. This code works fine when i run it with a bunch of images and or small video's. See the code below:

for ($i=0; $i<count($_FILES['images']['error']); $i++) {
   if ($_FILES['images']['error'][$i] == UPLOAD_ERR_OK) {
       $tmpName = $_FILES['images']['tmp_name'][$i];
       $name = $_FILES['images']['name'][$i];
       $type = $_FILES['images']['type'][$i];
       if (strpos($type, 'image') !== false) {
           $type = "img";
       }elseif(strpos($type, 'video') !== false){
           $type = "vid";
       }else{
           exit();
       }
       move_uploaded_file(($tmpName), $dir.$name);
       $upload = array(
           'name'=>$name,
           'type'=>$type
       );
       $uploads[] = $upload;
   }
}

But when my client tries to upload a video bigger than 64mb the program doesnt upload it... I already tried to change the max_file_size and other according parameters to allow bigger files. But my clients hosting provider doesnt allow this.

So are there any other ways of uploading big files to my server via my custom cms?

Thomas

解决方案

So as said in comments. Reference material is below code examples. Trick is to cut the file into chunks that are less than the upload limit. This method can be extended to the point that when a file upload is interrupted you can continu on the last known part. :-)

Basic JavaScript class to assist in uploading the file, determines the chunks to be sent to a PHP server.

function fileUploader() {
    // Called when the file is selected
    this.onFileSelected = function() {
        // Read file input (input type="file")
        this.file = this.fileInput.files[0];
        this.file_size = this.file.size;
        this.chunk_size = (1024 * 1000);

        this.range_start = 0;
        this.range_end = this.chunk_size;

        this.slice_method = 'slice';
        this.request = new XMLHttpRequest();

        // Start uploading
        this.upload();
    };

    this.upload = function()
    {
        var self = this,
            chunk;

        // Last part reached
        if (this.range_end > this.file_size) {
            this.range_end = this.file_size;
        }

        // Chunk the file using the slice method
        chunk = this.file[this.slice_method](this.range_start, this.range_end);

        // Open a XMLHttpRequest
        var endpoint = "/url/to/php/server/for/processing";
        this.request.open('PUT', (endpoint));
        this.request.overrideMimeType('application/octet-stream');
        this.request.send(chunk);

        // Make sure we do it synchronously to prevent data corruption
        this.request.onreadystatechange = function()
        {
            if (self.request.readyState == XMLHttpRequest.DONE && self.request.status == 200) {
                self.onChunkComplete();
            }
        }
    };

    this.onChunkComplete = function()
    {
        if (this.range_end === this.file_size)
        {
            // We are done, stop uploading
            return;
        }

        this.range_start = this.range_end;
        this.range_end = this.range_start + this.chunk_size;
        this.upload();
    };
}

And for the PHP bit:

...  
$out = fopen("{$filePath}.partial", "a+");                      
fwrite($out, file_get_contents("php://input"));
fclose($out);
...

Big warning here, make sure to properly validate and take security measures to ensure the safety of your clients upload function. You are writing the raw PHP input to a file.

When the upload is done you can rename the file to it's original name including the correct extension.

Reference material:
http://creativejs.com/tutorials/advanced-uploading-techniques-part-1/index.html
https://secure.php.net/manual/en/wrappers.php.php

In a nutshell.. it's break the file into small chunks using a processor, upload the files using conventional methods (like you would normally upload a file), append the input to a temporarily file. Some pitfalls I encountered were sending extra params and alike to the endpoint, avoid those as it's appended to the file and it will corrupt your file.

这篇关于上传的文件太大php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆