文章目录
语法基础
1.文件
(1) 文件操作模式
操作符 | 模式名称 | 描述 |
---|---|---|
r | 读模式 | 打开文件,从头开始, **注意: ** 文件必须存在 |
r+ | 读写模式 | 打开文件,从头开始读写, 注意: 文件必须存在 |
w | 只写模式 | 打开文件,从文件头开始写文件, 注意: 文件如果存在,则会清空文件;如果不存在,会自动创建文件 |
w+ | 读写模式 | 同上 |
a | 追加 | 在文件的末尾进行写操作 |
a+ | 追加 | 在文件末尾进行读写操作 |
注意 |
---|
注意; 在操作符后面加上b,表示该文件会以二进制的方式读或者写 |
(2) 文件目录
主要函数:
- $ opendir(目录名)获取文件目录
- str readdir($)返回当前文件下一个文件
实例:
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
<meta name="viewport" content="width=device-width, user-scalable=no">
<style>
*{
margin:0;
padding:0;
}
h1{
font-size:80px;
text-align:center;
}
li{
font-size:30px;
margin-left:80px;
margin-top:20px;
}
</style>
</head>
<body>
<h1>目录</h1>
<div>
<ol>
<?php
$dir_name=opendir("../htdocs");
while($file_name=readdir($dir_name)){
if($file_name!=".."&&$file_name!="."){
//第一个变量为域名,第二个变量为端口 $url='http://'.$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"]."/".$file_name;
echo '<li><a href="'.$url.'">'.$file_name.'</a></li>';
}
}
?>
</ol>
</div>
</body>
</html>
加强版代码: 实现管理员登录,自适应
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
<meta name="viewport" content="width=device-width, user-scalable=no">
<style>
*{
margin:0;
padding:0;
}
html{
font-size:100px;
}
h1{
font-size:.8rem;
text-align:center;
}
li{
font-size:.3rem;
margin-left:.8rem;
margin-top:.2rem;
}
div:nth-child(2){
margin-top:-.5rem;
}
input:nth-child(1){
margin-left:.25rem;
width:60%;
height:.3rem;
border-radius:8px;
box-shadow:4px 4px 4px;
}
input:nth-child(2){
width:20%;
height:.3rem;
box-shadow:4px 4px 4px;
border-radius:8px;
}
</style>
</head>
<body>
<h1>目录</h1>
<div>
<form name="input" method="get" action="./index.php">
<input type="text" name="e" value="管理员密码">
<input type="submit" value="登录">
</form>
<ol>
<?php
//中文编码声明
header("Content-type:text/html,charset=utf-0p8");
$dir_name=opendir("../htdocs");
//设置数组,保存参数
$contains=array();
while($file_name=readdir($dir_name)){
if($file_name!=".."&&$file_name!="."){
//第一个变量为域名,第二个变量为端口
$url='http://'.$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"]."/".$file_name;
//分割字符串
$expanded_name=explode('.',$file_name)[1];
//将信息存入数组中
$contains[count($contains)]=array($url,$file_name,$expanded_name);
}
}
for($i=0;$i<count($contains);$i++){
if($_GET["e"]=="cuihao"){
echo '<li><a href="'.$contains[$i][0].'">'.$contains[$i][1].'</a></li>';
}else{
if($contains[$i][2]!="php"){
echo '<li><a href="'.$contains[$i][0].'">'.$contains[$i][1].'</a></li>';
}
}
}
?>
</ol>
</div>
</body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
/**屏幕适配**/
function custom_screen(){
//获取html,html的fontSize,屏幕大小
var element_html=document.documentElement;
//alert(element_html);
var font_html=window.getComputedStyle(element_html);
//alert(font_html.fontSize);
var screen_width=document.body.clientWidth;
//alert(screen_width);
//设置比例
element_html.fontSize=39300/screen_width+"px";
}
/**主函数**/
function main(){
custom_screen();
alert("执行完毕");
}
/**执行**/
$(document).ready(function(){
main();
})
</script>
</html>
(3) 读写文件
- $fp=fopen(“filename”,“w”)
- fread($fp,filesize(filename))
- fwrite($fp,text)
- fclose($fp)
2.页面缓存
将缓存的数据释放出来
将释放出来的数据显示在浏览器上
注意: 使用顺序为 ob_flush()->flush()
网络编程
1. curl类库
(1) curl_init()
步骤:
- 创建curl句柄( curl_init() ),打开箱子。
- 添加属性( curl_setopt(句柄,键,值) ),往箱子中放入东西
- 发送请求( curl_exec(句柄) ),发送快递
- 结束会话( curl_close() ),结账
我把操作curl发送请求的过程比喻成了寄快递的过程。简单易懂,无论code多么复杂,但实际上都为这四个步骤,只不过对发送和请求的处理比较复杂。
(2) curl_multi()
由于php没有多线程模块,因此当网络请求过多时便会显得很软肋。接下来将介绍到网络并发模式 curl_multi()
步骤:
- 创建 init curl_init() 句柄
- 设置参数 ( curl_setopt(句柄,键,值) )
- 创建 inti curl_multi_init() 批句柄
- 向批句柄中添加请求 curl_multi_add_handle(批句柄,子句柄)
- 执行请求 int curl_multi_exec(批句柄,状态),通过do while判断状态是否大于0,是继续,否完成。
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
或者
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
注意: 以上两种代码都是判断批句柄是否执行完成,但是第二种方法开销过大。同时也可以加上异常检验 int curl_errno(句柄) 和 string curl_error(句柄)
6.得到数据 string curl_multi_getcontent(子句柄),需要设置参数(CURLOPT_RETURNTRANSFER,true)
7.关闭句柄 curl_multi_close(批句柄)
(3)curl_setopt()
- curl_setopt($ch,CURLOPT_NOPROGRESS,1)
- curl_setopt($ch,CURLOPT_PROGRESSFUNCTION,“function”)
分析: 以上两个参数可以实现获取加载进程,function回调函数中的参数有先后顺序,即:
/* * $ch
* $countDownloadSize 总下载量
* $currentDownloadSize 当前下载量
* $countUploadSize
* $currentUploadSize
* */
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)
分析: 该属性设置获取数据不输出在浏览器,若要输出,可设置为0。
2. Snoopy类库
注意: 在使用Snoopy时,需要修改配置文件,找到extent php_openill,去掉前面的;号
使用步骤:
- 创建Snoopy对象
- 添加Snoopy对象属性
- 开始调用fetch方法,发送求情
- 得到结果(Snoopy对象的属性results)