Jump to content

[SOLVED] file_exist() issue...


JsusSalv

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/85104-solved-file_exist-issue/
Share on other sites

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";
}
        

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.