Laravel 上的 jquery 文件上传验证错误

罗布斯平

当我上传图像时,在 laravel 5.5 上使用 jquery 上传文件我收到错误

POST http://localhost:8000/upload 500(内部服务器错误)

细节是

{,…}
exception
:
"ErrorException"
file
:
"I:\AppServ\www\comefour\app\Http\Controllers\UploadController.php"
line
:
8
message
:
"Declaration of App\Http\Controllers\UploadController::validate($uploaded_file, $file, $error, $index) should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)"
trace
:
[{file: "I:\AppServ\www\comefour\app\Http\Controllers\UploadController.php", line: 8,…},…]

我的控制器代码来自演示 UploadHandler.php

public function validate($uploaded_file, $file, $error, $index) {

    if ($error) {
        $file->error = $this->get_error_message($error);
        return false;
    }
    $content_length = $this->fix_integer_overflow(
        (int)$this->get_server_var('CONTENT_LENGTH')
    );
    $post_max_size = $this->get_config_bytes(ini_get('post_max_size'));
    if ($post_max_size && ($content_length > $post_max_size)) {
        $file->error = $this->get_error_message('post_max_size');
        return false;
    }
    if (!preg_match($this->options['accept_file_types'], $file->name)) {
        $file->error = $this->get_error_message('accept_file_types');
        return false;
    }
    if ($uploaded_file && is_uploaded_file($uploaded_file)) {
        $file_size = $this->get_file_size($uploaded_file);
    } else {
        $file_size = $content_length;
    }
    if ($this->options['max_file_size'] && (
            $file_size > $this->options['max_file_size'] ||
            $file->size > $this->options['max_file_size'])
        ) {
        $file->error = $this->get_error_message('max_file_size');
        return false;
    }
    if ($this->options['min_file_size'] &&
        $file_size < $this->options['min_file_size']) {
        $file->error = $this->get_error_message('min_file_size');
        return false;
    }
    if (is_int($this->options['max_number_of_files']) &&
            ($this->count_file_objects() >= $this->options['max_number_of_files']) &&
            // Ignore additional chunks of existing files:
            !is_file($this->get_upload_path($file->name))) {
        $file->error = $this->get_error_message('max_number_of_files');
        return false;
    }
    $max_width = @$this->options['max_width'];
    $max_height = @$this->options['max_height'];
    $min_width = @$this->options['min_width'];
    $min_height = @$this->options['min_height'];
    if (($max_width || $max_height || $min_width || $min_height)
       && preg_match($this->options['image_file_types'], $file->name)) {
        list($img_width, $img_height) = $this->get_image_size($uploaded_file);

        // If we are auto rotating the image by default, do the checks on
        // the correct orientation
        if (
            @$this->options['image_versions']['']['auto_orient'] &&
            function_exists('exif_read_data') &&
            ($exif = @exif_read_data($uploaded_file)) &&
            (((int) @$exif['Orientation']) >= 5)
        ) {
            $tmp = $img_width;
            $img_width = $img_height;
            $img_height = $tmp;
            unset($tmp);
        }

    }
    if (!empty($img_width)) {
        if ($max_width && $img_width > $max_width) {
            $file->error = $this->get_error_message('max_width');
            return false;
        }
        if ($max_height && $img_height > $max_height) {
            $file->error = $this->get_error_message('max_height');
            return false;
        }
        if ($min_width && $img_width < $min_width) {
            $file->error = $this->get_error_message('min_width');
            return false;
        }
        if ($min_height && $img_height < $min_height) {
            $file->error = $this->get_error_message('min_height');
            return false;
        }
    }
    return true;
}

它是否违反了laravel规则?我怎样才能改变它?

在下面的控制器列表中有另一个有效的句子

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
    $index = null, $content_range = null) {
    $file = new \stdClass();
    $file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error,
        $index, $content_range);
    $file->size = $this->fix_integer_overflow((int)$size);
    $file->type = $type;
    if ($this->validate($uploaded_file, $file, $error, $index)) {
        $this->handle_form_data($file, $index);
        $upload_dir = $this->get_upload_path();
        if (!is_dir($upload_dir)) {
            mkdir($upload_dir, $this->options['mkdir_mode'], true);
        }
        $file_path = $this->get_upload_path($file->name);
        $append_file = $content_range && is_file($file_path) &&
            $file->size > $this->get_file_size($file_path);
        if ($uploaded_file && is_uploaded_file($uploaded_file)) {
            // multipart/formdata uploads (POST method uploads)
            if ($append_file) {
                file_put_contents(
                    $file_path,
                    fopen($uploaded_file, 'r'),
                    FILE_APPEND
                );
            } else {
                move_uploaded_file($uploaded_file, $file_path);
            }
        } else {
            // Non-multipart uploads (PUT method support)
            file_put_contents(
                $file_path,
                fopen($this->options['input_stream'], 'r'),
                $append_file ? FILE_APPEND : 0
            );
        }
        $file_size = $this->get_file_size($file_path, $append_file);
        if ($file_size === $file->size) {
            $file->url = $this->get_download_url($file->name);
            if ($this->is_valid_image_file($file_path)) {
                $this->handle_image_file($file_path, $file);
            }
        } else {
            $file->size = $file_size;
            if (!$content_range && $this->options['discard_aborted_uploads']) {
                unlink($file_path);
                $file->error = $this->get_error_message('abort');
            }
        }
        $this->set_additional_file_properties($file);
    }
    return $file;
}

public function head() {
    $this->header('Pragma: no-cache');
    $this->header('Cache-Control: no-store, no-cache, must-revalidate');
    $this->header('Content-Disposition: inline; filename="files.json"');
    // Prevent Internet Explorer from MIME-sniffing the content-type:
    $this->header('X-Content-Type-Options: nosniff');
    if ($this->options['access_control_allow_origin']) {
        $this->send_access_control_headers();
    }
    $this->send_content_type_header();
}
开发者

您命名了您的validate()方法validate,它是控制器上的“保留”方法,它期望如下:

public function validate(Request $request, array $rules,
                         array $messages = [], array $customAttributes = [])

你可以在这里看到源代码

我建议您将该方法重命名为其他名称。validateFile也许?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel文件上传验证

来自分类Dev

验证jQuery文件上传预览

来自分类Dev

maxNumberOfFiles上的jquery文件上传警报

来自分类Dev

Laravel“在非对象上对成员函数getClientOriginalName()的文件上传错误调用”

来自分类Dev

Laravel错误:多个文件上传插入上的``preg_replace():参数不匹配''

来自分类Dev

引导jQuery文件上传和laravel

来自分类Dev

Laravel 5在有效文件类型上验证CSV文件错误

来自分类Dev

jQuery验证IE上的错误:[Object object]

来自分类Dev

jQuery验证上的不同错误消息

来自分类Dev

在 Laravel 上验证表单

来自分类Dev

行上的jQuery验证

来自分类Dev

在表单提交php上验证文件上传

来自分类Dev

Laravel 5.1文件上传验证

来自分类Dev

Laravel没有在服务器上上传实际文件

来自分类Dev

在xls文件上加载上传文件错误

来自分类Dev

在xls文件上加载上传文件错误

来自分类Dev

使用jQuery多次上传的文件大小验证

来自分类Dev

如何在jQuery上laravel加密

来自分类Dev

Laravel 5验证-表单元素上的错误类

来自分类Dev

视频文件上的Laravel验证不起作用

来自分类Dev

视频文件上的Laravel验证不起作用

来自分类Dev

jQuery的文件上传轨道“错误,空文件上传结果”

来自分类Dev

jQuery的文件上传轨道“错误,空文件上传结果”

来自分类Dev

laravel 5.2中的文件上传错误

来自分类Dev

Laravel 5.1文件上传错误

来自分类Dev

每个输入上的addClass验证jQuery中的错误

来自分类Dev

jQuery验证显示动态字段名称上的错误

来自分类Dev

每个输入上的addClass验证jQuery中的错误

来自分类Dev

静态表单上的 jQuery 验证