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 calls to file_delete()

2 string references to 'file_delete'

File

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

Code

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