php 如何在本地主机上运行文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/312881/
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-08-24 22:21:40  来源:igfitidea点击:

How do I run a file on localhost?

phplocalhostxamppincludessi

提问by James Musser

How do I actually run a file on localhost? I know it is working, but how do I run a file on it, and how do I verify that the file is in fact running on localhost?

我如何在本地主机上实际运行文件?我知道它正在运行,但是如何在其上运行文件,以及如何验证该文件实际上是在 localhost 上运行的?

Server newbie here, additional questions (I have xampp running Apache 2.2):From your responses it sounds like I have to type in the path in the browser in order to open the file on the localhost. So, if I want to have the file in a subdirectory, I would have to type the full path including the directory?

服务器新手在这里,其他问题(我有 xampp 运行 Apache 2.2):从您的回答中,听起来我必须在浏览器中输入路径才能在本地主机上打开文件。所以,如果我想将文件放在子目录中,我必须输入包括目录的完整路径?

Is there a way to have the browser point to the localhost while I am working on my html, instead of having to type in the path all the time?

有没有办法在我处理 html 时让浏览器指向本地主机,而不必一直输入路径?

I am working in Dreamweaver CS3, and want to use includecommands (either SSI or PHP), but not have to keep putting my files up on HostGator in order to see if they are working.

我在 Dreamweaver CS3 中工作,想使用包含命令(SSI 或 PHP),但不必一直将我的文件放在 HostGator 上以查看它们是否正常工作。

What is the best way to do this (please be specific, I know nothing). If there is a detailed tutorial anywhere, really appreciate a link.

这样做的最佳方法是什么(请具体说明,我一无所知)。如果任何地方都有详细的教程,非常感谢链接。

Thank you

谢谢

回答by markus

Ok, thanks for the more specific info, ppl may remove their downvotes now...

好的,感谢提供更具体的信息,ppl 现在可能会删除他们的反对票...

What you are proposing is a very common thing to do! You want to run your web application locally without uploading it to your host yet. That's totally fine and that's what your Apache is there for. Your Apache is a web server meaning its main purpose is to serve HTML, PHP, ASP, etc. files. Some like PHP; it first sends to the interpreter and then sends the rendered file to the browser. All in all: it's just serving pages to your browser (the client).

你提议的事情是很常见的事情!您想在本地运行您的 Web 应用程序,而无需将其上传到您的主机。这完全没问题,这就是您的 Apache 的用途。您的 Apache 是一个 Web 服务器,这意味着它的主要目的是为 HTML、PHP、ASP 等文件提供服务。有些喜欢PHP;它首先发送到解释器,然后将渲染的文件发送到浏览器。总而言之:它只是为您的浏览器(客户端)提供页面。

Your web server has a root directory which is wwwroot (IIS) or htdocs (apache, xampp) or something else like public_html, www or html, etc. It depends on your OS and web server.

您的网络服务器有一个根目录,它是 wwwroot (IIS) 或 htdocs (apache, xampp) 或其他类似 public_html、www 或 html 等的目录。这取决于您的操作系统和网络服务器。

Now if you type http://localhostinto your browser, your browser will be directed to this webroot and the server will serve any index.html, index.php, etc. it can find there (in a customizable order).

现在,如果你输入http://localhost到浏览器,浏览器将被引导到这个根目录和服务器将提供任何index.htmlindex.php等它可以找到有(可自定义的顺序)。

If you have a project called "mytutorial" you can enter http://localhost/mytutorialand the server will show you the index-file of your tutorial, etc. If you look at the absolute path of this tutorial folder then it's just a subfolder of your webroot, which is itself located somewhere on your harddrive, but that doesn't matter for your localhost.

如果您有一个名为“mytutorial”的项目,您可以输入http://localhost/mytutorial,服务器将向您显示教程的索引文件等。如果您查看本教程文件夹的绝对路径,那么它只是您的 webroot 的子文件夹,即本身位于您硬盘上的某个位置,但这对您的本地主机无关紧要。

So the relative path is

所以相对路径是

http://localhost/mytutorial

while the absolute path may be

而绝对路径可能是

c:/webservices/apache/www

or

或者

c:/xampp/htdocs

If you're working with Dreamweaver you can simplify the testing process by setting up your local server as a testing server in your project settings. Try it! It's easy. Once it's done, you can just press the browser icon with any of your files and it will open on localhost.

如果您使用 Dreamweaver,则可以通过在项目设置中将本地服务器设置为测试服务器来简化测试过程。尝试一下!这很简单。完成后,您只需按下包含任何文件的浏览器图标,它将在本地主机上打开。

回答by Daniel Schaffer

I'm not really sure what you mean, so I'll start simply:

我不太确定你的意思,所以我会简单地开始:

If the file you're trying to "run" is static content, like HTML or even Javascript, you don't need to run it on "localhost"... you should just be able to open it from wherever it is on your machine in your browser.

如果您尝试“运行”的文件是静态内容,例如 HTML 甚至 Javascript,则不需要在“本地主机”上运行它...浏览器中的机器。

If it is a piece of server-side code (ASP[.NET], php, whatever else, uou need to be running either a web server, or if you're using Visual Studio, start the development server for your application (F5 to debug, or CTRL+F5 to start without debugging).

如果它是一段服务器端代码(ASP[.NET]、php 或其他任何代码),您需要运行 Web 服务器,或者如果您使用的是 Visual Studio,请为您的应用程序启动开发服务器(F5调试,或 CTRL+F5 启动而不调试)。

If you're using a web server, you'll need to have a web site configured with the home directory set to the directory the file is in (or, just put the file in whatever home directory is configured).

如果您使用的是 Web 服务器,则需要配置一个网站,并将主目录设置为文件所在的目录(或者,只需将文件放在配置的任何主目录中)。

If you're using Visual Studio, the file just needs to be in your project.

如果您使用的是 Visual Studio,则该文件只需要在您的项目中即可。

回答by Rob Kennedy

Localhost is the computer you're using right now. You run things by typing commands at the command prompt and pressing Enter. If you're asking how to run things from your programming environment, then the answer depends on which environment you're using. Most languages have commands with names like systemor execfor running external programs. You need to be more specific about what you're actually looking to do, and what obstacles you've encountered while trying to achieve it.

本地主机是您现在使用的计算机。您可以通过在命令提示符下键入命令并按 Enter 来运行。如果您要问如何从您的编程环境运行东西,那么答案取决于您使用的环境。大多数语言都有名称类似于systemexec用于运行外部程序的命令。你需要更具体地说明你真正想要做什么,以及你在尝试实现它时遇到了哪些障碍。

回答by Yagnik Patel

You can do it by running with following command.

您可以通过运行以下命令来完成。

php -S localhost:8888

回答by caglarozdag

Looking at your other question I assume you are trying to run a php or asp file or something on your webserver and this is your first attempt in webdesign.

查看您的其他问题,我假设您正在尝试在您的网络服务器上运行 php 或 asp 文件或其他内容,这是您在网页设计中的第一次尝试。

Once you have installed php correctly (which you probably did when you got XAMPP) just place whatever file you want under your localhost (/www/var/html perhaps?) and it should run. You can check this of course at localhost/file.php in your browser.

一旦您正确安装了 php(您在获得 XAMPP 时可能已经这样做了),只需将您想要的任何文件放在 localhost 下(也许是 /www/var/html?),它就会运行。您当然可以在浏览器中的 localhost/file.php 中检查这一点。

回答by shubhshrma

Think of it this way.

这么想吧。

Anything that you type after localhost/is the path inside the root directory of your server(www or htdocs).

您输入的任何内容都是localhost/服务器根目录中的路径(www 或 htdocs)。

You don't need to specify the complete path of the file you want to run but just the path after the root folder because putting localhost/takes you inside the root folder itself.

您不需要指定要运行的文件的完整路径,而只需指定根文件夹之后的路径,因为 put 会将localhost/您带入根文件夹本身。