apache 如果不在 htdocs 文件夹下,PHP 将无法找到我的 php 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1397448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:09:16  来源:igfitidea点击:

PHP cannot find my php files if not under htdocs folder

phpapacheconfiguration

提问by z-boss

By someone's advice I've put all my PHP files in a separate folder (inc) on the same level as htdocs. Only index.php is left in htdocs. So, it's like this:

根据某人的建议,我已将所有 PHP 文件放在与 htdocs 相同级别的单独文件夹 (inc) 中。htdocs 中只剩下 index.php。所以,它是这样的:

C:\myproject\htdocs
- index.php

C:\myproject\htdocs
- index.php

C:\myproject\inc
- login.php
- util.php
- register.php
...

C:\myproject\inc
- login.php
- util.php
- register.php
...

Now, when I go to localhost in my browser index.php is processed and shown correctly. But any links to other php files are not found. I tried to prepend links with "inc", but they're still not found. What should I do?

现在,当我在浏览器中访问 localhost 时,index.php 被处理并正确显示。但是没有找到任何指向其他 php 文件的链接。我试图在链接前加上“inc”,但仍然没有找到。我该怎么办?

My php.ini file has this line (it's on Windows):
include_path = ".;C:\myproject\inc"

我的 php.ini 文件有这一行(它在 Windows 上):
include_path = ".;C:\myproject\inc"

回答by cletus

The point of an include directory is to put files you don't want to be accessible by a Webserver. If login.php needs to be accessible via a URL like:

包含目录的目的是放置您不希望 Web 服务器访问的文件。如果需要通过以下 URL 访问 login.php:

http://yourdomain.com/login.php

http://yourdomain.com/login.php

then don't put login.php in the include directory. Putting util.php in an include directory makes sense because you never want this:

然后不要将 login.php 放在包含目录中。将 util.php 放在包含目录中是有道理的,因为你永远不想要这个:

http://yourdomain.com/util.php

http://yourdomain.com/util.php

回答by too much php

You can't put web-accessible files outside the htdocs folder, you should be using the 'inc' folder for files like 'database_functions.inc' which should not be opened directly in your browser:

您不能将可通过网络访问的文件放在 htdocs 文件夹之外,您应该使用 'inc' 文件夹来存放诸如 'database_functions.inc' 之类的文件,这些文件不应直接在浏览器中打开:

http://localhost/index.php // directly accessible - goes in htdocs
http://localhost/login.php // directly accessible - goes in htdocs
http://localhost/register.php // directly accessible - goes in htdocs

http://localhost/util.php // you don't want people loading this file directly - put in 'inc'
http://localhost/database_functions.php // you don't want people loading this file directly - put in 'inc'

回答by Mark Rushakoff

I believe you need to escape the backslashes in your php.ini -- so it should be C:\\myproject\\inc. But as others have pointed out, you won't be able to use a browser to access the PHP files in your include directory, because the web server will not allow access to a directory outside the htdocs tree.

我相信您需要转义 php.ini 中的反斜杠 - 所以它应该是C:\\myproject\\inc。但正如其他人指出的那样,您将无法使用浏览器访问包含目录中的 PHP 文件,因为 Web 服务器不允许访问 htdocs 树之外的目录。