Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.