打开流失败:权限被拒绝-Codeigniter

时间:2019-09-11 13:25:23

标签: php codeigniter

我正在尝试使用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>

2 个答案:

答案 0 :(得分:0)

尝试一下,更改目录权限,

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
}

答案 1 :(得分:0)

 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

}