簡體   English   中英

在線托管時,file_exists() 找不到文件

[英]file_exists() can't find file when hosted online

該代碼在我的本地主機(通過 xampp)上完美運行,但是當我將它移到在線主機時,這個問題就開始了。

我有一個驅動程序 php 代碼,如果它在文件夾pagestechPosts中找到它,它會導航到頁面。問題是我可以通過file_exists()加載一些文件但不能加載其他文件?

驅動程序代碼位於/public_html中我的主機根目錄中的index.php

<?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);

這是我的文件的文件夾樹

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

home.php它通過這個加載我頁面的全部內容

<?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'; ?>

我測試的

1.使用__DIR__的絕對路徑引用

在索引中index.php

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

同樣,它無法加載頁面。

2.將三元算子改為登陸Forall_Posts而不是home

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

它落在它上面,所以這意味着可以找到該文件。

解決方案,但不是最好的

僅為Forall_Posts.php添加特定的 if 條件


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" .

在這一點上我什么都不明白,為什么它沒有 go 到 else 狀態,只顯示404.php頁面,但 Z34D1F91FB2E514B8576FAB1A75A89A6B 跳過第一個特定條件?

您是否嘗試更改參考之一

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

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

當您想從不是來自“back”目錄的文件夾中獲取內容時,您不需要此“../”或“./”,讓我們考慮 index.php 並且您想要包含 E.php 您可以只需編寫 include_once("pages/E.php");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM