ohctech_p8/find_file_for_telegram.php
2024-10-16 19:18:52 +05:30

40 lines
1.1 KiB
PHP

<?php
// Function to find a file in a directory
function findFile($directory, $filename) {
// Add trailing slash to directory if missing
$directory = rtrim($directory, '/') . '/';
// Use glob to get an array of files matching the pattern
$files = glob($directory . '*', GLOB_MARK);
// Loop through the files
foreach ($files as $file) {
// If the file is a directory, recursively search it
if (is_dir($file)) {
$filePath = findFile($file, $filename);
if ($filePath !== false) {
return $filePath;
}
}
// If the file matches the filename, return its path
elseif (basename($file) === $filename) {
return $file;
}
}
// If file is not found, return false
return false;
}
// Example usage
$directory = 'C:/Users/dusha/OneDrive/Desktop/pdf/';
$filename = 'Dushant Mali.pdf';
$filePath = findFile($directory, $filename);
if ($filePath !== false) {
echo "File found at: " . $filePath;
} else {
echo "File not found.";
}
?>