PHP原生操作的使用
1、PHP读取文件
// 打开文件
$file = fopen("zh.html", "r+") or die("unable to open file");
// 读取文件
$content = fread($file, filesize("zh.html"));
// 打印文件
print_r($content);
// 关闭文件
fclose($file);
2、PHP写入文件
$file = fopen("z.html", "a") or die("读取文件失败");
$insert = "<meta charset='utf8'>\n<h1>这是一段HTML代码</h1>\r\n";
fwrite($file, $insert);
fwrite($file, "<title>我是一个标题</title>");
fclose($file);
3、PHP读取表单
$p = $_REQUEST;
$e = $_ENV;
print_r($p);
sleep(3);
header("Location: zh.html"); // PHP的跳转
4、PHP原生的渲染页面以及页面传值
class Base
{
// 存key
public $key;
// 存value
public $value;
// 存数据
public $array;
public function assign($key, $value)
{
if (is_array($value)) {
$this->array["$key"] = $value;
}else{
$this->array["$key"] = compact("$value");
}
}
public function display($tpl)
{
$this->assign($this->key, $this->value);
extract($this->array);
if (file_exists($tpl)) {
include $tpl;
}
}
}
class IndexController extends Base {
public function index($tpl, $data = [])
{
$this->assign("data", $data);
$this->display($tpl);
}
}
$i = new IndexController();
$i->index("zh.html", ["index" => $_REQUEST]);
HTML部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<h1>你好</h1>
<?php var_dump($data) ?>
<button onclick="test()">单机</button>
</body>
<script>
/*$.getJSON("https://novel-cn.oss-cn-shanghai.aliyuncs.com/content/839f6cbf56b76a1bd8b29e60/0828f909d0d1d8778cd8f6e2", function (res) {
console.log(res)
})*/
function test() {
console.log(11)
$.ajax({
type: "get",
url: "https://novel-cn.oss-cn-shanghai.aliyuncs.com/content/839f6cbf56b76a1bd8b29e60/0828f909d0d1d8778cd8f6e2",
dataType: "jsonp",
success: function () {
console.log(111)
}
});
}
</script>
</html>