简体   繁体   中英

jquery file upload validate error on laravel

use the jquery upload file on laravel 5.5 when I upload a image I got the error

POST http://localhost:8000/upload 500 (Internal Server Error)

the detail is

{,…}
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,…},…]

my controller code is from the demo 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;
}

Does it violate laravel rule? How can I change it?

Have another validte sentence on the controller list below

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

You named your validate() method validate , which is a "reserved" method on controllers, that expects exactly this:

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

You can see the source here .

I suggest you rename the method to something else. validateFile maybe?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM