gpt4 book ai didi

javascript - Laravel - 如何验证超过 upload_max_filesize 指令的图像的最大大小

转载 作者:行者123 更新时间:2023-12-02 15:44:56 25 4
gpt4 key购买 nike

我正在使用ajax请求将图像发送到服务器并立即返回一些数据。 (用于测试)

return [
'photo' => Request::file('avatar'),
'hasFile' => Request::hasFile('avatar'),
'extension' => Request::file('avatar')->getClientOriginalExtension(),
'size' => Request::file('avatar')->getClientSize(),
'maxFileSize' => Request::file('avatar')->getMaxFilesize()
];

当图像小于upload_max_filesize指令时,我有以下响应:

{"photo":{},"hasFile":true,"extension":"jpg","size":900498,"maxFileSize":2097152}

当图像大于upload_max_filesize指令时,我有以下响应:

{"photo":{},"hasFile":false,"extension":"jpg","size":0,"maxFileSize":2097152}

查看上次响应的大小。我如何验证它?

最佳答案

您需要使用 size 而不是 max 来验证文件大小。最大值:值

Blockquote The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule So we go to the Size rule:

大小:值

Blockquote The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.

获取文件大小

$filesize = Input::file('photo')->getSize();

推送待验证数据中的文件大小

$data['filesize'] = $filesize;

你的规则应该是这样的 $rules = ['filesize' => 'size:最大大小(以 KB 为单位)'];

当文件大小== false时,您也可以在调用表单验证之前执行此操作

 $file_size    Input::file('avatar')->getSize() // false
if ($filesize){
// continue
}

我认为这种方法要容易得多。

希望这有帮助。

关于javascript - Laravel - 如何验证超过 upload_max_filesize 指令的图像的最大大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32282413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com