<?php
/**
* File Error Detector (Non-Production Use)
*
* Detects potential file errors (permissions, existence) for non-production environments.
*/
/**
* Checks if a file exists and is readable.
*
* @param string $filePath The path to the file.
* @return bool True if the file exists and is readable, false otherwise.
*/
function isFileReadable(string $filePath): bool
{
if (!file_exists($filePath)) {
return false; // File does not exist
}
if (!is_readable($filePath)) {
return false; // File is not readable
}
return true; // File exists and is readable
}
/**
* Checks if a directory exists and is writable.
*
* @param string $dirPath The path to the directory.
* @return bool True if the directory exists and is writable, false otherwise.
*/
function isDirectoryWritable(string $dirPath): bool
{
if (!is_dir($dirPath)) {
return false; // Directory does not exist
}
if (!is_writable($dirPath)) {
return false; // Directory is not writable
}
return true; // Directory exists and is writable
}
/**
* Detects file errors in a given directory.
*
* @param string $directoryPath The path to the directory to check.
* @return array An array of errors found. Empty array if no errors.
*/
function detectFileErrors(string $directoryPath): array
{
$errors = [];
if (!is_dir($directoryPath)) {
$errors[] = "Error: Directory '$directoryPath' does not exist.";
return $errors;
}
$files = scandir($directoryPath);
if ($files === false) {
$errors[] = "Error: Could not scan directory '$directoryPath'.";
return $errors;
}
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue; // Skip current and parent directory entries
}
$filePath = $directoryPath . '/' . $file;
if (!isFileReadable($filePath)) {
$errors[] = "Error: File '$filePath' is not readable.";
}
if (is_file($filePath) && !is_readable($filePath)) {
$errors[] = "Error: File '$filePath' is not readable.";
}
}
return $errors;
}
// Example Usage (for testing/non-production)
if (php_sapi_name() !== 'cli') {
// Prevent execution when running from command line
exit;
}
$directoryToScan = './uploads'; // Replace with your directory
$errors = detectFileErrors($directoryToScan);
if (empty($errors)) {
echo "No file errors detected in '$directoryToScan'.\n";
} else {
echo "File errors detected in '$directoryToScan':\n";
foreach ($errors as $error) {
echo "- " . $error . "\n";
}
}
?>
Add your comment