PHP文件目录未找到。

huangapple go评论68阅读模式
英文:

PHP file directory not found

问题

I've translated the code as you requested. Here's the translated code:

我正在制作一个简单的简历类型的网站,可以在其中编辑我的个人资料。我遇到的问题是,我有一个 header.php 文件,其中包含所有导航按钮。我将该 header 文件包含到每个页面中。

当我在我的个人资料页面上单击“编辑”个人资料时,它会将我带到另一个页面,启用了输入字段以编辑我的个人资料。问题是,当我在编辑个人资料页面上(位于子文件夹中)并使用“../header.php”包含根时,导航选项不起作用,因为在 header.php 中,所有文件都在根文件夹中,但当我从子文件夹包含头文件并尝试导航时,它尝试在子文件夹中查找文件,但该文件不存在。

它应该让我通过正常方式浏览页面,即通过返回一个目录,但我无法制作硬编码的解决方案,因为我的页眉位于根文件夹中。

以下是我的代码:
header.php:

<?php
// 需要数据库文件以进行连接。
require 'resources/database/mysqli_connect.php';
// 启动会话。
session_start();
$rank = 0;
// 如果会话不为空,分配变量并运行查询。
if (!empty($_SESSION["id"])) {
    $id = $_SESSION["id"];
    $rank = $_SESSION["rank"];
    $result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
    $row = mysqli_fetch_assoc($result);
}
?>
<!DOCTYPE html>
<html>
<head>

</head>
<header>
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
        <a href="./index.php" class="navbar-brand">IKAWORLD</a>
        <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarMenu">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a href="./index.php" class="nav-link">Home</a>
                </li>
                <?php
                if ($rank == 5) {
                    echo
                        "<li class='nav-item'>
                            <a href='./admin.php' class='nav-link'>Admin</a>
                        </li>";
                }
                ?>
                <?php
                if (empty($_SESSION["id"])) {
                    echo
                        "<li class='nav-item'>
                            <a href='login.php' class='nav-link'>Login</a>
                        </li>
                        <li class='nav-item'>
                            <a href='register.php' class='nav-link'>Register</a>
                        </li>";
                } else {
                    echo
                        "<li class='nav-item'>
                            <a href='profile.php' class='nav-link'>Profile</a>
                        </li>
                        <li class='nav-item'>
                            <a href='logout.php' class='nav-link'>Logout</a>
                        </li>";
                }
                ?>
            </ul>
        </div>
    </nav>
</header>
</html>

根导航页面示例(profile.php):

<?php
// 需要数据库文件以进行连接 //
include 'header.php';
require './resources/database/mysqli_connect.php';
// 启动会话。
$rank = 0;

// 如果用户已登录,则分配变量和值 //
if (!empty($_SESSION["id"])) {
    // 已登录用户的ID //
    $id = $_SESSION["id"];
    $result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
    $row = mysqli_fetch_assoc($result);
    // 这是与用户关联的父ID //
    // 检查已登录用户的级别 //
    $rank = $_SESSION["rank"];
    if ($rank == 1) {
        $level = "User";
    } elseif ($rank == 2) {
        $level = "Verified User";
    } elseif ($rank == 3) {
        $level = "Family";
    } elseif ($rank == 4) {
        $level = "Friend";
    } elseif ($rank == 5) {
        $level = "Admin";
    }
}
// 如果用户未登录,将其重定向到登录页面 //
else {
    header("Location: ./login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="resources/stylesheet/stylesheet.css">
    <link rel="stylesheet" href="resources/stylesheet/register.css">
</head>
<body>
    <div class="content-wrapper">
        <br>
        <h1 class="subheading">Profile</h1>
        <table>
            <th>Category</th>
            <th>Value</th>
            <tr>
                <td>User ID:</td>
                <td><?php echo $row['id']; ?></td>
            </tr>
            <tr>
                <td>Username:</td>
                <td><?php echo $row['username']; ?></td>
            </tr>
            <tr>
                <td>First name:</td>
                <td><?php echo $row['first_name']; ?></td>
            </tr>
            <tr>
                <td>Last name:</td>
                <td><?php echo $row['last_name']; ?></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><?php echo $row['email']; ?></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td>--------------</td>
            </tr>
            <tr>
            <tr>
                <td>Rank</td>
                <td><?php echo $level; ?></td>
            </tr>
        </table>
        <a href="edit_pages/editProfile.php"><button>Edit Profile</button></a>

        <br><br>
    </div>
</body>
</html>

编辑个人资料页面(位于子文件夹中):

<?php
// 需要连接的数据库文档 //
include '../header.php';
require '../resources/database/mysqli_connect.php';
$rank = 0;
// 如果会话不为空,分配变量并运行查询。
if (!empty($_SESSION["id"])) {
    $id = $_

<details>
<summary>英文:</summary>

I&#39;m making a simple resume-type website in which I can edit my profile. The issue that I am having is, I have a header.php file which has all the navigation buttons. I&#39;m including that header file into every page which I have.

When I go on my profile and click &#39;Edit&#39; profile, it takes me to another page with input fields enabled to edit my profile. The problem is, when I am on the edit profile page (which is in a subfolder), and it has included root using &#39;../header.php&#39;, the navigation options are not working because on header.php, all files are in the root folder, but when I&#39;m including the header file from a subfolder and trying to navigate, it&#39;s not going to the root folder, it&#39;s trying to find the file in the subfolder which doesn&#39;t exist.

It should let me navigate through the pages as normal by going back a directory, but I can&#39;t make a hard-coded solution as my header is in the root folder.

Here&#39;s my code:
header.php:

<?php
// requires database file for connection.
require 'resources/database/mysqli_connect.php';
// Starts the session.
session_start();
$rank = 0;
// If session is not empty, assigns variables and runs the query.
if(!empty($_SESSION["id"])){
$id = $_SESSION["id"];
$rank = $_SESSION["rank"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
}
?>
<!DOCTYPE html>
<html>
<head>

&lt;/head&gt;
&lt;header&gt;
&lt;nav class=&quot;navbar navbar-expand-sm navbar-dark bg-dark&quot;&gt;
&lt;a href=&quot;./index.php&quot; class=&quot;navbar-brand&quot;&gt;IKAWORLD&lt;/a&gt;
&lt;button class=&quot;navbar-toggler&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarMenu&quot;&gt;
&lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarMenu&quot;&gt;
&lt;ul class=&quot;navbar-nav ml-auto&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a href=&quot;./index.php&quot; class=&quot;nav-link&quot;&gt;Home&lt;/a&gt;
&lt;/li&gt;
&lt;?php 
if($rank == 5){
echo
&quot;&lt;li class=&#39;nav-item&#39;&gt;
&lt;a href=&#39;./admin.php&#39; class=&#39;nav-link&#39;&gt;Admin&lt;/a&gt;
&lt;/li&gt;&quot;;
}
?&gt;
&lt;?php 
if(empty($_SESSION[&quot;id&quot;])){
echo 
&quot;&lt;li class=&#39;nav-item&#39;&gt;
&lt;a href=&#39;login.php&#39; class=&#39;nav-link&#39;&gt;Login&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&#39;nav-item&#39;&gt;
&lt;a href=&#39;register.php&#39; class=&#39;nav-link&#39;&gt;Register&lt;/a&gt;
&lt;/li&gt;&quot;;
} else{
echo
&quot;&lt;li class=&#39;nav-item&#39;&gt;
&lt;a href=&#39;profile.php&#39; class=&#39;nav-link&#39;&gt;Profile&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&#39;nav-item&#39;&gt;
&lt;a href=&#39;logout.php&#39; class=&#39;nav-link&#39;&gt;Logout&lt;/a&gt;
&lt;/li&gt;&quot;;
}
?&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;/header&gt;

</html>


Root navigation page example (profile.php):

<?php
// Requires database file for connection //
include 'header.php';
require './resources/database/mysqli_connect.php';
// Starts the session.
$rank = 0;

// If user is logged in, assigns variables and values //
if(!empty($_SESSION["id"])){
// Logged in user's ID //
$id = $_SESSION["id"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
// This is the parent ID associated with the user //
// This checks the rank of the logged user //
$rank = $_SESSION["rank"];
if ($rank == 1){
$level = "User";
} else if ($rank == 2){
$level = "Verified User";
} else if ($rank == 3){
$level = "Family";
} else if ($rank == 4){
$level = "Friend";
} else if ($rank == 5){
$level = "Admin";
}
}
// If user is not logged in, it brings them to the login page //
else{
header("Location: ./login.php");
}

?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="resources/stylesheet/stylesheet.css">
<link rel="stylesheet" href="resources/stylesheet/register.css">
</head>
<body>
<div class="content-wrapper">
<br>
<h1 class="subheading">Profile</h1>
<table>
<th>Category</th>
<th>Value</th>
<tr>
<td>User ID:</td>
<td><?php echo $row['id']; ?></td>
</tr>
<tr>
<td>Username:</td>
<td><?php echo $row['username']; ?></td>
</tr>
<tr>
<td>First name:</td>
<td><?php echo $row['first_name']; ?></td>
</tr>
<tr>
<td>Last name:</td>
<td><?php echo $row['last_name']; ?></td>
</tr>
<tr>
<td>Email:</td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Password:</td>
<td>--------------</td>
</tr>
<tr>
<tr>
<td>Rank</td>
<td><?php echo $level; ?></td>
</tr>
</table>
<a href="edit_pages/editProfile.php"><button>Edit Profile</button></a>

        &lt;br&gt;&lt;br&gt;
&lt;/div&gt;
&lt;/body&gt;

</html>


edit profile page (located in a subfolder):

<?php
// Requires DB document for connection //
include '../header.php';
require '../resources/database/mysqli_connect.php';
$rank = 0;
// If session is not empty, assigns variables and runs the query.
if(!empty($_SESSION["id"])){
$id = $_SESSION["id"];
$rank = $_SESSION["rank"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
}
else{
// If user not logged in, it redirects them //
header("Location: ../login.php");
}

// This code runs when user clicks update profile //
if(isset($_POST["updateProfile"])){
// Stores input data to variables + cleans them to prevent SQLi Injection //
$username = mysqli_real_escape_string($conn, $_POST["userName"]);
$firstname = mysqli_real_escape_string($conn, $_POST["firstName"]);
$lastname = mysqli_real_escape_string($conn, $_POST["lastName"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
$password = mysqli_real_escape_string($conn, $_POST["password"]);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Checks if any fields have been left empty //
if (empty($_POST[&quot;userName&quot;]) || empty($_POST[&quot;firstName&quot;]) || empty($_POST[&quot;lastName&quot;]) || empty($_POST[&quot;email&quot;]) || empty($_POST[&quot;password&quot;])) {
// If missing fields found, it throws this error //
echo &quot;&lt;script&gt;alert(&#39;Missing fields detected!&#39;)&lt;/script&gt;&quot;;
}
else{
// If everything is good, it creates the query to update user details in the DB //
$query = &quot;update users set username = &#39;$username&#39;, first_name = &#39;$firstname&#39;, last_name = &#39;$lastname&#39;, email = &#39;$email&#39;, password = &#39;$hashed_password&#39; where id = &#39;$id&#39;&quot;;
if ($conn -&gt; query($query) == TRUE){
// If query is successful, it alerts the user with successful message //
echo &quot;&lt;script&gt; alert(&#39;Updated Successful&#39;); &lt;/script&gt;&quot;;
// Redirects user/
header(&quot;Location: ../profile.php&quot;);
} else{
// If unsuccessful, this error appears //
echo &quot;&lt;script&gt; alert(&#39;An error has occurred.&#39;); &lt;/script&gt;&quot;;
}        
}

}

?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="../resources/stylesheet/register.css">
<link rel="stylesheet" href="../resources/stylesheet/stylesheet.css">
</head>

&lt;body&gt;
&lt;div class=&quot;content-wrapper&quot;&gt;
&lt;br&gt;
&lt;h1 class=&quot;subheading&quot;&gt;Edit Profile&lt;/h1&gt;
&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
&lt;table&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;tr&gt;
&lt;td&gt;Username:&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&lt;?php echo $row[&#39;username&#39;]; ?&gt;&quot; name=&quot;userName&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;First name:&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&lt;?php echo $row[&#39;first_name&#39;]; ?&gt;&quot; name=&quot;firstName&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Last name:&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&lt;?php echo $row[&#39;last_name&#39;]; ?&gt;&quot; name=&quot;lastName&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Email:&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&lt;?php echo $row[&#39;email&#39;]; ?&gt;&quot; name=&quot;email&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Password:&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;password&quot; name=&quot;password&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;button type=&quot;submit&quot; name=&quot;updateProfile&quot;&gt;Update Profile&lt;/button&gt;
&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
&lt;/div&gt;
&lt;/body&gt;

</html>


</details>
# 答案1
**得分**: 1
You should not use a relative path to link to other files. If you use the relative path, you rely on your current environment. However, your environment can change. So you may have to change your code. As an example, if you move your file to a subdirectory in another subdirectory of the subdirectory, that means adjusting the code...
Instead, I would use the absolute path. This links the complete path to your file, including your environment.
This can be slower but is probably not critical for your project. It has several advantages, for example, you don't have to change the code, and it's much more efficient when you collaborate with other programmers who may be working in different environments.
Maybe see this answer here for the absolute PATH: https://stackoverflow.com/a/11604489/21940880
<details>
<summary>英文:</summary>
You should not use a relative path to link to other files. If you use the relative path, you rely on your current environment. However, your environment can change. So you may have to change your code. As example you move your file in a Sub directory in another Sub directory of the Sub directory. That means adjusting the code... 
Instead, I would use the absolute path. This links the complete path to your file, including your environment. 
This can be slower, but is probably not critical for your project. It has several advantages, for example, you don&#39;t have to change the code and it&#39;s much more efficient when you collaborate with other programmers who may be working in different environments.
Maybe see this answer here for the absolute PATH: https://stackoverflow.com/a/11604489/21940880
</details>

huangapple
  • 本文由 发表于 2023年6月13日 10:49:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定