Delete All Files from Given Folder – PHP Script

by Hiroshi on January 12, 2013

in File Manipulation

Actually I was having hard time in setting up a database backup script. It was using a function and it was not being controlled easily. If I implemented a simple check that if form was posted then run that db-backup script, it was simply giving call to undefined function. By reloading page backup copies are being created. So I had to control it. What I did was, use this script that deletes all data from backup folder and then after that I placed db-backup script, so I succeeded in creating single fresh database backup when I launched that page. Here it is:

Read contents of folder and delete all files from it.

//Delete All files from folder
//backups is our folder, note the path backups/*
$files = glob('backups/*'); // get all file names
foreach($files as $file){ // iterate files
unlink($file); // delete file

Related Posts

Previous post:

Next post: