【问题标题】:failed to open stream: Permission denied - Codeigniter无法打开流:权限被拒绝 - Codeigniter
【发布时间】:2019-09-11 13:25:23
【问题描述】:

我正在尝试使用 file_put_contents 通过 REST API 上传文件(图像)。但是,它给出了错误:- 无法打开流:权限被拒绝。

我已经解决了这里的大部分问题并尝试了几乎所有事情

  • 将所有者更改为 root 或 apache
  • 将组更改为 root 或 apache
  • 将权限更改为 755 或 777
  • 还有其他选项

我仍然不确定出了什么问题,需要一些帮助。

这是我上传文件的代码。

public function tryImage_post(){
        $image = base64_decode($this->input->post("img_front"));
        $image_name = md5(uniqid(rand(), true));
        $filename = $image_name . '.' . 'png';
        //rename file name with random number
        $path = FCPATH."img/".$filename;
        //image uploading folder path
        file_put_contents($path . $filename, $image);
        // image is bind and upload to respective folde
    }

错误信息:-

<p>Message:  file_put_contents(/var/www/html/development/img/8f2e7959de8beb89ba5e304a6e69d894.png8f2e7959de8beb89ba5e304a6e69d894.png): failed to open stream: Permission denied</p>

【问题讨论】:

  • 文件名出现两次,是故意的吗? /var/www/html/development/img/ 是否存在并具有正确的权限?

标签: php codeigniter


【解决方案1】:

试试这个,改变目录权限,

chmod -R 777 var/www/html/development/img

之后,更改您的代码,如下所示。

public function tryImage_post(){
    $image = base64_decode($this->input->post("img_front"));
    $image_name = md5(uniqid(rand(), true));
    $filename = $image_name . '.' . 'png';
    //rename file name with random number
    $path = FCPATH."img/".$filename;
    //image uploading folder path
    file_put_contents($path, $image);
    // image is bind and upload to respective folde
}

【讨论】:

    【解决方案2】:
     public function tryImage_post(){
        $image = base64_decode($this->input->post("img_front"));
        $image_name = md5(uniqid(rand(), true));
        $filename = $image_name . '.' . 'png';
        //rename file name with random number
        file_put_contents("img/".$filename, $image);
         //image uploading folder path
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2011-06-22
    • 2015-12-16
    • 2015-11-15
    • 2017-09-23
    • 2015-02-01
    相关资源
    最近更新 更多