JsusSalv Posted January 8, 2008 Share Posted January 8, 2008 Hello: I am having an issue with the file_exist() function. I understand how this function works. I am trying to check if a .pdf has been placed in a directory. Wherefore, the file name for the PDF will vary all the time. Therefore, when using file_exist(), I cannot specify the actual file name as specified in Example #1 from php.net manual (http://us.php.net/file_exists) <?php $filename = '/path/to/foo.txt'; <--I won't know the exact name of the PDF if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> Below is my code: <?php // Path to directory where PDFs are placed $filePath = "/path/to/dir/"; // Open the diretory & check for files and exclude the '..', '..', and the php processing file itself $dir = opendir ("$filePath"); while (false !== ($filename = readdir($dir))) { if($filename != "." && $filename != ".." && $filename != "if_else2.php") { // Only PDFs should be placed in directory. Just in case, check for pdf extension if (strpos($filename, '.pdf',1) ) { $pdf_file = $filename; if (file_exists($pdf_file) && is_readable($pdf_file)) { echo "Message: $pdf_file Exists"; } else { echo "Error Message: $pdf_file does not Exist"; } } } } ?> I have tried using *pdf appended to the $filePath but that doesn't work. Perhaps i coded the script incorrectly. The script does work. It tells me that there is a PDF file in the directory...if one had been uploaded. However, when there are no PDFs the script doesn't display the else {echo "Error Message: $pdf_file does not Exist";} message. It just shows up blank. I want the script to show the error message if there are no PDFs located within the directory. Can someone help me? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/85104-solved-file_exist-issue/ Share on other sites More sharing options...
sdi126 Posted January 9, 2008 Share Posted January 9, 2008 It is because strpos is returning false...it nevers find the position of .pdf if a file is not there... You need to move your else code down like so: if (strpos($filename, '.pdf',1) ) { $pdf_file = $filename; if (file_exists($pdf_file) && is_readable($pdf_file)) { echo "Message: $pdf_file Exists"; } } else { echo "Error Message: $pdf_file does not Exist"; } Quote Link to comment https://forums.phpfreaks.com/topic/85104-solved-file_exist-issue/#findComment-434102 Share on other sites More sharing options...
roopurt18 Posted January 9, 2008 Share Posted January 9, 2008 if (file_exists($pdf_file) && is_readable($pdf_file)) { echo "Message: $pdf_file Exists"; } else { echo "Error Message: $pdf_file does not Exist"; } } I don't understand why you're doing any of that in the first place. You are using opendir() and readdir() so you are only getting files that already exist. If you want a message when no .pdfs exist, set a flag to false and set it to true whenever you find a pdf. If, after the loop, the flag is still false, then no .pdfs were found. Quote Link to comment https://forums.phpfreaks.com/topic/85104-solved-file_exist-issue/#findComment-434106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.