• missing xbfish.com image

Unlink in CakePHP

missing xbfish.com image
Posted on

unlink in CakePHP 1.3 doesn’t seem to work perfectly. You will always receive Warning error (2) due to “No such file or directories”.

Fret not! Allow me to introduce the class File in CakePHP 1.3

Apparently, little documentation, examples or tutorials have surfaced for this File Class. But, we shall make use of this to delete file/directories in CakePHP 1.3

For instance, you would like to delete butterfly.jpg under YOURWEBROOT/img folder. First, you construct a File object:

$file = new File(WWW_ROOT ."/img/butterfly.jpg");

As you can see, we make use of CakePHP WWW_ROOT to define the directory instead of using $this->webroot. This is because the file are store using system directories. By using webroot directories, CakePHP will output “No such file or directories” error again.

Example output of WWW_ROOT: E:\\xampp\\htdocs\\cake13\\app\\webroot\\
Example output of $this->webroot: \\cake13\\

Last but not least, to delete butterfly.jpg, we do the following:

if($file->delete()){
   echo "file deleted successfully";
}else{
   echo "file failed to be delete";
}

$file->delete() return true when the file has successfully been deleted. Hence, we check for successions of file deletion using the if statement as shown above.

I hope this tutorial helps ! :D

4 Comments

  • November 8, 2011 - 9:43 pm | Permalink

    Hey man, thanks for this instant info, it works! :)

  • nano
    February 8, 2012 - 5:11 pm | Permalink

    Thanks a lot. : )

  • Meka
    February 9, 2012 - 9:04 am | Permalink

    Nice job, I save a lot of time with this. Realy! thanks a lot! greetings from ARGENTINA. :)

  • 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