1

I have a problem with unlink or delete file with codeigniter

Here is my controller

function delete($id_post=''){
    $this->home_model->delete($id_post);
    $this->session->set_flashdata('danger', "Your photo has been deleted...");
    redirect("home");
}

And this is my model

function delete($id_post=''){
    $file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='$id_post'");
    unlink(base_url("uploads/" . $file_name));
    $sql  = "DELETE FROM post WHERE id_post=?";
    $outp = $this->db->query($sql,array($id_post));
}

Doc is the name of column that contain image.

If i click button delete, it delete a data in database and works successfully but not the image in file folder. I want, when i delete a data it is also delete image in file folder uploads. Uploads folder is in the root aplication system.

Any answer?

Many thanks...

Andhika R.K.
  • 426
  • 1
  • 11
  • 30

7 Answers7

0

This is because you does not fetch your result resource. Use:

$query = $this->db->query("SELECT doc FROM post WHERE post.id_post='$id_post'");
$row = $query->result();
$file_name = str_replace('localhost/latihan/uploads/','',$this->db->query("SELECT doc FROM post WHERE post.id_post='".$id_post."'")->row()->doc);
unlink("uploads/" . $file_name);
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • it returning an error like this Fatal error: Cannot use object of type stdClass as array, because $row maybe? – Andhika R.K. Dec 30 '14 at 12:53
  • `var_dump($row);` Whats inside it? – vaso123 Dec 30 '14 at 12:57
  • `Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\latihan\application\modules\home\models\home_model.php on line 241`... The line 241 is `$file_name = $row[0]['doc'];` – Andhika R.K. Dec 30 '14 at 13:02
  • Errr.. And what if `$row['doc']` only, not `$row[0]['doc']`? – vaso123 Dec 30 '14 at 13:02
  • Oh, I see now, somebody edited my code. Please after `$file_name = ....` says a `var_dump($file_name);` and show me the result. – vaso123 Dec 30 '14 at 13:05
  • $file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='".$id_post."'")->row()->doc; – Arun Dec 30 '14 at 13:09
  • i add `var_dump($file_name)` below `$file_name=` but it still not delete image file – Andhika R.K. Dec 30 '14 at 13:13
  • Ok, I've just want to know, what is the output of the `var_dump` Please show me that. `var_dump` is just dump out the content of variable. – vaso123 Dec 30 '14 at 13:13
  • Just right after `$file_name = ....`. It will output anywhere. `var_dump($file_name); die();` It will show, whats in the `$file_name` and terminate the script. This is how should debug on the output. – vaso123 Dec 30 '14 at 13:23
  • The Output is `Message: Undefined index: doc And NULL` – Andhika R.K. Dec 30 '14 at 13:25
  • Ok, so this means, we did not fetch the query. Try this, what @Arun sad, and replace the `$file_name = ...` line buy this: `$file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='".$id_post."'")->row()->doc;` And now tell me, what `var_dump` says. – vaso123 Dec 30 '14 at 13:28
  • So now you are close. I think, now just you neeed to replace the `'localhost/latihan/uploads/'` to nothing, and unlink the file in the uploads directory. From here, I think, now you can figure out. remove the `var_dump` and `die` – vaso123 Dec 30 '14 at 13:37
  • And do not add URL to the file, when you want to delete it, you can delete it on filesystem, not through the web. – vaso123 Dec 30 '14 at 13:39
0

Try this:

$this->load->helper("url");
unlink(base_url("uploads/" . $file_name));
user3111011
  • 145
  • 1
  • 9
0

The query() function returns a database result object. So replace your model code with:

function delete($id_post=''){
    $file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='$id_post'");
    $row = $file_name->row();
    $file_name = $row['doc'];
    unlink(base_url("uploads/" . $file_name));
    $sql  = "DELETE FROM post WHERE id_post=?";
    $outp = $this->db->query($sql,array($id_post));
}
amit
  • 874
  • 7
  • 16
0

Instead of this

$file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='$id_post'");

please replace this

$file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='".$id_post."'")->row()->doc;
Arun
  • 750
  • 5
  • 12
0
function delete($id_post=''){
    $file_name = $this->db->query("SELECT doc FROM post WHERE post.id_post='$id_post'");

    $res = $this->db->result();  // this returns an object of all results
    $row = $res[0];            
    $filename = $row['doc']; // get the file name from array
    // do the delete after getting the filename
}
amit
  • 874
  • 7
  • 16
Kiren S
  • 3,037
  • 7
  • 41
  • 69
0

can use this using directory path

unlink(FCPATH.'uploads/yourfile');
adiga
  • 34,372
  • 9
  • 61
  • 83
Zohaib Yunis
  • 376
  • 5
  • 11
-1

You are trying to delete your file with URL address you have to specifiy local document address like this:

unlink($_SERVER['DOCUMENT_ROOT']."uploads/".$filename);

here is similiar question:

Cannot unlink file in Codeigniter

Community
  • 1
  • 1
mirza
  • 5,685
  • 10
  • 43
  • 73