我想通过命令行从 php 运行简单的嵌入式网络服务器
php -S 0.0.0.0:8000
并让它显示当前目录的内容。
从手册页开始,唯一可能的附加选项-S
是-t
文档根目录。我知道我可以创建一个index.php
包含以下内容的文件
<html>
<body>
<?php
$directory = "./";
$files = glob($directory . "*");
foreach($files as $file) {
echo "<a href=".rawurlencode($file).">".basename($file)."</a><br>";
}
?>
</body>
</html>
但我不想在该目录中创建文件。
不能将该代码作为命令行参数运行吗?我已经尝试使用该选项-r
,甚至使用虚拟 bash 文件和 -选项,-F
如下所示:-F <(… php code here …)
但该-S
命令似乎只接受-t
作为附加命令。
有什么诀窍可以实现吗?
PS:我知道使用 python 2 或 python 3 很容易使用 python 的嵌入式网络服务器显示当前目录
python -m SimpleHTTPServer # python 2
python -m http.server # python 3
但我想用 php 来做。