我试图建立一个PHP网站,我想测试我的PHP文件,而不是上传到我的主机。基本上是在上传之前先在我自己的机器上测试。我怎么做呢?
当前回答
这是一个简单的,确保本地运行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服务器
其他回答
如果你有一台本地机器,有正确的软件:支持PHP的web服务器,没有理由你不能像你描述的那样做。
目前我正在一台Windows XP机器上使用XAMPP,(在家里)使用Kubuntu和LAMP堆栈。
您还可以使用代码在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或任何您选择的端口
如果你在MAC MAMP上
安装并运行XAMPP: http://www.apachefriends.org/en/xampp.html
AppServ是一个在Windows中运行的小程序:
Apache PHP MySQL phpMyAdmin
它还将为Apache提供启动和停止按钮。我觉得这很有用。
推荐文章
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PHP中获得变量名作为字符串?
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 在PHP中对动态变量名使用大括号
- 如何从对象数组中通过对象属性找到条目?
- 如何从关联数组中删除键及其值?
- PHP字符串中的花括号
- PHP -如何最好地确定当前调用是否来自CLI或web服务器?
- 无法打开流:没有这样的文件或目录
- 在php中生成一个随机密码