Like Us on Facebook

How to Delete all files in a Folder using PHP

This post is about “How to Delete all files in a Folder using PHP”. Some we have seen many useless file in our web server. And they need to free, free for web space. You can delete these file very easily using php. You can delete a particular file in a folder or you can also delete all files in a folder, its upto you. After delete, web server space will be increase. So this is very important to free your server space and also need to delete useless files.

So this tutorial will be very useful for you. You have to follow simple few steps. And this tutorial also very easy to understand. So lets start..


Delete all files in a Folder using PHP UandBlog

index.php


<table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
      <td width="19%">
<img src="file/Desert.jpg" width="100" style="height:auto;" /></td>
      <td width="81%">
<a href="delete.php?name=Desert" style="color:#FF0000;">Delete</a></td>
   </tr>
   <tr>
      <td>
<img src="file/Chrysanthemum.jpg" width="100" style="height:auto;" /></td>
      <td>
<a href="delete.php?name=Chrysanthemum" style="color:#FF0000;">Delete</a></td>
   </tr>
   <tr>
      <td>
<img src="file/Hydrangeas.jpg" width="100" style="height:auto;" /></td>
      <td>
<a href="delete.php?name=Hydrangeas" style="color:#FF0000;">Delete</a></td>
   </tr>
   <tr>
      <td>
<img src="file/Jellyfish.jpg" width="100" style="height:auto;" /></td>
      <td>
<a href="delete.php?name=Jellyfish" style="color:#FF0000;">Delete</a>
</td>
   </tr>
</table>

 

.
In below code their is two function is used, one glob() and another unlink()

we used glob() function which means to find files in a directory matching a specified pattern. This function returns an array of files/directories, or FALSE on failure.

And we used unlink() function to delete a file
 



delete.php


<?php
$name = $_GET['name'];

//Delete particular file from folder
$folders = glob('file/'.$name); //get file name
foreach($folders as $file){
    if(is_file($file))
    unlink($file); //delete file
}

//*****************************************************************************//

//Delete all files from folder
$folders = glob('file/*.jpg'); //get all file names
foreach($folders as $file){
    if(is_file($file))
    unlink($file); //delete file
}

?>

<script>window.location = 'index.php';</script>

 

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 1 )

johnny John:
Good tutorials thank you
Admin:
thank you

    Leave a comment..

    Click to login.