http://drupal.org/node/413744

What is the best way to do this?

What is the proper way to remove the field in CCK and FILEFIELD module?? If I have a node is the best way $node->field_file_attach[0] = NULL or do I need to delete from content_field_file_attach and do a file_delete() ?

It appears so... Is this right? I really don't want to do the db_query and file_delete() bu don't want stuff left over.

$node = node_load($item->nid);
$count = db_result(db_query('SELECT count(*) from {content_field_file_attach} where field_file_attach_fid = %d',
$node->field_file_attach[0]['fid']));
if ($count) {
print "Found the file : $count\n";
db_query("UPDATE {content_field_file_attach} SET field_file_attach_fid = NULL,
field_file_attach_list = NULL, field_file_attach_data = NULL WHERE field_file_attach_fid = %d", $node->field_file_attach[0]['fid']);
}
file_delete($node->field_file_attach[0]['filepath']);
$node->field_file_attach[0] = NULL;
$node->status = 1;
$node->promote = 0; // Display on front page?
$node->sticky = 0; // Display top of page?
$node->format = 1; // Filtered HTML?
$node->validated = 1;
node_save($node);

Comments

quicksketch’s picture

Title: See my question » What's the Proper Way to Delete a File in PHP

Close. What you have will work except that you should use the field_file_delete() function provided by FileField instead of file_delete(). file_delete() will delete the file itself but leave the database entry. field_file_delete() will check that the file is not used anywhere else, and if it isn't then it will delete both the file and the database entry in the files table.

billnbell’s picture

Do I need:

$node->field_file_attach[0] = NULL;
and node_save?

Also,

Replace file_delete($node->field_file_attach[0]['filepath']);

with the following?

field_file_delete($node->field_file_attach[0]);

billnbell’s picture

Maybe it should be:

field_file_delete($node->field_file_attach);
and $node->field_file_attach = NULL;

I have no idea.

quicksketch’s picture

The code you've used sort of is mixing approaches actually. You can either do it all manually (by calling field_file_delete() and deleting the row in content_field_file_attach), or you can do it through the node API, which would probably be the easier approach.

This should work, though I haven't tested it:

$nid = 10;
$node = node_load($nid);
$node->field_file_attach[0] = NULL;
$node->revision = 0;
node_save($node);
quicksketch’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

El Bandito’s picture

Status: Closed (fixed) » Active

Sorry to re-open this one but I'm unclear. Quicksketches code in #4 seems to imply that if I set the $node's filefield to NULL and then node_save the file itself will be deleted ?

Surely the slight correction of billnbell's suggestion in #3 is the correct solution .... or not ?

field_file_delete($node->field_file_attach[0]);
$node->field_file_attach[0] = NULL;

The call to field_file_delete() removes the database record from the files table and the actual file itself, and NULLing the node field removes the now-defunct field.

Any pointers ?

Thanks

quicksketch’s picture

Sorry to re-open this one but I'm unclear. Quicksketches code in #4 seems to imply that if I set the $node's filefield to NULL and then node_save the file itself will be deleted ?

Yep that's right. You don't need to delete the file or the record from the files table, that's all done for you.

El Bandito’s picture

Status: Active » Closed (fixed)

Thanks for the clarification and your efforts on this and other modules.

Issue closed.