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 select file and push those into array and run a foreach loop over that array to delete using unlink?
<?php
$i = 1;
$files = scandir("upload_dms_file/uploads_meeting_document");
$files_2 =scandir("upload_dms_file/uploads_filing_file");
$result=array_intersect($files,$files_2);
// $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>
   
<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 page:
Hope someone can guide me how to solve it. I have stucked these problem in the few days. Thanks.
You can make changes in your deletion code like below.
<?php
$folder1 = "uploads_meeting_document";
$folder2 = "uploads_filing_file";
$files = scandir($folder1);
$files_2 =scandir($folder2);
if(in_array($_GET["name"],$files)) {
unlink($folder1 . "/" . $_GET["name"]);
};
if(in_array($_GET["name"],$files_2 )) {
unlink($folder2 . "/" . $_GET["name"]);
};
// Redirecting back
header("Location: " . $_SERVER["HTTP_REFERER"]);
?>
First get list of all files in both folder then each each folder if given file exist in the folder then unlink. This will delete file from both folder.