• Tutorial List

PHP Check file existence & Delete Files

Last updated Jul 08, 2020

How to Check the Existence of a file?

The PHP file_exists() function is used to check the existence of a specific file. This function returns a boolean value –

  • If the specified file exists, it will return true.
  • Otherwise, it will return false.

👉 This function takes only the file name with the path –

<?php
    if(file_exists('./my.txt')){
        echo "This file exists";
    }
    else{
        echo "This file not exists";
    }
?>
Browser Output
This file exists

How to delete a file using PHP?

The PHP unlink() function is used to delete an existing file.

📝 If the file does not exist which you want to delete, It will return FALSE value with an error, else it will return TRUE. So, before deleting a file you must check the file existence.

📢 The unlink() function permanently deletes your files from your machine.

<?php
    $theFile = './my.txt';

    if(file_exists($theFile)){
      
       $process =  unlink($theFile);

       if($process){
            echo "The file has been deleted.";
       }
       else{
           echo "Opps, there is an error.";
       }
       
    }
    else{
        echo "This file not exists";
    }
?>
Browser Output
The file has been deleted.

How to delete multiple files at once using PHP?

<?php
    $allFiles = [
        './my-file.txt',
        './test.txt',
        './my-new-file.txt'
    ];

    foreach($allFiles as $theFile){

        if(file_exists($theFile)){

            $process =  unlink($theFile);
     
            if($process){
                 echo "<strong>$theFile</strong> file has been deleted.<br>";
            }
            else{
                echo "<strong>$theFile</strong> Opps, there is an error.<br>";
            }
            
         }
         else{
             echo "<strong>$theFile</strong> file not exists.<br>";
         }
    }
?>
Browser Output
./my-file.txt file has been deleted.
./test.txt file not exists.
./my-new-file.txt file has been deleted.

How to delete all files at once using PHP?

<?php
    $allFiles = array_filter(glob("./*"), function($theFIle){
        return is_file($theFIle);
    });
    
    foreach($allFiles as $theFile){

        if(file_exists($theFile)){

            $process =  unlink($theFile);
     
            if($process){
                 echo "<strong>$theFile</strong> file has been deleted.<br>";
            }
            else{
                echo "<strong>$theFile</strong> Opps, there is an error.<br>";
            }
            
         }
         else{
             echo "<strong>$theFile</strong> file not exists.<br>";
         }
    }
?>

How to delete specific types of files using PHP?

In the following example, deleting all the .txt files.

<?php
    $allFiles = glob("./*.txt");
    
    foreach($allFiles as $theFile){

        if(file_exists($theFile)){

            $process =  unlink($theFile);
     
            if($process){
                 echo "<strong>$theFile</strong> file has been deleted.<br>";
            }
            else{
                echo "<strong>$theFile</strong> Opps, there is an error.<br>";
            }
            
         }
         else{
             echo "<strong>$theFile</strong> file not exists.<br>";
         }
    }
?>

Ask Questions

Your email address will not be published. Required fields are marked *