Hey Laracasts community!
I'm running into a frustrating issue in my Laravel app where, after uploading multiple documents, creating a ZIP file, and uploading it to cloud storage (DigitalOcean Spaces), the cleanup process isn't fully working. Specifically:
The individual files get deleted.
The ZIP file gets deleted.
But the empty directory persists, even though scandir() claims files still exist (yet I can't see them in the file system).
This leaves behind empty folders cluttering the server.
I'm wondering if ZipArchive is somehow causing this (e.g., file locks or lingering references), but I suspect it's a logic error in my cleanup code. The code works for the most part, but the directory check fails.
Here's the relevant snippet from my familyHeadSave method (inside a loop for handling document uploads like Aadhaar, PAN, etc.). This is where the zipping and cleanup happens:
$count = 0;
$documentTypes = [
'aadhaar_document_name' => [
'document_name_id' => 1,
'document_number' => $familyHead->aadhaar_number,
],
'pan_document_name' => [
'document_name_id' => 2,
'document_number' => $familyHead->pan_number,
],
'gst_document_name' => [
'document_name_id' => 10,
'document_number' => $familyHead->gst_number,
]
];
foreach ($documentTypes as $inputName => $docInfo) {
if (!empty($request->$inputName) && isset($request->$inputName[0])) {
$fileName = time() . mt_rand(100, 999) . '.zip';
// ... (code for saving ClientDocument record, unit checks, etc.) ...
$policyDocumentNames = [];
$extensionWithOutName = pathinfo($fileName, PATHINFO_FILENAME);
$destinationFilePath = public_path("/s/" . getAgentPath($authUser->agent_code) . "/client/client_documents/" . $extensionWithOutName . "/");
$zipPaths = "s/" . getAgentPath($authUser->agent_code) . "/client/client_documents/" . $extensionWithOutName . "/";
// Upload individual files to spaces and collect names
foreach ($request->$inputName as $file) {
$policyDocument = time() . mt_rand(100, 999) . '.' . $file->getClientOriginalExtension();
if (!is_dir($destinationFilePath)) {
mkdir($destinationFilePath, 0777, true);
}
if ($file->move($destinationFilePath, $policyDocument)) {
$policyDocumentNames[] = $policyDocument;
}
if (File::exists($destinationFilePath . '/' . $policyDocument)) {
$uploadFileSpaces = uploadFileOnSpaces(public_path($zipPaths . $policyDocument), $zipPaths . $policyDocument);
}
}
// Create ZIP
$zip = new ZipArchive;
if ($zip->open($destinationFilePath . $fileName, ZipArchive::CREATE) === TRUE) {
foreach ($policyDocumentNames as $document) {
$filePath = $destinationFilePath . $document;
if (File::exists($filePath)) {
$zip->addFile($filePath, basename($filePath));
}
}
$zip->close();
}
// Upload ZIP to spaces
if (File::exists($destinationFilePath . '/' . $fileName)) {
$uploadFileSpaces = uploadFileOnSpaces(public_path($zipPaths . $fileName), $zipPaths . $fileName);
}
// Cleanup (this is where the issue is - directory not deleting)
if ($uploadFileSpaces == 1) {
foreach ($policyDocumentNames as $docName) {
@unlink($destinationFilePath . $docName);
}
if (is_dir($destinationFilePath) && count(scandir($destinationFilePath)) == 2) {
rmdir($destinationFilePath);
}
@unlink($destinationFilePath . $fileName);
}
// ... (rest of the loop: store units, save UploadedDocuments, etc.) ...
}
}
please help!!! thank you !