This topic is locked

Deleting a Record's Associated Files?

2/7/2007 8:03:47 PM
PHPRunner General questions
J
jasteele12 author

Hello,
I have a record something like this:
Listing int(11),

File1 varchar(255),

...

File12 varchar(255)
These fields correspond to up to 12 files (some quite weird how they wanted them) stored in various places in the filesystem. I need to delete these when the record is deleted from the listing page.
Which event function should I use (BeforeDelete, AfterDelete), and does anyone have some sample code to share? I'm thinking something like this but I don't want to mess this up:

function BeforeDelete(&$values) {
$listing = $values['Listing'];

@unlink('./folder1/'. $listing. '_F1.pdf');

@unlink('./folder2/'. $values['File2']);

// ...

@unlink('./document/'. $listing. '_F12.doc');
return true;

}


Thank you in advance for any help!

John

J
Jane 2/8/2007

John,
you can just check off Delete file when associated record is deleted (global option) on the "Edit as" settings dialog on the Visual Editor tab and files will be removed automatically.

J
jasteele12 author 2/14/2007

John,

you can just check off Delete file when associated record is deleted (global option) on the "Edit as" settings dialog on the Visual Editor tab and files will be removed automatically.


Hi Jane,
Thanks for the response, but the only contents of the fields are the base filenames, not including the paths.
I would have done it differently, but I inherited an existing DB that I couldn't change (tied to many Excel spreadsheets).
I have no control over their upload form (which is very complicated/strange), but the base filenames end up in the db (MySQL). Not to mention the weird paths to these uploads (which aren't all pictures either).
So my question still stands, BeforeDelete or AfterDelete and what would the code look like?
Thanks, John

J
Jane 2/14/2007

John,
use BeforeDelete event and unlink function for this purpose.

Here is the article about unlink function:

http://ru.php.net/manual/en/function.unlink.php