Reputation: 553
I have a script where when I press delete it deletes information in the database according to the ID passed, but there is a file name in the database as well that is shows where the file was uploaded, how would I pass the filename from the database into codeigniter to see if the file exists and if it does, delete it.
Also there will be multiple rows on occasions with different file names where I would need it to delete all the file it returns.
I am a beginner so you will need to explain things more than usual, I don't even know where to begin for this, I searched around Google and here for about an hour or so with no luck.
Upvotes: 2
Views: 8247
Reputation: 4611
What you're trying to do seems to be explained in this tutorial - go to the "Deleting Files" section towards the bottom of the page:
http://code.tutsplus.com/tutorials/creating-a-file-hosting-site-with-codeigniter--net-3534
Upvotes: 0
Reputation: 16510
Without knowing your database schema, the only suggestion I have for pulling it out is to use the get_where
helper in the database library to retrieve the record in question. Once you have it, you can check and delete it using file_exists
and unlink
. I'll assume the filepaths you wish to delete are in an array called $results
foreach ($results as $path) {
if (file_exists($path)) {
unlink($path) or die('failed deleting: ' . $path);
}
}
As a general matter of course, it's very helpful to see relevant pieces of your code before trying to answer your question. I hope the example above is enough to get you started!
Upvotes: 8