我试图建立一个PHP网站,我想测试我的PHP文件,而不是上传到我的主机。基本上是在上传之前先在我自己的机器上测试。我怎么做呢?
当前回答
AppServ是一个在Windows中运行的小程序:
Apache PHP MySQL phpMyAdmin
它还将为Apache提供启动和停止按钮。我觉得这很有用。
其他回答
我经常使用以下命令来旋转我的PHP Laravel框架:
$ php artisan serve --port=8080
or
$ php -S localhost:8080 -t public/
在上述命令中: Artisan是Laravel中包含的命令行界面,它使用服务调用内置的php服务器
使用内置的web服务器运行。
php -S <addr>:<port> -T
在这里, -S:切换到运行内置web服务器。 -T:开关 为内置web服务器指定文档根目录。
您还可以使用代码在PHP中创建自己的服务器!
<?php
set_time_limit(0);
$address = '127.0.0.1';
$port =4444;
$server = '$address + $port';
// <-- Starts Server
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
echo "\n Server is running on port $port waiting for connection... \n\n";
while(1)
{
socket_listen($sock);
$client = socket_accept($sock);
$input = socket_read($client, 443);
$incoming = array();
$incoming = explode("\r\n", $input);
$fetchArray = array();
$fetchArray = explode(" ", $incoming[0]);
$file = $fetchArray[1];
if($file == "/"){
$file = "src/browser.php";//当服务器启动时,这个文件是打开的!
} else {
$filearray = array();
$filearray = explode("/", $file);
$file = $filearray[1];
}
echo $fetchArray[0] . " Request " . $file . "\n";
// <-- Control Header
$output = "";
$Header = "HTTP/1.1 200 OK \r\n" .
"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" .
"Content-Type: text/html \r\n\r\n";
$Content = file_get_contents($file);
$output = $Header . $Content;
socket_write($client,$output,strlen($output));
socket_close($client);
}
print('server running..');
运行这段代码,然后打开浏览器到localhost:443或任何您选择的端口
如果您正在使用Windows,那么WPN-XM服务器堆栈可能是一个合适的替代方案。
使用Apache Friends XAMPP。它将设置Apache HTTP服务器,PHP 5和MySQL 5(据我所知,可能还有更多)。你不需要知道如何配置apache(或任何模块)来使用它。
你会有一个Apache提供的htdocs目录(可以通过http://localhost/访问),应该可以把你的PHP文件放在那里。在我的安装中,它位于C:\xampp\htdocs。
这是一个简单的,确保本地运行php服务器的方法:
php -S 0.0.0.0:<PORT_NUMBER>
PORT_NUMBER为1024 ~ 49151的整数
例如:php -S 0.0.0.0:8000
注:
如果你使用localhost而不是0.0.0.0,你可能会碰到一个 拒绝连接错误。 如果想让任何接口都可以访问web服务器,请使用0.0.0.0。 如果URI请求没有指定 文件,则index.php或index.html在给定目录下都是 返回。
给定以下文件(router.php)
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false; // serve the requested resource as-is.
} else {
echo "<p>Welcome to PHP</p>";
}
?>
运行这个…
php -S 0.0.0.0:8000 router.php
... 并在浏览器中导航到http://localhost:8000/,将显示如下:
Welcome to PHP
参考:
内置web服务器
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 加载HTML文件到WebView
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利