Akhil Biju
Akhil Biju

Reputation: 33

How to delete a file using PHP - Codeigniter

I am using CodeIgniter framework, and the column 'file' contains the URL of the file. And whenever I execute this I get this: Message: unlink() expects parameter 1 to be a valid path, object given. Help me solve this...

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

Upvotes: 2

Views: 623

Answers (1)

Abhishek Singh
Abhishek Singh

Reputation: 509

Problem is in your query this should be correct:

$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");
    }

Let me know if you face any issue.

Upvotes: 1

Related Questions