Search code examples
phpunlinkarray-intersect

How to use array_intersect to find the same files?


I have a problem regarding the deletion of two files (both with the same file name) in two diffrent folders. I tried using the unlink function for deletion. How can i delete these two files in these two different folders using PHP unlink function?

Can anyone guide me on how to use array_intersect to find the files, push those into array and run a foreach loop over that array to delete using unlink?

Below is my code:

<?php
$i = 1;
$files = scandir("upload_dms_file/uploads_meeting_document");
$files_2 = scandir("upload_dms_file/uploads_filing_file");
$date = $files[$a];
for ($a = 2; $a < count($files); $a++) {

    ?>

    <tr>
        <td> <?php echo $i++; ?></td>
        <td> <a href="upload_dms_file/uploads_meeting_document/<?php echo $files[$a]; ?>" target="_blank"> <?php echo $files[$a]; ?></a></td>
        <td>
            <button class="btn btn-sm btn-primary"><a href="upload_dms_file/uploads_meeting_document/<?php echo $files[$a]; ?>"
                                                      download="<?php echo $files[$a]; ?>" style="color: white;">
                    Download</a></button>
            &nbsp &nbsp
            <button class="btn btn-sm btn-primary"><a
                        href="upload_dms_file/delete_meeting_document.php?name=uploads_meeting_document/<?php echo $files[$a]; ?>" style="color: white;">
                    Delete
                </a></button>
        </td>
    </tr>

    <?php
} ?>

Below is my deletion code, it succeeds deleting the file in the uploads_meeting_document folder, so my question is how to delete the other copy of the file located in the uploads_filing_file (variable is $files_2) folder?

<?php
unlink($_GET["name"]);

// Redirecting back
header("Location: " . $_SERVER["HTTP_REFERER"]);
?>


Below is my output:

Output1


Solution

  • To compare if a file exists in two directories you need the basename. Using scandir() is not the best way, because you will also receive directories which can not be deleted, but you would try as '.' and '..' always do exist (in *NIX systems).

    Proper way is an iterator who does not read all into memory what will save speed and memory on very large directories.

    Here is a code example how to delete files, which exist in different locations.

    const PATH1 = 'upload_dms_file/uploads_meeting_document/';
    const PATH2 = 'upload_dms_file/uploads_filing_file/';
    
    $files1 = getFiles(PATH1);
    $files2 = getFiles(PATH2);
    
    $intersects = array_intersect($files1, $files2);
    
    foreach($intersects as $filename) {
        deleteFile(PATH1, $filename);
        deleteFile(PATH2, $filename);
    }
    
    function getFiles(string $path): array
    {
        $files = [];
        $iterator = new DirectoryIterator($path);
        foreach ($iterator as $fileInfo) {
            if ($fileInfo->isFile()) $files[] = $fileInfo->getFilename();
        }
    
        return $files;
    }
    
    function deleteFile($path, $filename) {
        $fullpath = $path . $filename;
        if(file_exists($fullpath)) unlink($fullpath);
    }