PHP file_exists returns false for existing file, until REFRESH is performed

Posted on 2009-02-22
Last Modified: 2012-06-27
I am trying to debug code on an existing 3rd party website that makes use of file_exists() to determine which version of a homepage exists.

The code returns false the first time the page is invoked, thus showing the wrong version of the homepage. But after pressing REFRESH on the browser, it then finds the file.

I can only imagine that after the page is first displayed, some other setting is made dynamically which somehow enables the file_exists() functionality, or at least fixes it.

Any thoughts as to what might be behind this?
// TEST---------------------------------------------

	echo "TEST 1<br>";

	if (file_exists(JB_THEME_PATH.'index-home.php')) {

		echo "FILE: " . JB_THEME_PATH.'index-home.php' . " DOES EXIST<br>";

	} else {

		echo "FILE: " . JB_THEME_PATH.'index-home.php' . " DOES NOT EXIST<br>" . "USING FILE: " . JB_DEFAULT_THEME_PATH.'index-home.php' . " INSTEAD!";



Open in new window

Question by:rascal
    LVL 5

    Accepted Solution

    Most likely on your form you have something like this
    <form method=post action="<? $_SERVER['PHP_SELF']?>">
    and you have your PHP code above your form at a guessing. Try having the PHP queries on another file and link to them instead of using the same page.
    LVL 1

    Author Comment

    Thansk drakeshe,
    I'll have a look at the code and see if that's the cause. This is 3rd party shrinkwrap software that's been around a while (Jamit job board) so I'm thinking their code on it's own is probably ok, and that perhaps it's a site setting of some kind(?), but I'll check it out.
    LVL 5

    Assisted Solution

    I had the same problem myself. Probably the best way to get it to work is to get JavaScript to refresh the page again.
    LVL 8

    Assisted Solution

    your code worked for me .... may a full script will  be easier ....

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    In this article, we will look at our content management system as a whole, to get an idea of how we will start to manage these content items.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now