XAMPP为何找不到文件

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

Why is XAMPP not finding the files

问题

所以,我一直在尝试在我的学校项目中使用XAMPP服务器,但是当我尝试从另一个目录中需要特定的php文件时,我收到了一个内部服务器错误,这是我的意思:

我有这样的目录结构:

XAMPP为何找不到文件

而在我的about.php文件中,我试图需要about.view.php,但是当我这样做时,我收到了一个内部服务器错误。以下是我的about.php内容:

XAMPP为何找不到文件

为什么会出现这个错误?我尝试了我能想到的一切可能的事情。请帮助我。

英文:

So, I have been trying to use the XAMPP server as part of my school project, but I am getting an internal server error when I try to require a particular php file from another directory, here is what I mean:

I have this directory heirarchy:

XAMPP为何找不到文件

and in my about.php file I am trying to require the about.view.php but I get an internal server error when I do that. Here is my about.php content:

XAMPP为何找不到文件

Why am I getting this error? and I tried every possible thing I can think of. Please, help me out.

答案1

得分: 2

### 如何从另一个文件夹引用文件?

您的 `about.php` 文件位于 `controllers` 文件夹中。您希望根据您的代码包含 `views/about.view.php` 文件。这意味着从 `about.php` 开始,您想要包含 `controller/views/about.view.php` 文件。这是不正确的,因为您的视图文件夹不在 `controllers` 文件夹内,而是位于外部。

**正确的方法是使用 `../` 向上移动一级。** 这意味着从您的 interhub 文件夹开始,请求应该被解释为 `../views/about.view.php`。因此,它从 `controllers` 文件夹开始,然后使用 `../` 返回到 `interhub` 文件夹,然后使用 `/views` 进入 `views` 文件夹。经过这些步骤,它将找到 `about.view.php` 文件,您的代码将正确运行。

```php
require_once "../views/about.view.php";

更多信息 - 文件路径

额外信息 - 如何查看错误?

好的,此文件引用已修复,检查!如果您收到进一步的 500 错误代码,这绝对不是因为这个问题,而是由于您的 PHP 代码中存在错误。您可以将错误输出到屏幕上或在 Apache error.log 文件中查看它们。

// 显示错误消息而不是空白页面上的 "错误 500"。
ini_set('display_errors', 1);
error_reporting(E_ALL);

> 警告! 仅用于家庭开发目的!<br>在实际代码中,这会存在安全漏洞!


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

### How to require file from another folder?

Your `about.php` file is located in the `controllers` folder. You want to include the `views/about.view.php` file according to your code. That means starting from `about.php`, you want to include the `controller/views/about.view.php` file. This is incorrect because your views folder is not inside the `controllers` folder. It is located outside of it.

**The correct approach is to go up one level by using `../`.** This means starting from your interhub folder, the request should be interpreted as `../views/about.view.php`. So, it starts from the `controllers` folder, then goes back one level to the `interhub` folder with `../`, and then goes into the `views` folder with `/views`. After these steps, it will find the `about.view.php` file and your code will run correctly.

```php
require_once &quot;../views/about.view.php&quot;;

More information - file path

Extra - How to can view errors?

Okay, this file reference is fixed, check! If you receive further 500 error codes, it's definitely not because of this, but rather due to an error in your PHP code. You can output the errors to the screen or view them in the Apache error.log file.

// Show error message instead of &quot;Error 500&quot; blank page.
ini_set(&#39;display_errors&#39;, 1);
error_reporting(E_ALL);

> Warning! Use it exclusively for home development purposes!<br>In live code, this poses a security vulnerability!

huangapple
  • 本文由 发表于 2023年6月6日 15:50:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412470.html
匿名

发表评论

匿名网友

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

确定