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

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

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

笔者在 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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

Vim实用技巧

Vim实用技巧

[英] Drew Neil / 杨源、车文隆 / 人民邮电出版社 / 2014-5-1 / 59.00元

vim是一款功能丰富而强大的文本编辑器,其代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中得到非常广泛的使用。vim能够大大提高程序员的工作效率。对于vim高手来说,vim能以与思考同步的速度编辑文本。同时,学习和熟练使用vim又有一定的难度。 《vim实用技巧》为那些想要提升自己的程序员编写,阅读本书是熟练地掌握高超的vim技巧的必由之路。全书共21章,包括121个技巧。每一章......一起来看看 《Vim实用技巧》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

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

正则表达式在线测试