Community Documentation

file_delete

5 file.inc file_delete($path)
6 file.inc file_delete($path)
7 file.inc file_delete(stdClass $file, $force = FALSE)
8 file.inc file_delete(stdClass $file, $force = FALSE)

Delete a file.

Parameters

$path A string containing a file path.:

Return value

TRUE for success, FALSE for failure.

Related topics

▾ 9 functions call file_delete()

file_move in includes/file.inc
Moves a file to a new location.
locale_uninstall in modules/locale/locale.install
Implementation of hook_uninstall().
system_cron in modules/system/system.module
Implementation of hook_cron().
upload_delete in modules/upload/upload.module
upload_delete_revision in modules/upload/upload.module
upload_save in modules/upload/upload.module
user_validate_picture in modules/user/user.module
_locale_rebuild_js in includes/locale.inc
(Re-)Creates the JavaScript translation file for a language.
_user_edit_submit in modules/user/user.module

File

includes/file.inc, line 480
API for handling file uploads and server file management.

Code

<?php
function file_delete($path) {
  if (is_file($path)) {
    return unlink($path);
  }
}
?>

Comments

This function is incomplete!

When deleting a file with this function, the information stored on the database that is related to this file is left behind! It should be part of the function the cleaning up of the "files" table on the database.

For those that want to do the cleaning up themselves, here is how:

<?php
 
 
//You should use single quotes here because the path has backslashes!
 
$path = 'c:\windows\temp/test.txt';
 
 
//Try to delete the file
 
$success = file_delete($path);
 
 
//If the file was successfully deleted, update the database
 
if ($success){

   
db_query('DELETE FROM {files} WHERE filepath = "%s"', $path);
  }
 
?>

Still incomplete

Removing a row from the files table is a good start, but how about all the other tables that will have a foreign key pointing to files.fid? (For starters, any CCK filefield.)

Login or register to post comments