Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete multiple files from the folder

Tags:

php

how can i delete multiple files from the folder?

code:$query=$this->main_model->get($id);

if($query->num_rows()>0)
{
        foreach ($query->result() as $row)
        {
            unlink("uploads/".$id."/".$row->img_name);
            unlink("uploads/".$id."/".$row->img_name_tn);
            unlink("uploads/".$id."/".$row->file);
            unlink("uploads/".$id."/".$row->file2);
            unlink("uploads/".$id."/Thumbs.db");
        }
            rmdir("uploads/".$id);
}

here is the code i used but it delete the files at ones. and how can i delete all files from the folder?

like image 306
keyur Avatar asked Jan 20 '23 17:01

keyur


1 Answers

originally from php.net:

<?php
 $dir = '/uploads/';
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") { // strip the current and previous directory items
            unlink($dir . $file); // you can add some filters here, aswell, to filter datatypes, file, prefixes, suffixes, etc
        }
    }
    closedir($handle);
}
?>
like image 147
metaforce Avatar answered Jan 28 '23 11:01

metaforce