表單上傳文件,圖片可選可不選,使用request對象獲取所有圖片時(shí)報(bào)錯(cuò);報(bào)錯(cuò)信息如下:
$file = $request->file('img');
postman請求
按照手冊應(yīng)該返回null
實(shí)際報(bào)錯(cuò)信息為:
控制器中打印$request->file('img');
請求參數(shù)中包含file類型字段 img,img1,img未選擇圖片!
什么瀏覽器
從報(bào)錯(cuò)看是postman沒傳遞content-type數(shù)據(jù)導(dǎo)致的,我這沒復(fù)現(xiàn)這個(gè)問題,可能是我這postman版本和你的不一樣。
你先本地修改下vendor/workerman/workerman/Protocols/Http/Request.php
,將 parseUploadFile 方法里
$file = [
'name' => $match[2],
'tmp_name' => $tmp_file,
'size' => $size,
'error' => $error,
'type' => null,
];
改成
$file = [
'name' => $match[2],
'tmp_name' => $tmp_file,
'size' => $size,
'error' => $error,
'type' => '', // 這里null改成''
];
目前看這個(gè)問題可能只出現(xiàn)在某些版本的postman上,瀏覽器沒有問題,這個(gè)將在下個(gè)workerman版本做兼容。