Archive

Posts Tagged ‘unlink’

Unlink in CakePHP

July 9th, 2010

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

Willie Programming , , , ,