文件上传

1
2
3
4
5
6
7
8
9
$name = "picture"//文件名
$f_type = ".png"//文件类型
$tmp = $_FILES['file']['tmp_name'];
$filepath = 'file/document/';//上传的路径
if(move_uploaded_file($tmp,$filepath.$name.$f_type)){
echo "上传成功";
}else{
echo "上传失败";
}

统计目录下文件数

1
2
3
4
5
6
7
$folderPath = "file/document/";
$countFile = 0;
$totalFiles = glob($folderPath . "*");
if ($totalFiles){
$countFile = count($totalFiles);
}
echo "该序列已有文件:".$countFile."个。";

创建文件夹

1
2
3
4
5
6
7
$dir = iconv("UTF-8", "GBK", "file/document/");//文件夹路径
if (!file_exists($dir)){
mkdir($dir,0777,true);
echo "创建成功,请记好您新建的序列哦~[".$news."]";
} else {
echo '['.$news."]已存在,您换个试试吧。";
}

将文件|文件夹添加到ZIP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
function z_addDir2Zip($dir, $zip){
$handler = opendir($dir); //打开当前文件夹由$dir指定
while(( $filename = readdir($handler) ) !== false ){
if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..',不要对他们进行操作
if(is_dir( $dir . '/' . $filename )){// 如果读取的某个对象是文件夹,则递归
z_addDir2Zip( $dir . "/" . $filename, $zip);
}else{ //将文件加入zip对象
echo "向ZIP中添加 [".$filename." ]成功!";
$zip->addFile($dir."/".$filename,iconv('utf-8', 'gbk//ignore', $filename));
//iconv('utf-8', 'gbk//ignore', $filename)对中文文件名转码保存到zip中
}
}
}
@closedir($dir);
}

$file_path = "file/document/".$class_student."/".$down;
$zip_path = 'file/zip/'.$class_student.'-'.$down.'.zip';
$zip = new ZipArchive();
if($zip->open($zip_path, ZipArchive::OVERWRITE)=== TRUE){
z_addDir2Zip( $file_path, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
echo"
<script>
setTimeout(function(){window.location.href='file/zip/$class_student-$down.zip';},200);
</script>";
}else{ echo "新建zip出错";}
?>

其他的addFile用法

1
2
3
4
5
6
7
8
9
10
11
12
// 将指定文件添加到zip中
$zip->addFile('test.txt');
// test.txt文件添加到zip并将其重命名为newfile.txt
$zip->addFile('test.txt', 'newfile.txt');
// 将test.txt文件添加到zip文件中的test文件夹内
$zip->addFile('test.txt', 'test/newfile.txt');
//将一个空的目录添加到zip中
$zip->addEmptyDir ('test');
// 将有指定内容的new.txt文件添加到zip文件中
$zip->addFromString('new.txt', '要添加到new.txt文件中的文本');
// 将有指定内容的new.txt添加到zip文件中的test文件夹
$zip->addFromString('test/new.txt', '要添加到new.txt文件中的文本');

文件名中文乱码问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
如果文件名包含汉字,ZIPARCHIVE::addFile() 会导致无法将文件压缩到压缩包中,或者压缩以后文件名乱码。 
可以使用ZipArchive::addFromString()来实现。
注意:如果操作系统是Windows,文件系统编码是gbk.
如果php文件的文件编码是utf-8,需要相应转码。
ZipArchive::addFile() fails if the filename contains Chinese characters.
ZipArchive::addFromString() should be used instead.

<?php
$z = new ZipArchive;
$file = '中文.txt';
if($z->open(ZIPARCHIVE::CREATE)===true){
$zip->addFile($dir."/".$filename,iconv('utf-8', 'gbk//ignore', $filename));
//iconv('utf-8', 'gbk//ignore', $filename)对中文文件名转码保存到zip中
//这里的文件名转码一定要到addfromstring方法中在使用
//$z->addFromString($file, file_get_contents($file));
//for windows
//$z->addFromString($file, file_get_contents(iconv('utf-8', 'gbk//ignore', $file)));
}
// ...
?>

Session用户验证

login.php

1
2
3
4
session_start();//启用session
setcookie("usname",$usname,time()+3600);//创建cookie并给他输入值
setcookie("passwd",$passwd,time()+3600);
header("location:Admin_Management.php"."?usname=$usname");

admin.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
session_start();//启用session
header("Content-type:text/html;charset=utf-8");
date_default_timezone_set('PRC'); //调整时区
$usname = $_GET["usname"];//接收数据
//判断是否为管理员身份
if ($usname=="da1sy") {
echo "<script>alert('登陆成功!')</script>";
//判断是否接收到了数据,有,则以SESSION方式登录
}else{
echo "<script>alert('未登录!')</script>";
header("location:login.html");
}
?>

图片添加文字水印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/*打开图片*/
//1.配置图片路径 $src = "1.jpg";
$src="aaa.png";
// $src =$img_path;
//2.获取图片信息
$info = getimagesize($src);
//3.通过编号获取图像类型
$type = image_type_to_extension($info[2], false);
//4.在内存中创建和图像类型一样的图像
$fun = "imagecreatefrom" . $type;
//5.图片复制到内存
$image = $fun($src);
$top= $info[0]/2-($info[0]/4);
$left= $info[1]/2;
/*操作图片*/
//1.设置字体的路径
$font = "D:\phpstudy_pro\WWW\jianti.ttf";
//2.填写水印内容
$content = "水印内容刘浩鹏";
//3.设置字体颜色和透明度
$color = imagecolorallocatealpha($image,255,48,48, 0);
//4.写入文字
// 画布资源 字体大小 旋转角度 x轴 y轴 字体颜色 字体文件 需要渲染的字符串
imagettftext($image, 90, 40, $left,$top, $color, $font, $content);
imagettftext($image, 90, 40, $left,$top+250, $color, $font, $content);
/*输出图片*/
//浏览器输出
//header("Content-type:".$info['mime']);
$fun = "image" . $type;
//$fun($image);
//在浏览器中输出图片
$imgPathName = "aaa.png";
//添加水印之后的图片
//图片路径名称
$fun($image, $imgPathName); //保存图片
/*销毁图片*/
imagedestroy($image);
?>

七牛云OSS-API

  • 下载SDK,放到项目文件夹中

    sudo wget https://github.com/qiniu/php-sdk/archive/v7.2.6.tar.gz

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once '../../../api/qiniu_oss/sdk/autoload.php';
// 引入七牛鉴权类
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

/**
* 七牛云图片上传基础类库
* Class Upload
* @package app\common\lib
*/
class Upload
{
/**
* 七牛云图片上传操作
* @return bool|string
*/
public static function imageQiNiu(){
// 要上传文件的临时文件
$file = $_FILES['file']['tmp_name'];
if (empty($file)){
$message = "您提交的图片数据不合法";
return false;
}else{
//拿到上传文件的格式
$pathinfo = pathinfo($_FILES['file']['name']);
//获取图片后缀名
$ext = $pathinfo['extension'];
$config = config('qiniu');
//构建一个鉴权对象
$auth = new Auth($config['ak'],$config['sk']);
//生成上传的token
$token = $auth->uploadToken($config['bucket']);

//上传到七牛云后 保存的文件名
$saveFileName = date("YmdHis").substr(md5($file),0,6).rand(0000,9999).".".$ext;
//初始化UploadManager类
$uploadMgr = new UploadManager();
$opRes = $uploadMgr->putFile($token,$saveFileName,$file);
if ($opRes[1] != null){
return false;
}else{
return $saveFileName;
}
}
}

/**
* 七牛云图片删除操作
* @param $delFileName 七牛云图片名称,例:2020041208595410935c8386.jpg
* @return bool
*/
public static function delImgQiNiu($delFileName)
{
// 判断是否是图片
$isImage = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $delFileName);
if(!$isImage){
return false;
}else{
//七牛云账号配置信息
$conf = config('qiniu');
// 构建鉴权对象
$auth = new Auth($conf['ak'],$conf['sk']);
// 配置
$config = new Config();
// 管理资源
$bucketManager = new BucketManager($auth, $config);

// 删除文件操作
$opRes = $bucketManager->delete($conf['bucket'], $delFileName);
if (is_null($opRes)) {
// 删除操作成功
return true;
}else{
return false;
}
}
}
}

阿里云OSS-API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
$accessKeyId = 'accessKeyId';
$accessKeySecret = 'accessKeySecret';
$endpoint = '节点';
$bucket = '空间名';
$domain="访问域名";
$object = $new_file_name;
// <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt
$filePath = $tmp_name;
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$opRes = $ossClient->uploadFile($bucket, $object, $filePath);
if ($opRes[1] != null){
alert("上传失败");
}else{
header('Content-type: text/html; charset=UTF-8');
$json = new Services_JSON();
echo $json->encode(array('error' => 0, 'url' => $domain.$object));
exit;
}