dow57588 2014-07-19 05:09
浏览 25

使用Drop Zone格式化laravel 4错误?

EDIT:

I have managed to solve the first issues with wrong file type. By changing the if statement to:

if ($v->fails())
    {
        return Response::json('error', 400);
    }

I was able to return the error for the any non image file type. However, I still get the below problem for anything greater than my size limit.

I have the following code that uploads a photo to my server using Dropzone.js and laravel 4.

public function upload()
{
    $file = Input::file('file');
    $input = Input::all();


$rules = array
(
    'file' => 'mimes:jpeg,bmp,png,gif | Max:2048'
);


$v = Validator::make($input, $rules);

if( $v->passes() ) 
{

    $l = 0; 
    $dir = 'photos';
    if ($handle = opendir($dir)) {
        while (($file = readdir($handle)) !== false){
        if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
            $l++;
        }
    }


    $dir = 'photos/' . $l;
    $i = 0;
    if ($handle = opendir($dir)) {
        while (($file = readdir($handle)) !== false){
        if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
            $i++;
        }
    }

    if($i >= 10000)
        {   
            $l++;
            $myDir = 'photos/' . $l;
            mkdir($myDir);

            $destinationPath = $myDir;
            $i = 0;
        }
    else
        {
            $destinationPath = 'photos/' . $l;
        }

    public_path().'/uploads' 
    $filename = $i + 1 . ".jpg";
    $upload_success = Input::file('file')->move($destinationPath, $filename);

    if( $upload_success ) 
    {
        return 10;
    } 
    else 
    {
        return 20;
    }
}

else
{
    return Redirect::to('upload')->withErrors($v);
}

}

Now the code works fine, it successfully uploads the photos as intended, however I can't seem to get it to show errors correctly.

As shown in my code, I have two validation rules one for size and one for file type. Each one of these have their own issues.

If I upload an incorrect file type I get no error back. If I upload too large of a file I do get an error, but it seems to be an error to do with the laravel config as opposed to my validation rules:

{"error":{"type":"Symfony\\Component\\HttpFoundation\\File\\Exception\\FileException","message":"The file \"Window quotes LP.psd\" exceeds your upload_max_filesize ini directive (limit is 2048 kb).","file":"C:\\wamp\\www\eal_estate\\vendor\\symfony\\http-foundation\\Symfony\\Component\\HttpFoundation\\File\\UploadedFile.php","line":255}}

So my question is how do I get these errors to work with the validation? I don't want to display that long error message to end user, so how do I both fix it so my validation errors work, as well as not allow that default error to come through?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题