Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

如何以Magento 2方式使用$ _FILES?

$_FILES在上传文件时使用过,但magento-connect不允许使用全局变量,它会在验证报告中产生错误:

Direct use of $_FILES Superglobal detected

如何解决此错误?

解决方案

你必须使用

$uploader = $this->_objectManager->create(
  'Magento\MediaStorage\Model\File\Uploader',
  ['fileId' => 'my_file_uploader']
);

代替$ _FILES,并且这样更好,您可以访问对象中的各种方法。其中fileId的值是需要上传文件的name值,如 <input id="file" type="file" name="file"/>

检查输出,请使用:

 var_dump($uploader->validateFile());

这将返回一个数组,其中包含有关上载文件的详细信息

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magento2教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论