如何在我的计算机上运行 htdocs 目录之外的 PHP 文件?

发布于 2024-09-04 15:53:32 字数 224 浏览 3 评论 0原文

我刚刚在计算机上安装了 Apache 和 PHP,以便能够在本地运行 PHP 文件。

如果 PHP 文件位于 Apache2.2\htdocs 目录中,我可以运行该文件。

是否可以在该目录之外运行 PHP 文件?

我正在寻找一个简单的解决方案,因为我需要的只是编写一个小的 PHP 代码并在本地尝试。

我希望能够通过右键单击 PHP 文件来运行它 ->使用火狐打开

I just installed Apache and PHP on my computer to be able to run PHP files locally.

I can run a PHP file if it is located in Apache2.2\htdocs directory.

Is that possible to run PHP files outside this directory ?

I'm looking to a simple solution, because all I need is to write a small PHP code and try it locally.

I would like to be able to run the PHP file by right clicking it -> Open with Firefox

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一袭水袖舞倾城 2024-09-11 15:53:32

您可以使用命令行从任何地方运行 PHP 脚本:

php yourscript.php

You can run a PHP script from anywhere using the command line:

php yourscript.php
哥,最终变帅啦 2024-09-11 15:53:32

有一些方法可以做到这一点,但最简单的方法是使用“include”或“required”php 命令:

<?
include ('/somewhere/outside/of/your/htdocs/file.php');
?>

请注意,您可能需要关闭“open_basedir”php 指令。

There is some ways to do it, but the simplest way is using 'include' or 'required' php command:

<?
include ('/somewhere/outside/of/your/htdocs/file.php');
?>

Please note you may need to turn off 'open_basedir' php directive.

[旋木] 2024-09-11 15:53:32

较新版本的 php 附带 php-cli:命令行界面。因此,要运行 php 代码,您只需输入:

 php some_code.php

Newer versions of php come with php-cli: a command line interface. So to run php code, you just need to type:

 php some_code.php
一身仙ぐ女味 2024-09-11 15:53:32

或者您可以将 apache 配置中的路径更改为另一个目录

Or you can change paths in apache config to another dir

埖埖迣鎅 2024-09-11 15:53:32

您无法使用 Firefox 打开它。
因为firefox与win PHP无关。
因此,您需要从网络服务器请求此 URL

使用 http://127.0.0.1/ 创建一个 Windows 快捷方式file.php

或您喜欢点击网页链接的任何方式

you cannot open it with firefox.
Because firefox has nothing to do win PHP.
So, you need to request this URL from a web-server

Create a windows shortcut with http://127.0.0.1/file.php

or whatever way you prefer to click web links

怼怹恏 2024-09-11 15:53:32

是的。您没有说明是否要从 Web 服务器或命令行运行它,但您可以在 Web 根目录之后使用 include

Yes. You don't say if you want to run it from the web server, or from the command line, but you can use include past the web root.

熊抱啵儿 2024-09-11 15:53:32

您可以从命令行运行它,

也可以在 htdocs 中创建一个“包装器”脚本,其中“包含”来自 htdocs 目录外部的文件

You can either run it from the command line,

or you can create a "wrapper" script within your htdocs that "includes" the file from outside of the htdocs directory

赠佳期 2024-09-11 15:53:32

我一直以同样的方式想知道这个问题,假设你在驱动器中有一个文件夹,而不是驱动器 C 中的 apache/htdocs 文件夹,并且你想直接在这个文件夹中开发。但想一想,您会将文件夹放在其他位置并期望它在网络上运行吗?现在方式。你为什么要这么做?

I had always wondered about it the same manner, say you have a folder in a drive either than the apache/htdocs folder in drive C, and you would like to develop directly in this folder. But think about it this way, would you put your folders else where and expect it to be running on the web? Now Way. Why would you do that?

动次打次papapa 2024-09-11 15:53:32

转到 conf 文件夹并打开 httpd 文件更改
DocumentRoot“C:/your/path”和
目录“C:/your/path”
如果它说禁止访问,

请转到此链接

错误消息“禁止您无权访问/在此服务器上”

Goto conf folder and open httpd file change
DocumentRoot "C:/your/path" and
Directory "C:/your/path"
This should work

if it says forbidden access go to this link

Error message "Forbidden You don't have permission to access / on this server"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文