关于phpstorm安装
sudo apt install php7.2-cgi
php内置服务器
多入口
- 当前目录
php -S localhost:6060
- 指定目录
php -S localhost:6060 -t .
- 如果请求url中没有指定访问的文件, 那么默认执行url对应目录下的
index.php
或index.html
- 如果没有这两个文件, 则返回
Not Found
, 即不支持浏览目录
单入口
- 指定路由脚本
php -S localhost:6060 router.php
-
每次请求都会先执行路由脚本
- 如果脚本返回
false
, 则返回请求的文件 - 否则把输出返回到浏览器
- 如果脚本返回
-
路由脚本示例(也可用于多入口)
<?php
if (preg_match('/\.(?:mp4|png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false; // 直接返回请求的文件
} else {
echo "<p>Welcome to PHP</p>"; // 输出到浏览器
}
?>
php返回文件
当请求php
文件时, 返回其他的文件
通过设置响应头下载文件
$fileName = "video.mp4";
$file = fopen($fileName, "r");
header("Content-type: application/octet-stream");
header("Content-Length: " . filesize($fileName));
header("Content-Disposition: attachment; filename=" . $fileName);
echo fread($file, filesize($fileName));
fclose($file);
通过重定向
header("Location: video.mp4");
html加载php
注意:
- 分号
- 文件加载先后
输出到console
<?
function console_log($src)
{
if (is_array($src) || is_object($src))
{
echo("console.log(" . json_encode($src) . ");");
}
else
{
echo("console.log(" . $src . ");");
}
}
console_log("fuck");
js调用php变量
<?php
function tranlate_js($src, $dest)
{
if (is_array($dest) || is_object($dest))
{
console_log("...");
return;
}
if (is_array($src) || is_object($src))
{
echo("var " . $dest . "=" . json_encode($src) . ";");
}
else
{
echo("var " . $dest . "=" . $src . ";");
}
}
$arr = array("you", 2, 3);
tranlate_js($arr, "shit"); // var shit = $array
console.log(shit);
php调用js方法
function my_log(a)
{
console.log(a);
}
$str = "'you'";
echo "my_log(" . $str . ");";