Enhance readFiles method in SapFileController with improved error handling and debugging information

This commit is contained in:
dhanabalan
2025-09-23 09:48:24 +05:30
parent 86ad06def7
commit 1d998d6478

View File

@@ -32,25 +32,47 @@ class SapFileController extends Controller
$path = "/srv/pds.iotsignin.com/sapftp/In/"; $path = "/srv/pds.iotsignin.com/sapftp/In/";
// $files = scandir($path); $isDir = is_dir($path);
if (!is_dir($path) || !is_readable($path)) { $isReadable = is_readable($path);
return response()->json([
'status' => 'error',
'message' => 'Folder not accessible',
], 500);
}
$files = array_filter(scandir($path), function($file) use ($path) { if (!$isDir || !$isReadable) {
return $file !== '.' && $file !== '..' && is_file($path . $file) return response()->json([
&& pathinfo($file, PATHINFO_EXTENSION) === 'txt'; 'status' => 'error',
}); 'message' => 'Folder not accessible',
'debug' => [
'path_checked' => $path,
'is_dir' => $isDir,
'is_readable' => $isReadable,
'php_user' => get_current_user(),
]
], 500);
}
if (empty($files)) { // Scan folder
return response()->json([ $allFiles = scandir($path);
'status' => 'error', if ($allFiles === false) {
'message' => 'No text files found', return response()->json([
], 404); 'status' => 'error',
} 'message' => 'Failed to scan directory',
'debug' => ['path_checked' => $path]
], 500);
}
// Filter only .txt files
$files = array_filter($allFiles, function($file) use ($path) {
return $file !== '.' && $file !== '..' && is_file($path . $file)
&& pathinfo($file, PATHINFO_EXTENSION) === 'txt';
});
if (empty($files)) {
return response()->json([
'status' => 'error',
'message' => 'No text files found',
'debug' => [
'scanned_files' => $allFiles
]
], 404);
}
$data = []; $data = [];