File_exists fails

Richard Korts
Richard Korts used Ask the Experts™
on
I have this code:
$found = false;
$nxt = 109; // letter m
for ($j = 0; $j < 14; $j++) {
	$ltr = chr($nxt);
	$fname = "../summsp/" . $_SESSION['vid'] . "summary" . $ltr . ".pdf";
	if (file_exists($fname)) {
		$found = true;
		break;
	}
	$nxt--;
	if ($nxt == 96) {
		$nxt = 0;
	}	
}
if (! $found) {
	echo "summary file " . $fname . " not there.<br>";
}	

Open in new window


It does NOT find the file; it echo's this:

summary file ../summsp/18605summary.pdf not there.

As the attachment shows (snippet from filezilla), the file is there.

Why does the code fail?

Thanks
18605summary.JPG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jagadishwor DulalSenior Web Developer

Commented:
So you are looking in your directory? is the search path is valid try using relative path. Do you try removing ../ also?
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Best to make your file name an absolute path, based on your DocumentRoot, which is best extracted from your Webserver environment variables.
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Used absolute path (as per the host), same problem.

summary file /home/lakoshva/summsp/18607summary.pdf not there.

That file is there.

I put a tiny php program in that copied a single file from "../summsp/<flie name> to a folder under public_html, it worked.

Can I use wildcards in the copy (I do not know in the general case) if there is a suffix (a - m) on the file name, like 18607summarya.pdf.

Can I do copy like this?

copy("../summsp/18605summary*.pdf", "thold/18605summary*.pdf");

Or with some other wildcard identifier that php recognizes?

Thanks
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Commented:
Under what path are you running your script? I cleaned it up a bit so you can see the full path where it checks the file existence.

<?php

$found = false;
$video = (string) $_SESSION['vid'] ?? '';
$directory = __DIR__ . DIRECTORY_SEPARATOR . 'summsp' . DIRECTORY_SEPARATOR;

for ($asc = 109; $asc > 96; $asc--) {
    $fname = $directory . $video . "summary" . chr($asc) . ".pdf";

    if (file_exists($fname)) {
        $found = true;
        break;
    }
}

if (!$found) {
    echo "Not found! Last file checked: " . $fname . "<br>";
}

Open in new window

Richard KortsBusiness Owner / Chief Developer

Author

Commented:
There can also be (the most likely case) when $asc = 0( null character).

I have found a way to copy them to a different directory within public_html.

I'm going to follow that path.

Thanks

Commented:
Your code was reaching that condition of $n char 0 which if you echo it gets translated to nothing but in fact the file_exists check was using its binary \000 which is why it might have caused confusion. Try the code I posted above and see the exact path.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial