| Author |
Message |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2958 Location: Earth
|
Posted: Wed Oct 17, 2007 4:26 am Post subject: deleting a file using php |
|
|
| Code: | <?php
$do = unlink("/home/user/public_html/file.html");
if($do=="1"){
echo "The file was deleted successfully.";
} else { echo "There was an error trying to delete the file."; }
?> |
i searched the net and found the above code for deleting the file. I have only one problem i cant seem to solve
I want to delete all the files that are called auctionnotsold.php and auctionwon.php which i ccan work out how to do to delete just those two. But there are several versions of them
auctionnotsold.php
auctionnotsold.php.1
auctionnotsold.php.10
auctionnotsold.php.100
and it goes on. How would i modify the above code to delete all files that are called auctionnotsold.php and auctionwon.php no matter what the .number is after them? _________________ Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG |
|
| Back to top |
|
| |
ClickFanatic Est. 2005

Joined: 18 Jan 2005 Posts: 3857
|
Posted: Wed Oct 17, 2007 7:36 am Post subject: |
|
|
You can retrieve an array of filepaths matching a certain pattern using the glob() function.
Then, for each item in this array, you would call the unlink() function.
Like so:
| Code: | <?php
// Note the use of * as a wildcard
$files = glob('/path/to/dir/auctionnotsold.php*');
foreach($files as $file)
{
if(unlink($file))
echo $file . ' has been deleted<br />';
else
echo 'Could not delete ' . $file . '<br />';
}
?> |
_________________ Captain Jell-O Buster from the Future
[img]http://feeds.feedburner.com/sparepencil.1.gif[/img] |
|
| Back to top |
|
| |
|
|
|