dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
2168
share rss forum feed


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

3 edits

PHP: file_exists fails to work or pebkac?

This is a continuation from my older post but now it's based on a different topic...

I'm struggling in getting my URL validation engine (php file in a folder labeled "engine") to work. I had it working once and when I tried to tweak it a little to make it direct home first and going to a different folder if someone clicks on the "Blog" link. Then it stopped working. I tried going back to the way it was originally and it still doesn't work.

I even tried having it print out specific strings after each step to see where the error is. I am still not finding the error. I think it may be where the if function (Line 10 in validateURL.php) is or it may be something before then. I'm not completely sure. Can someone help out? I'll enclose each included file below.

validateURL.php - This is where I think the problem is.

<?php
    $default = '/content5/home'; //Whatever default page
    $error = '/content5/error404.php';
  
    $tempPage = isset($_GET['page']) ? $_GET['page'] : $default;
    echo $tempPage . '<br>';
    echo '$tempPage does not contain $default... $tempPage = ' . $tempPage . '<br>';
    $tempPage = '/content5/' . basename($tempPage) . '.php';
  
    if (!file_exists($tempPage))    { //Checks if the file doesn't exist
        echo 'Load failed <br>' . $tempPage . '<br>';
        echo 'File does not exist... ';
        print_r(file_exists($tempPage) . '<br>');
        $page = $error; //If it doesn't, it'll load the error page
    }
    else{
        $page = $tempPage; //Assigns page from $tempPage to $page
    }
        require($page); //And now it's on your page!
?>
 

genMenu.php
<?php
    $menu = array(); 
    require('/section/nav.php');
  
    //$title='Home'; Default title 
  
    function generateMenu()    { 
        global $menu,$default,$title; 
        $error = '<span class="error"><p>There has been an error</p></span>';
        $default = 'Home';
        echo '    <ul>';
        $p = isset($_GET['page']) ? $_GET['page'] : $default;
        foreach ($menu as $link=>$item)    {
            $newLink = $link;
            $class = '';
            if(filter_var($newLink, FILTER_SANITIZE_STRING)){
                if ($link==$p)    {
                $class='class="selected"';
                }
            }
            else{
                die($error);
            }
            
            echo '<li '.$class.'><a href="?page='.$link.'">'.$item.'</a></li>';
        }
        echo '</ul>';
    }
?>
 

p.s... genMenu is called in the beginning of the index.php file and validateURL is called in the body of the index.php.

I am running on wampServer on Windows 7... so...

My index is in C:/wamp/www/
My other pages are in C:/wamp/www/content5/ such as home.php, contact.php, about.php, etc...


MVS
Premium
join:2005-04-18

Try using the full path for $tempPage:

$tempPage = 'C:/wamp/www/content5/' . basename($tempPage) . '.php';
 


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

4 edits
reply to Ashke

I tweaked around in validateURL.php and found out that the error lies in the last if-else function.

<?php
    $default = '/content/home'; //Default Page
    $error = '/content/error404.php';
  
    $tempPage = isset($_GET['page']) ? $_GET['page'] : $default;
    print_r('isset( $_GET[\'page\'] ) = ' . isset($_GET['page']) . '<br>');
    echo '$tempPage = isset($_GET[\'page\'] ? $_GET[\'page\'] : $default === ' . $tempPage . '<br>';
  
    //This if / else functions checks to see if $tempPage is the same as $default or 'home'
    //Then it will process the instructed codes
    if ($tempPage == $default){ // Checks to see if $tempPage contains the same as $defualt or home
        echo '$tempPage is the same as $default... ' . $tempPage . '<br>';
        echo 'Appending .php to $tempPage <br>';
        $tempPage .= '.php'; // appends '.php' to $tempPage
        echo $tempPage . '<br>';
    }
    
    else if($tempPage == 'home'){
        $tempPage = '/content/' . $tempPage . '.php';
    }
    
    else {
        echo '$tempPage is not the same as $default... ' . $tempPage . '<br>';
        echo 'attaching proper directory path and extension to $tempPage <br>';
        $tempPage = '/content/' . basename($tempPage) . '.php';
        echo $tempPage . '<br>';
    }
 
    //require($tempPage); //For when the codes below fail
 
    if (!file_exists($tempPage))    { //Checks if the file doesn't exist
        echo 'File does not exist...<br>';
        echo 'Load failed <br>';
        echo $tempPage . '<br>';
        $page = $error; //If it doesn't, it'll load the error page
    }
    
    else{
        $page = $tempPage; //Assigns page from $tempPage to $page
    }
    
    require($page); //And now it's on your page!
?>
 

When I comment out the last if-else function and remove the // from Line 27, the page loads just fine... So can someone please tell me what's going on? *baffled*

--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to MVS

I would like to avoid using the absolute file path because I plan on uploading the pages onto the site using linux server. As far as my understanding goes, Linux does not have letter-based directories (A:, C:, D:, etc...).
--
-Ashke



MVS
Premium
join:2005-04-18

Yes, that's true. In that case, you could use:

$tempPage = dirname(__FILE__) . '/content/' . basename($tempPage) . '.php';
 


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

2 edits

said by MVS:

Yes, that's true. In that case, you could use:

$tempPage = dirname(__FILE__) . '/content/' . basename($tempPage) . '.php';
 

Hrmm... Doesn't quite work because if I do it that way, dirname(__FILE__) will change the path to c:\wamp\www\engines\

My directories are as listed:
C:\wamp\www\
<dir> content/
<dir> css/
<dir> engines/
<dir> fonts/
<dir> section/
<dir> variables/
blah.php
index.php
index1.php
test2.php
testmysql.php
 

C:\wamp\www\content\
<dir> blog/
about.php
contact.php
error404.php
funstuff.php
home.php
resume.php
sitemap.php
test.php
 

C:\wamp\www\engines\
genMenu.php
validateURL.php
 

The validateURL.php is in the engines folder.... Let me post what I am seeing on my page...

--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to MVS

Using the latest validateURL.php file posted above.

isset( $_GET['page'] ) = 1
$tempPage = isset($_GET['page'] ? $_GET['page'] : $default === home
File does not exist...
Load failed
/content/home.php
--
-Ashke


MVS
Premium
join:2005-04-18
reply to Ashke

Oh, I see. Then, try this instead:

$tempPage = dirname(__FILE__) . '../content/' . basename($tempPage) . '.php';
 


Steve
I know your IP address
Consultant
join:2001-03-10
Foothill Ranch, CA
kudos:5

Instead of:

$tempPage = dirname(__FILE__) . '../content/' . basename($tempPage) . '.php';
 
maybe use
$tempPage = dirname(__FILE__) . '/../content/' . basename($tempPage) . '.php';
 
to put a / between the dirname and dot-dot-content.

To the OP: please indent your code so we can read it.


MVS
Premium
join:2005-04-18

Oops! Thanks for catching that. It's been a long day...



Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

said by MVS:

Oops! Thanks for catching that. It's been a long day...

It's all good
--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to Steve

And also to MVS See Profile,

I tried using the codes below...

$tempPage = dirname(__FILE__) . '../content/' . basename($tempPage) . '.php';
 

and
$tempPage = dirname(__FILE__) . '/../content/' . basename($tempPage) . '.php';
 

Unfortunately, nothing works :(

--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to Ashke

*Faceslams onto desk repeatedly*

I figured out the problem... See the variable declarations in Lines 1 and 2 on top of the validateURL.php code block? For some reason, I can't include the initial slash when declaring the location.

When I removed them, it worked just fine...

$default = 'content5/home';
$error = 'content5/error404.php';
 

Does anyone have a chance to know why it doesn't like the first '/' in the string?

--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to Steve

said by Steve:

To the OP: please indent your code so we can read it.

Done... indented all of the block quotes in previous posts.
--
-Ashke


MVS
Premium
join:2005-04-18
reply to Ashke

With the '/' at the beginning, PHP thinks that you're specifying the full path to a file or directory, since that's what Unix-style file paths look like (for example, "/var/www/html/index.html"). Without the '/', it knows that you're referring to a relative path.

That's why I was suggesting to try altering the $tempPage variable, but it looks like it needed to be taken one step further with the other variables as well.



Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

said by MVS:

With the '/' at the beginning, PHP thinks that you're specifying the full path to a file or directory, since that's what Unix-style file paths look like (for example, "/var/www/html/index.html"). Without the '/', it knows that you're referring to a relative path.

That's why I was suggesting to try altering the $tempPage variable, but it looks like it needed to be taken one step further with the other variables as well.

You have a point...

But when a folder is designated as the original starting destination per the server's setup, why does the '/' look farther back than the said designated folder? I mean it's not in the APACHE alias file.

It's kind of perplexing... >.>
--
-Ashke


MVS
Premium
join:2005-04-18

That's because, in Linux and other Unix-like operating systems, '/' refers to the root directory. So, if you tell PHP to open '/files/index.html', it will look in the root directory for a directory named 'files', and it will look in there for a file called 'index.html'. But if you tell it to open 'files/index.html', it will look in the current directory (and a few other configurable places) instead.



Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

Good point....

So for Windows-based servers, it will look in '/My Computer/' if I used '/' in the beginning of the string?
--
-Ashke



MVS
Premium
join:2005-04-18

My guess is that it would refer to the root directory of the current drive (for example, "C:\"), but I don't have a Windows server available to test that.


dave
Premium,MVM
join:2000-05-04
not in ohio
kudos:8
Reviews:
·Verizon FiOS
reply to Ashke

There's no file system directory called "My Computer", so, probably not. "My Computer" is an entity in the shell namespace only. So unless your server happens to be dealing with the shell namespace, I'd suppose you don't see "My Computer".

Overall philosophy difference here: Unix tends to put non-file-system objects into the file system namespace -- like /dev/foo for example. Windows tends to put file system objects into some other namespace -- like the assorted x:\$Recycle.Bin directories show up in Explorer as "Recycle Bin", and only one of them. Not everything you see in Windows Explorer is a file.



Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

said by dave:

There's no file system directory called "My Computer", so, probably not. "My Computer" is an entity in the shell namespace only. So unless your server happens to be dealing with the shell namespace, I'd suppose you don't see "My Computer".

Overall philosophy difference here: Unix tends to put non-file-system objects into the file system namespace -- like /dev/foo for example. Windows tends to put file system objects into some other namespace -- like the assorted x:\$Recycle.Bin directories show up in Explorer as "Recycle Bin", and only one of them. Not everything you see in Windows Explorer is a file.

I guess you guys are right... The Linux server went batshit-berserk when I tried it....
--
-Ashke