File_exists fails

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
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jagadishwor DulalSenior Web DeveloperCommented:
So you are looking in your directory? is the search path is valid try using relative path. Do you try removing ../ also?
0
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Best to make your file name an absolute path, based on your DocumentRoot, which is best extracted from your Webserver environment variables.
0
Richard KortsBusiness Owner / Chief DeveloperAuthor 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
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Ionut A. TudorPHP ProgrammerCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor 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
0
Ionut A. TudorPHP ProgrammerCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.