php 常用的日期函数
1540949469 时间戳 当前距离1970年1月1日 0:0:0 的秒数 返回单位是秒
//echo (time());
//date()日期函数
echo (date("y-m-d h:i:s")); //获取当前时间 y-m-d 年-月-日 h:i:s 时-分-秒
php 设置默认的时区
date_default_timezone_set("PRC"); //中国(北京)的时区
//echo (date_default_timezone_get()); //获取设置的时区
是在配置文件设置 php.ini date.timezone = PRC
补充:
几个数组函数
array_merge() 合并数组或者合并多个数组
array_sum 统计所有的总和
array_slice(截取的数组,截取的元素(这里的元素不是下标))
array_slice (截取的数组,起始元素,长度)
strrpos ( ) 1获取最后出现的位置 2从右向左截取
php 文件函数
is_writeable("文件路径") 判断文件是否可写 //文件是否存在
如果可写 返回true 文件不存在或者只读 返回false
is_executable("文件路径") 判断文件是否可以执行 windows .exe .sys .com 都是可执行文件
file_exists() 文件或者目录是否存在
filesize() 文件的大小(读具体文件的大小) 单位是b 字节
1G = 1024M
1M = 1024KB
1KB = 1024B
php 文件创建时间 文件修改时间 文件访问时间
filectime("文件路径") create:创建 获取文件的创建时间
filemtime("文件路径") modify:修改 获取文件的修改时间
fileatime("文件路径") activor:访问 获取文件的访问时间
opendir(“打开目录地址”) 打开一个目录
readdir(“读取的内容”) 读取目录中的内容 返回的是个路径 . .. ./ ../
php文件上传 图片上传功能
php用表单上传 步骤: 1.上传位置2.文件类型3.文件大小4.文件重新命名5.文件保存到数据库
用ajax异步提交
(1):表单设置上传图片 表单上加入 enctype="multipart/form-data"
(2):$_FILES 超级数组
Array
(
[imgs] => Array
(
[name] => 6.jpg 图片名字
[type] => image/jpeg 图片类型
[tmp_name] => C:\Users\Administrator\AppData\Local\Temp\php75C3.tmp
图片存放地址(虚拟地址)
[error] => 0 0:上传成功 1:上传失败
[size] => 132244 图片大小 返回的字节
)
)
(3):上传图片的函数 move_uploaded_file("原有路径","现有路径") 现有路径覆盖原有路径 <=> copy()
上传图片的函数 copy()
(4):处理图片命名
(5):图片上传的格式
(6):控制图片大小
(7):图片的地址存入到数据库
例子:
<form method="post" action="10.31.php" enctype="multipart/form-data">
<input type="file" name="imgs" /><br/>
<input type="submit" value="上传图片"/>
</form>
<?php
if(!empty($_FILES)){
//判断文件大小
if($_FILES['imgs']['size'] > 2*1024*1024)
{
die("你上传的文件太大");
}
//判断文件的类型
$path = array(".jpg",".png",".gif",".bmp");
$pathname = substr($_FILES['imgs']['name'],strrpos($_FILES['imgs']['name'],"."));
if(!in_array($pathname,$path))
{
die("你上传的文件类型不符合");
}
//文件重命名
$file_img = time().rand(99,5000).$pathname;
//创建文件夹
$file = "uploads/".date("y");
if(!file_exists($file))
{
mkdir($file);//创建一个文件
}
$file = $file."/".date("m-d");
if(!file_exists($file))
{
mkdir($file);//创建一个文件
}
$path = $file."/".$file_img;
//上传图片
copy($_FILES['imgs']['tmp_name'],$path);
}
?>
全静态缓存 伪静态缓存
$b = file_get_contents(“获取文件信息”)
file_put_contents("读取到自己文件里面",$b)
加载文件
include("xx.html"); //加载视图文件
图片绘画 验证码
先开启gd库
application programme interface interface:接口 application:应用程序
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); 设置图片绘画(图片大小 )
header("Content-type: image/png;charset=utf8");设置图片的类型,和字符集
$im = imagecreatetruecolor(宽度, 高度);返回图像资源
$back_color = imagecolorallocate($im, 红, 绿, 蓝);使用三元色画背景
imagefilledrectangle(图像资源,右上x,右上y,左下x,左下y,颜色) //画矩形
imagerectangle($im, 左上x, 左上y, 右下x, 右下y,颜色资源); //画边框
imagesetpixel($im, x坐标,y坐标,颜色资源) //画干扰点
imageline图像资源,右上x,右上y,左下x,左下y,颜色) //画直线
imagearc($im,中点x,中点y,横距离,竖距离,起始角度,结束角度,颜色) //画曲线
imagepng($im);//生成图像
imagedestroy($im);//销毁图片
imagefttext() 能绘制UTF-8编码的字符串汉字
小例子:
<?php
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=utf8");
//指定边框的宽度和高度
$s_width = 100;
$h_height= 80;
$im = imagecreatetruecolor($s_width, $h_height);
//画矩形(边框)
$back_color = imagecolorallocate($im,255,255,255); //使用三元色画背景
imagefilledrectangle($im,0,0,$s_width,$h_height,$back_color);//画框
//画干扰点
for($i=0;$i<20;$i++)
{
$tpixe_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
//imagesetpixel($im, x坐标,y坐标,颜色资源)
imagesetpixel($im,rand(0,$s_width),rand(0,$h_height),$tpixe_color);
}
//画直线
for($i=0;$i<10;$i++)
{
$line_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,$s_width),rand(0,$h_height),rand(0,$s_width),rand(0,$h_height),$line_color );
}
//画弧线
for($i=0;$i<10;$i++)
{
//imagearc($im,中点x,中点y,横距离,竖距离,起始角度,结束角度,颜色)
$arc_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagearc($im,rand(0,$s_width),rand(0,$h_height),rand(0,100),rand(0,100),rand(-90,90),rand(70,360),$arc_color);
}
//画数字图像
$str = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
$x =20;
$y =40;
for($i=0;$i<4;$i++)
{
//随机数字
$char = $str[rand(0,strlen($str)-1)];
$font_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagefttext($im, 20 , rand(-45,45), $x + $i*20, $y, $font_color, "ariblk.ttf", $char);
}
//生成图像
imagepng($im);
//销毁图片
imagedestroy($im);//通常与生成图片连用
?>
补充几道练习题: