处理Laravel 4上传大文件异常

 忧伤玫瑰coco_873 发布于 2023-02-06 01:36

我的目标是管理最大上传文件异常,并显示客户端友好消息,但我不知道控制它的最佳位置在哪里.这是我的控制器方法:

public function upload_file()
    {
        if (!Input::hasFile('file'))
            return;

        $utils = App::make('utils');
        $file = Input::file('file');

        $name = Input::get('name');
        $size = $file->getSize();

        if ($size > FileModel::$max_file_size)
            return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %smb.',FileModel::$max_file_size/1000000)));

        $original_file_name = $file->getClientOriginalName();

        $destination_directory = "";

        $final_file_name = $utils->copy_file_to_location($file);

        return json_encode(array('success'=>true, 'file'=>$original_file_name));
    }

这是utils copy_file_to_location方法:

public function copy_file_to_location($file, $destination_directory = "")
    {
        if (!isset($file))
            return;
        $file_name = time()."_".$file->getClientOriginalName();

        $file->move(app_path().'/storage/files/'.$destination_directory, $file_name);
        return $file_name;
    }

我不知道在哪里处理上传文件大小超过服务器最大上传文件大小变量的文件时引发的异常.我应该在何处以及如何处理此消息以显示用户友好的消息,并且不要锁定用户界面.顺便说一下,我在客户端使用ExtJs 4.谢谢.


编辑


我发现了一个相关的问题很有帮助(这是同样的问题),但我需要知道在Laravel里面我应该检查一下这个问题.

1 个回答
  • 有两种情况:文件大小大于php变量upload_max_filesize,第二种是大于post_max_size变量的大小.在第一个例子中引发了一个例外,因此它很容易捕获它.在第二种情况下,没有例外,我用这个问题来解决它.

    现在,检查此代码:在Laravel控制器aciton方法中.我认为控制器动作中的代码从未被执行过,但我错了.所以最后这是一种解决这个问题的方法:

        public function upload_file()
        {
            $file_max = ini_get('upload_max_filesize');
            $file_max_str_leng = strlen($file_max);
            $file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1);
            $file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades'));
            $file_max = substr($file_max,0,$file_max_str_leng - 1);
            $file_max = intval($file_max);
    
            //handle second case
            if((empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'))
            { //catch file overload error...
                 //grab the size limits...
                return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit)));
            }
    
            try{
    
                if (!Input::hasFile('file'))
                    return;
    
                $utils = App::make('utils');
                $file = Input::file('file');
    
                $name = Input::get('name');
                $size = $file->getSize();
    
                if ($size > $file_max)
                    return json_encode(array('success'=>false, 'message'=>sprintf('El tamaño del archivo debe ser menor que %smb.',$file_max)));
    
                $original_file_name = $file->getClientOriginalName();
    
                $destination_directory = "";
    
                $final_file_name = $utils->copy_file_to_location($file);       
    
                return json_encode(array('success'=>true, 'file'=>$original_file_name));
            }
            catch (Exception $e)
            {
                //handle first case
                return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit)));
            }
        } 
    

    2023-02-06 02:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有