如何使用PHP删除文件 - Codeigniter

时间:2017-05-17 18:08:59

标签: php codeigniter

我使用CodeIgniter框架,列'file'包含文件的URL。每当我执行此操作时,我都会得到: 消息:unlink()期望参数1是有效路径,给定对象。 帮我解决这个问题......

$this->db->select('file');
$this->db->from('images');
$imageurl=$this->db->where('id',$id);
unlink($imageurl) or die("Couldn't delete file");

1 个答案:

答案 0 :(得分:1)

问题出在您的查询中,这应该是正确的:

$this->db->select('file');
$this->db->from('images');
$this->db->where('id',$id);
$resSQL = $this->db->get();
if ($resSQL->num_rows() > 0) {
        $resultRaw = $resSQL->result_array();
        $result = $resultRaw[0];
        $imageurl = $result['imageurl'];

        // Also for unlink any file you need to pass the relative path of that file like: /folder/sub-folder/file-name So, in this case if you are storing image url in database then use str_replace to replace some part of string.
           unlink($imageurl) or die("Couldn't delete file");
    }

如果您遇到任何问题,请告诉我。