This is a PHP function delete a specified file when it becomes unnecessary such as temporary file, log file, back up file, old file, etc.
1. PHP Delete File Function
<?php function delete_file( $dirname, $file ) { if ( $file != '.' && $file != '..' ) { if ( !is_dir( $dirname.'/'.$file ) && file_exists($dirname.'/'.$file) ) { unlink( $dirname.'/'.$file ); } return true; } } ?> |
2. Usage Delete File Function
<?php delete_file("D:/dev/web/htdocs/4rapiddev/demo/_tmp", "access.log"); delete_file("D:/dev/web/htdocs/4rapiddev/demo", "background.png"); ?> |
Note:
- 1. The file has to be writable or in a writable folder/directory
- 2. We may check whether the file exists before deleting
- 3. Would be great if combine this function with Delete Directory function