laravel5.1无法解析binary图片文件 error: 6

栏目: PHP · 发布时间: 4年前

内容简介:笔者在使用由此可见在

笔者在 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.

从这里我们可以得知两个信息:

  1. 如果 upload_tmp_dir 为空则表示使用系统默认缓存目录, linux/tmp
  2. 如果 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/;

第三步:重启 phpnginx (笔者服务器linux centos6.5)

service php-fpm restart
service nginx restart

再次请求图片上传接口就不再提示错误了

好了,问题解决~

happy coding!

以上所述就是小编给大家介绍的《laravel5.1无法解析binary图片文件 error: 6》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Android编程权威指南(第2版)

Android编程权威指南(第2版)

Bill Phillips、Chris Stewart、Brian Hardy、Kristin Marsicano / 王明发 / 人民邮电出版社 / 2016-5 / 109.00 元

Big Nerd Ranch是美国一家专业的移动开发技术培训机构。本书主要以其Android训练营教学课程为基础,融合了几位作者多年的心得体会,是一本完全面向实战的Android编程权威指南。全书共34章,详细介绍了8个Android 应用。通过这些精心设计的应用,读者可掌握很多重要的理论知识和开发技巧,获得最前沿的开发经验。 如果你熟悉Java语言,或者了解面向对象编程,那就立刻开始And......一起来看看 《Android编程权威指南(第2版)》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具