内容简介:笔者在使用由此可见在
笔者在 laravel5.1.*
中使用图片上传时 $request
无法解析 binary二进制图片
,并报错 mimeType: "application/octet-stream" -error: 6
使用 dd($request->all())
调试具体错误信息如下:
array:1 [
"image" => UploadedFile {#30
-test: false
-originalName: "1.jpg"
-mimeType: "application/octet-stream"
-size: 0
-error: 6
}
]
由此可见在 Illuminate\Http\Request
在解析 binary
就已经报错了
深入查看下有关 Request
包处理 file
代码,看到 error
状态码有以下几种情况:
define ('UPLOAD_ERR_OK', 0);
define ('UPLOAD_ERR_INI_SIZE', 1);
define ('UPLOAD_ERR_FORM_SIZE', 2);
define ('UPLOAD_ERR_PARTIAL', 3);
define ('UPLOAD_ERR_NO_FILE', 4);
define ('UPLOAD_ERR_NO_TMP_DIR', 6);
define ('UPLOAD_ERR_CANT_WRITE', 7);
define ('UPLOAD_ERR_EXTENSION', 8);
参照下,错误码为6对应为 UPLOAD_ERR_NO_TMP_DIR
即跟缓存目录有关
顺藤摸瓜,在 php.ini
配置文件中有个字段 upload_tmp_dir
允许设置上传文件临时目录
深究下 php 官网关于 upload_tmp_dir
的解释
upload_tmp_dir` string
The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default.
If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.
从这里我们可以得知两个信息:
-
如果
upload_tmp_dir为空则表示使用系统默认缓存目录,linux为/tmp -
如果
php.ini或者nginx fastcgi.conf中设置了open_basedir,那么也必须开放upload_tmp_dir目录
看到这里焕然大悟:因为笔者之前在服务器配置的时候遇到了一个问题 nginx搭建php提示No input file specified.
,当时在 fastcgi.conf
中设置了 open_basedir
条件满足,现在让我们来一步步解决它:
第一步:确认当前用户是否对 upload_tmp_dir
(linux默认为/tmp)有足够的访问权限
sudo chmod -R 777 /tmp
第二步:如果开启了 open_basedir
(笔者配置 fastcgi.conf
中),需一起开放 upload_tmp_dir
目录
fastcgi_param PHP_VALUE open_basedir=/opt/:/home/gitlab-runner/:/tmp/;
第三步:重启 php
与 nginx
(笔者服务器linux centos6.5)
service php-fpm restart service nginx restart
再次请求图片上传接口就不再提示错误了
好了,问题解决~
happy coding!
以上所述就是小编给大家介绍的《laravel5.1无法解析binary图片文件 error: 6》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
精通JavaScript+jQuery
曾顺 编著 / 人民邮电出版社 / 2008-9 / 59.00元
随着Ajax技术的不断风靡,其核心技术JavaScript越来越受到人们的关注,各种JavaScript的框架层出不穷。jQuery作为JavaScript框架的优秀代表,为广大开发者提供了诸多便利。 本书从介绍JavaScript的基础知识开始,围绕标准Web的各项技术予以展开,通过大量实例对JavaScript、CSS、DOM、Ajax等 Web关键技术进行深入浅出的分析,主要内容包括J......一起来看看 《精通JavaScript+jQuery》 这本书的介绍吧!