Enhance readFiles method in SapFileController with improved error handling and debugging information
This commit is contained in:
@@ -32,25 +32,47 @@ class SapFileController extends Controller
|
||||
|
||||
$path = "/srv/pds.iotsignin.com/sapftp/In/";
|
||||
|
||||
// $files = scandir($path);
|
||||
if (!is_dir($path) || !is_readable($path)) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Folder not accessible',
|
||||
], 500);
|
||||
}
|
||||
$isDir = is_dir($path);
|
||||
$isReadable = is_readable($path);
|
||||
|
||||
$files = array_filter(scandir($path), function($file) use ($path) {
|
||||
return $file !== '.' && $file !== '..' && is_file($path . $file)
|
||||
&& pathinfo($file, PATHINFO_EXTENSION) === 'txt';
|
||||
});
|
||||
if (!$isDir || !$isReadable) {
|
||||
return response()->json([
|
||||
'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)) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'No text files found',
|
||||
], 404);
|
||||
}
|
||||
// Scan folder
|
||||
$allFiles = scandir($path);
|
||||
if ($allFiles === false) {
|
||||
return response()->json([
|
||||
'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 = [];
|
||||
|
||||
Reference in New Issue
Block a user