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

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

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

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

查看所有标签

猜你喜欢:

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

失控的未来

失控的未来

[美]约翰·C·黑文斯 / 仝琳 / 中信出版集团 / 2017-4-1 / 59.00元

【编辑推荐】 20年前,尼古拉•尼葛洛庞帝的《数字化生存》描绘了数字科技给人们的工作、生活、教育和娱乐带来的冲击和各种值得思考的问题。数字化生存是一种社会生存状态,即以数字化形式显现的存在状态。20年后,本书以一种畅想的形式,展望了未来智能机器人与人类工作、生活紧密相联的场景。作者尤其对智能机器人与人类的关系,通过假设的场景进行了大胆有趣的描述,提出了人工智能的未来可能会面临的一些问题。黑文......一起来看看 《失控的未来》 这本书的介绍吧!

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

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器