Refactor readFiles method in SapFileController for improved file handling and readability

This commit is contained in:
dhanabalan
2025-09-23 09:38:47 +05:30
parent 7e1852cfed
commit 6a70b45f4f

View File

@@ -32,26 +32,24 @@ class SapFileController extends Controller
$path = "/srv/pds.iotsignin.com/sapftp/In/";
$files = scandir($path);
// $files = scandir($path);
$path = '/srv/pds.iotsignin.com/sapftp/In/';
$files = array_filter(scandir($path), fn($file) =>
$file !== '.' && $file !== '..' && is_file($path . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'txt'
);
$data = [];
foreach ($files as $file) {
if ($file == '.' || $file == '..') continue; // skip system entries
if (is_file($path . $file) && pathinfo($file, PATHINFO_EXTENSION) == 'txt') {
$data[] = [
'filename' => $file,
'content' => file_get_contents($path . $file),
'content' => file_get_contents($path . $file), // plain text content
];
}
}
if (empty($data)) {
return response()->json([
'status' => 'error',
'message' => 'No text files found',
], 404);
// Optional: move processed file to Out
rename($path . $file, '/srv/pds.iotsignin.com/sapftp/Out/' . $file);
}
return response()->json([