|
文件系统
1. 文件复制
bool copy (string $source , string $dest ) PHP 4, PHP 5)
<?php
header("content-type:text/html;charset=utf-8");
$source_path = "index.php";
$dest_path = "copy_content.php";
//将$source_path路径下的文件拷贝到目的路径下
if(copy($source_path, $dest_path)){
echo "复制成功";
}else{
echo "复制失败";
}
?>
| 2. 判断文件是否存在
bool file_exists( string $filename )
<?php
header("content-type:text/html;charset=utf-8");
$source_path = "ndex.php";
$dest_path = "copy_content.php";
//将$source_path路径下的文件拷贝到目的路径下
if(file_exists($source_path)){
if(copy($source_path, $dest_path)){
echo "复制成功";
}else{
echo "复制失败";
}
}else{
echo "文件不存在";
}
?>
| 3. 文件移动或者重命名
<?php
header("content-type:text/html;charset=utf-8");
//如果两个文件在同一个目录下,则是重命名,如果不在统一目录下则是移动源文件
$old_name = "test.php";
$new_name = "dest/test.php";
if(file_exists($old_name)){
echo "文件存在";
if(rename($old_name, $new_name)){
echo "文件移动成功";
}else{
echo "文件移动失败";
}
}else{
echo "文件不存在";
}
?>
| 4.删除文件
<?php
header("content-type:text/html;charset=utf-8");
$path = "dest/index.php";
if(file_exists($path)){
if(unlink($path)){
echo "文件删除成功";
}else{
echo "文件删除失败";
}
}
?>
| 5.建立目录
<?php
header("content-type:text/html;charset=utf-8");
$path = "c:/filename";
if(file_exists($path)){
echo "该目录存在";
}else{
if(mkdir($path)){
echo "创建成功";
}else{
echo "创建失败";
}
}
?>
| 6.删除目录
<?php
header("content-type:text/html;charset=utf-8");
$path = "c:/filename";
if(!file_exists($path)){
if(mkdir($path)){
echo "创建成功";
}
}else{
if(rmdir($path)){
echo "删除成功";
}else{
echo "删除失败";
}
}
?>
| 7.遍历目录(文件夹)
<?php
header("content-type:text/html;charset=utf-8");
$path = "/";
if(file_exists($path)){
$file_asc = scandir($path);//升序排列文件
$file_desc = scandir($path,1);//降序排列文件
echo "<p>该目录下的文件:<br>";
foreach ($file_asc as $key=>$value) {
echo $key."===$value"."<hr>";
}
}
?>
| 8.递归遍历目录文件
<?php
function getDirTree($path){
$tree = array();
$tmp = array();
if(!is_dir($path))
return null;
$files =scandir($path);
foreach ($files as $value) {
if($value=="."||$value=="..")
continue;
$full_path = $path."/".$value;
if(is_dir($full_path)){
$tree[$value] = getDirTree($full_path);
}else{
$tmp[] = $value;
}
}
$tree = array_merge($tree,$tmp);
return $tree;
}
$path = '../Demos';
foreach (getDirTree($path)as $key=>$value) {
if(is_array($value)){
foreach ($value as $para) {
echo $para."<br>";
}
}
echo $key."====".$value."<hr>";
}
?>
| GD函数库
1. 载入图片
<?php
//载入图片
//图路径
$path = "风景.jpg";
//避免中文图片名称导致乱码的问题
$path = iconv("utf-8","gbk", $path);
//从图像文件创建图像句柄
$img = imagecreatefromjpeg($path);
//发送header头部信息
header("content-type:text/html;charset=utf-8");
//输入图像
imagepng($img);
//释放与图像资源有关的内存
imagedestroy($img);
?>
| 2. 获取图片信息
<?php
function createImg($filepath){
if(!file_exists($filepath)){
return false;
}
$img_info = getimagesize($filepath);
//getimagesiez()函数获取很多参数 第三个参数为图片的类型
switch ($img_info[2]){
case 1:
$func_img = "imagecreatefromgif";//函数名称
$img_type = "gif";
break;
case 2:
$func_img = "imagecreatefromjpeg";//函数名称
$img_type = "jpeg";
break;
case 3:
$func_img = "imagecreatefrompng";//函数名称
$img_type = "png";
break;
default:
return false;
}
$img = $func_img($filepath);
return array($img,$img_type);
}
$path = "风景.gif";
$path = iconv("utf-8","gbk", $path);
$img_array = createImg($path);
$img = $img_array[0];
$img_type = $img_array[1];
header("content-type:image/$img_type;");
imagepng($img);
imagedestroy($img);
?>
| 3.添加水印
<?php
/**
* 英文文字水印
*/
$path = "风景.jpg";
$path = iconv("utf-8","gbk", $path);
//获取图片信息
$img_array = getimagesize($path);
$img_width = $img_array[0];
$img_height =$img_array[1];
//创建图像句柄
$img = imagecreatefromjpeg($path);
//设置水印颜色
$red = imagecolorallocate($img, 255, 0, 0);
//设置字体大小
$font = 9;
//设置水印位置
$str_x = $img_width/4;
$str_y = $img_height/5;
//设水印内容
$str = " wo ai ni";
//$str = iconv("utf-8", "gbk", $str);
//将水印输出到图片上
imagestring($img, 100, $str_x, $str_y, $str, $red);
header("content-type:image/jpeg");
//输出图片
imagejpeg($img);
imagedestroy($img);
?>
| 3. 添加自定义水印
<?php
$path = "风景.jpg";
$path = iconv("utf-8","gbk", $path);
//获取图片信息
$img_array = getimagesize($path);
$img_width = $img_array[0];
$img_height =$img_array[1];
//创建图像句柄
$img = imagecreatefromjpeg($path);
//设置水印颜色
$red = imagecolorallocate($img, 255, 0, 0);
//字体路径
$font_file = "mzd.ttf";
//字体倾斜角度
$font_angle = 20;
$font_size = 30;
$x = 100;
$y = 180;
$str = "尚图云相册";
imagettftext($img, $font_size, $font_angle, $x, $y, $red, $font_file, $str);
header("content-type:image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>
| 4. 添加图片水印
<?php
$path = "风景.jpg";
$path = iconv("utf-8","gbk", $path);
//获取图片信息
$img_array = getimagesize($path);
$img_width = $img_array[0];
$img_height =$img_array[1];
//创建图像句柄
$img = imagecreatefromjpeg($path);
//设置水印图片
$source_img_path = "Cd.gif";
$source_array = getimagesize($source_img_path);
$source_width = $source_array[0];
$source_height = $source_array[1];
//水印图片句柄
$source_img = imagecreatefromgif($source_img_path);
//设置原图片定位坐标
$dst_x = ($img_width-$source_width)/2;
$dst_y = ($img_height-$source_height)/2;
$src_x =0;
$src_y =0;
$src_w =$source_width;
$src_h =$source_height;
//从水印图片复制到一部分
imagecopy($img, $source_img, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
header("content-type:image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>
| 最简单的随机验证码:
<?php
//随机字符串0-1 a-Z
$str = "QWEPOIUYTRLAKSJDHFGZMXNBCqwertyuioplkjhgfdsazxcvbnm1234567890";
//字符创长度
$len = strlen($str);
$num_code = 5;
while($num_code-->0){
$rand_index = rand(0, $len-1);
echo $str[$rand_index];
}
?>
<?php
echo "<br>";
$rand_num =rand();
echo $rand_num."<br/>";
$str = md5($rand_num);
echo $str."<br/>";
$num_code=5;
echo substr($str, 0,$num_code);
?>
| 动态随即验证码1
<?php
$rand_num =rand();
$str = md5($rand_num);
$num_code=5;
//截取随机码
$str_code = substr($str, 0,$num_code);
//画布大小
$width = 50;
$height =20;
//创建图片句柄
$image = imagecreate($width, $height);
//背景颜色
$bg_color =imagecolorallocate($image, 255, 255, 255);
//边框色
$border_color =imagecolorallocate($image, 0, 0, 0);
//绘制边框
imagerectangle($image, 0, 0, $width-1, $height-1, $border_color);
//设置随机码颜色
$str_color = imagecolorallocate($image, 255, 0, 0);
//字体颜色
$str_size = 5 ;
//设置字体在画布中的位置
$str_x =2;
$str_y =2;
//将随机码添加到画布上
imagestring($image, $str_size, $str_x, $str_y, $str_code, $str_color);
//显示图片
imagepng($image);
//释放资源
imagedestroy($image);
?>
| 动态随即验证码2
<?php
$rand_num =rand();
$str = md5($rand_num);
$num_code=5;
//截取随机码
$str_code = substr($str, 0,$num_code);
//画布大小
$width = 60;
$height =25;
//创建图片句柄
$image = imagecreate($width, $height);
//背景颜色
$bg_color =imagecolorallocate($image, 255, 255, 255);
//边框色
$border_color =imagecolorallocate($image, 0, 0, 0);
//绘制边框
imagerectangle($image, 0, 0, $width-1, $height-1, $border_color);
for ($i = 0; $i < $num_code; $i++) {
//随机产生验证码颜色
$str_color = imagecolorallocate($image,rand(0,255), rand(0,255),rand(0,255));
//字体颜色
$font_size = 10;
//设置验证码位置
$str_x=floor(($width/$num_code)*$i)+rand(0,5);
$str_y =rand(2, $height-15);
//将随机码添加到画布上,每个随即字符都不同颜色大小
imagechar($image, $font_size, $str_x, $str_y, $str_code[$i], $str_color);
}
header("content-type:image/png");
//显示图片
imagepng($image);
//释放资源
imagedestroy($image);
?>
| 验证码的背景干扰点
<?php
/*
* 绘制干扰点
*/
$width =60;
$height=25;
//图片句柄
$image = imagecreate($width, $height);
//背景颜色
$image_bgcolor = imagecolorallocate($image, 255, 255, 255);
//$边框颜色
$image_bolorcolor = imagecolorallocate($image, 0, 0, 0);
//绘制边框
imagerectangle($image, 0, 0, $width-1, $height-1, $image_bolorcolor);
//设置干扰点个数
$num_distrub_points =200;
for ($i = 0; $i < $num_distrub_points; $i++) {
//随机设置干扰点颜色
$point_color = imagecolorallocate($image,rand(0, 255), rand(0, 255),rand(0, 255));
//干扰点的位置
$point_x = rand(2, $width-2);
$point_y = rand(2, $height-2);
//绘制干扰点
imagesetpixel($image, $point_x, $point_y, $point_color);
}
header("content-type:image/png");
imagepng($image);
imagedestroy($image);
?>
| 一个完整的验证码
<?php
/**
* 创建一个完整版的验证码
*/
function createCheckCode($width=60,$height=25,$num_code=5,$num_distrub_points=200){
//创建画布
$image = imagecreate($width,$height);
//绘制背景颜色
$image_bgcolor = imagecolorallocate($image, 255, 255, 255);
//会孩子边框颜色
$image_border_color = imagecolorallocate($image, 0, 0, 0);
//绘制边框
imagerectangle($image, 0, 0,$width-1, $height-1, $image_border_color);
/*
* 产生随机码
*/
$rand_num = rand();
$str = md5($rand_num);
//将随机码全部换成大写
$str_code = strtoupper(substr($str, 0,$num_code));
/*
* 绘制随机码(添加到画布上)
*/
for ($i = 0; $i < $num_code; $i++) {
//每个随机码的颜色
$str_color = imagecolorallocate($image,rand(0,255), rand(0,255),rand(0,255));
//随机码的字体大小
$font_size =5;
//随机码在画布中出现的位置
$str_x = floor(($width/$num_code)*$i)+rand(0, 5);
$str_y = rand(2, $height-15);
//将随机码添加到画布中
imagechar($image, $font_size, $str_x, $str_y, $str_code[$i], $str_color);
/*
* 绘制干扰点
*/
}
for ($i = 0; $i < $num_distrub_points; $i++) {
//每个干扰点的颜色
$point_color = imagecolorallocate($image,rand(0,100), rand(0,100),rand(0,100));
$point_x = rand(2,$width-2);
$point_y = rand(2,$height-2);
imagesetpixel($image, $point_x, $point_y, $point_color);
}
header("content-type:image/png");
//输出画布图片
imagepng($image);
imagedestroy($image);
}
createCheckCode();
?>
| 日期与时间操作
简单的unix时间戳
<?php
//UNIX时间戳
echo time();
?>
| 获取系统的时间
<?php
header("content-type:text/html;charset=utf-8");
//UNIX时间戳
echo time()."<br/>";
//指定某个时间点的时间戳
echo "指定某个时间点的时间戳:".mktime(0,0,0,12,22,2012)."<br/>";
echo date_default_timezone_get()."<br/>";
$week = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
/**
* 获取当前的时间戳信息
*/
$t = getdate();
echo "系统当前时间为:<p>";
echo $t['year']."年".$t['mon']."月".$t['mday']."日".$t['hours']."时".$t['minutes']."分".$t['second']."秒";
echo "<br/>今天是一年中的第".$t['yday']."天";
?>
| 格式化日期输出
<?php
header("content-type:text/html;charset=utf-8");
$week = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
/**
* 格式化当前的时间戳信息
*/
$format = 'Y年m月d日H点i分s秒';
$time = date($format);
?>
| 计算两个时间之间的间隔
<?php
header("content-type:text/html;charset=utf-8");
//当前时间
$from_timestamp = mktime();
//指定时间点
$to_timestamp = mktime(0,0,0,12,22,2015);
//计算时间戳的差
$interval = $to_timestamp- $from_timestamp;
//将时间戳转换成天数
$interval_day = floor($interval/(3600*24));
echo "时间间隔为:".$interval_day."天";
?>
| 设置cookie
<?php
header("content-type:text/html;charset=utf-8");
$name = 'last_login_time';
//cookie的值
$value = date('Y-m-d H-i-s');
//设置cookie,永久有效
setcookie($name,$value);
$name = 'last_login_time';
if(isset($_COOKIE[$name])){
echo "你上一次访问该网站的时间为:".$_COOKIE[$name]."<br/>";
}else{
echo "欢迎您第一次来到本站";
}
?>
| Session
开启session,获取session的名称以及修改session的名称
<?php
//开启session
session_start();
//返回session的名称
echo session_name()."<br/>";
//修改session的名称
session_name('haha');
//返回修改后的session名称
echo session_name();
?>
| 获取session的保存路径
<?php
header("content-type:text/html;charset=utf-8");
session_start();
echo "原session_id为:".session_id();
//修改session_id
session_id('1234567890');
echo "<hr>修改后的session_id为:".session_id()."<hr>";
echo "session文件的保存路径为:".session_save_path();
?>
| Session的简单使用
<?php
header("content-type:text/html;charset=utf-8");
session_start();
if(isset($_SESSION['name'])){
echo "已经注册了session<hr>";
}else{
echo "还没有注册session<hr>";
$_SESSION['name'] = 'SESSION名称';
}
if(isset($_SESSION['name'])){
echo "已经注册了session<hr>";
echo $_SESSION['name'];
}else{
echo "还没有注册session<hr>";
}
?>
| 数据库部分
Mysqli扩展库使用方法-------------------查询
<?php
header("content-type:text/html;charset=utf-8");
//创建一个与服务器链接的实例
$mysqli = new mysqli('localhost','root','123456','test');
if($mysqli->connect_errno){
die("数据库链接失败".$mysqli->connect_error);
}else{
echo "数据库链接成功";
}
$mysqli->select_db("test");
$sql = "select user,pass from user";
$result = $mysqli->query($sql);
if($result){
while ($row = $result->fetch_assoc()){
echo <<<TR
<tr>
<td>{$row['user']}</td>
<td>{$row['pass']}</td>
</tr>
TR;
}
}else{
echo "该语句执行失败";
}
$mysqli->close();
?>
| AJAX
1. index
<!DOCTYPEhtml>
<html>
<head>
<scripttype="text/javascript"src="ajax.js"></script>
<metacharset="UTF-8">
<title>ajax</title>
</head>
<body>
<formname="myform">
用户:<inputtype="text" name="username"/>
时间:<inputtype="text" name="time"/>
</form>
</body>
</html>
| 2. ajax.js
//获取ajax对象
var xmlHttp = new XMLHttpRequest();
/**
* 1.method http方法,例如:POST、GET、PUT及PROPFIND。大小写不敏感。
* 2.url 请求的URL地址,可以为绝对地址也可以为相对地址。
* 3.async 布尔型,指定此请求是否为异步方式,默认为true。如果为真,当状态改变时会调用onreadystatechange属性指定的回调函数。
* 4.username 如果服务器需要验证,此处指定用户名,如果未指定,当服务器需要验证时,会弹出验证窗口。
* 5.password 验证信息中的密码部分,如果用户名为空,则此值将被忽略。
*/
var method = "post";
var url = "test.php";
var async = true;
xmlHttp.open(method,url, async);
//设置回调函数,接收服务器端的信息以进行处理
xmlHttp.onreadystatechange =getServerInfo;
//xmlHttp.abort();调用次方法后,会放弃与服务器端进行数据通信
//想服务器端发送内容,为什么不起作用呢?
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
alert(xmlHttp.status);
/*****************ajax的回调函数*******************/
function getServerInfo(){
/**
* xmlHttp.readyState是服务器端返回的状态
*/
//alert(xmlHttp.readyState);
/*
if(xmlHttp.readyState==0){
alert("这个是不会弹出的,因为还没有服务器进行连接");
}
if(xmlHttp.readyState==1){
alert("xmlHttp.readyState==1\n该xmlHttp对象已经在服务器端建立了,即open方法已经执行,但是还没有调用send方法");
}
if(xmlHttp.readyState==2){
alert("xmlHttp.readyState==2\nsend方法已调用,但是当前的状态及http头未知");
}
if(xmlHttp.readyState==3){
alert("xmlHttp.readyState==3\n已接收部分数据,因为响应及http头不全,这时通过responseBody和responseText获取部分数据会出现错误");
}
*/
if(xmlHttp.readyState==4){
alert(xmlHttp.status);
//alert("xmlHttp.readyState==4\n数据接收完毕,此时可以通过通过responseBody和responseText获取完整的回应数据");
var headers = xmlHttp.getAllResponseHeaders();
//获取所有响应头信息
//alert(headers);
//获取指定的响应头里面的信息
//获取时间
//alert(xmlHttp.getResponseHeader("Date"));
//获取服务器
//alert(xmlHttp.getResponseHeader("Server"));
//获取服务器脚本版本
//alert(xmlHttp.getResponseHeader("X-Powered-By"));
//获取相应头长度
//alert(xmlHttp.getResponseHeader("Content-Length"));
//获取链接状态
//alert(xmlHttp.getResponseHeader("Connection"));
//获取文档类型
//alert(xmlHttp.getResponseHeader("Content-Type"));
//获取连接持续时间
//alert(xmlHttp.getResponseHeader("Keep-Alive"));
document.myform.time.value = xmlHttp.responseText;
}
}
| 3.test.php
<?php
$format = 'Y-m-d';
$time = date($format);
echo $time;
?>
| 4.总结
所谓的ajax就是多一个XMLHttpRequest对象实例与服务器进行异步通信,取得从服务器端返回的信息给用户,已完成局部刷新数据和从后台取数据的功能.需要的技术,html---js---后台脚本(php/asp/jsp),当进行小型数据传输的时候可以不需要xml文件,当存在大量文本的时候就需要xml文件
|
|
|