• missing xbfish.com image

Unlink in CodeIgniter

missing xbfish.com image

If you have problem using PHP unlink() function in CodeIgniter, I have a solution to it.

Below is the error you would most likely encounter:

A PHP Error was encountered
 
Severity: Warning
 
Message: unlink() [function.unlink]: http does not allow unlinking

Apparently, it seem that we can’t delete the file directly with absolute path in CodeIgniter. (Example of absolute path is http//:….)

So in order to resolve this, we have to do the following:

1
2
3
4
5
6
7
8
9
10
11
//define the root directory of CodeIgniter
$path = "images/" .$the_file_you_want_to_delete;
 
//from here, delete the file off using unlink
$result = unlink($path);
 
if($result){
   $data['status'] = "File has been deleted successfully!";
}else{
   $data['status'] = "There is a problem deleting the file!";
}

As you can see, unlinking the file using relative path works pretty fine and well.

2 Comments

  • July 19, 2009 - 1:06 am | Permalink

    Hi Willie,

    I know this is an old entry – but I’ve spent the last 30-40 mins trying to delete a file on a project I’m working. Unlink didn’t work so I ended up fiddling about with the file helper and the delete_files function until I stumbled on this entry. Thanks for the post mate – just found the inspiration for the solution to my problem.

  • mahrizal
    May 19, 2010 - 12:16 pm | Permalink

    thank’s your code works for me

    thanks so much

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

    Spam protection by WP Captcha-Free