简体   繁体   中英

file_exists() can't find file when hosted online

The code works perfectly on my local host (by xampp) but this problem started when I moved it to online host.

I have a driver php code that naviagte to the page if it finds it in folder pages or techPosts the problem is that I can load some files by file_exists() but can't load the others?

The driver code is located in index.php in the root of my host in /public_html

<?php

include_once "config.php";
include "functions.php";

$db = db_connection();

$page = isset($_GET['p']) ? $_GET['p'] : 'home';

include_once './views/_header.php';

if (file_exists("./pages/{$page}.php")) { //for the normal pages
    include "./pages/{$page}.php";
} elseif (file_exists("./techPosts/{$page}.php")) { //for the story of posts
    include "./techPosts/{$page}.php";
} else {
    include './pages/404.php';
}
include_once "./views/_footer.php";

db_close($db);

and here is the folder tree for my files

public_html/
    index.php
    pages/
        Forall_Posts.php
        home.php
        A.php
        B.php
        C.php
        D.php
        E.php
        F.php
    techPosts/
        test.php

in home.php it loads the whole content of my page by this

<?php require_once './pages/B.php' ?>

<?php require_once './pages/C.php'; ?>

<div id="aboutSection">
    <hr class="page-break" data-aos="zoom-out">
    <?php require_once './pages/D.php'; ?>
</div>

<hr class="page-break" data-aos="zoom-out">
<?php require_once './pages/E.php'; ?>

<hr class="page-break" data-aos="zoom-out">
<?php require_once './pages/F.php'; ?>

What I tested

1.Reference by absolute path using __DIR__

in index.php

if (file_exists(__DIR__."/pages/{$page}.php")) { //for the normal pages
    include __DIR__."/pages/{$page}.php";

same, it can't load the page.

2.Change ternary operator to land on Forall_Posts instead of home

$page = isset($_GET['p']) ? $_GET['p'] : 'Forall_Posts';

it lands on it, so it means that the file can be found.

Solution but not best one

Add a specific if condition just for Forall_Posts.php


if (file_exists("./pages/{$page}.php")) { //for the normal pages
    debug_to_console("First if condition");
    include "./pages/{$page}.php";
} elseif (file_exists("./pages/Forall_Posts.php")) { //only to this page
    debug_to_console("Second if condition");
    include "./pages/Forall_Posts.php";
} elseif (file_exists("./techPosts/{$page}.php")) { //for the story of posts
    include "./techPosts/{$page}.php";
} else {
    include './pages/404.php';
}

It can find Forall_Posts.php now, but when I try with A.php it goes to Forall_Posts.php and I have output that says "Second if condition" .

At this point I don't understand anything, why it didn't go to the else statemenet and just show the 404.php page but go to the specific if condition and skip the first one?

did you try changing reference one of the

<?php require_once './pages/E.php'; ?>

to

<?php require_once './pages/Forall_Posts.php.php'; ?>```

you do not need this "../" or "./" when you want to fetch content from a folder that is NOT from the "back" directory, let's consider the index.php and you want to include E.php you can simply write include_once("pages/E.php");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM