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

时间:2018-04-22 04:41:08

标签: php laravel validation post jquery-file-upload

在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();
}

1 个答案:

答案 0 :(得分:2)

您将validate()方法validate命名为&#34;保留&#34;控制器上的方法,完全符合这个:

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

您可以看到来源here

我建议你将方法重命名为其他方法。可能validateFile