摘要:将所有老师讲的封装的方法都写了一遍,包括上传和下载封装的方法<?php header('content-type:text/html;charset=utf-8'); date_default_timezone_set('Asia/shanghai'); /** * 文件创建操作 * @param $f
将所有老师讲的封装的方法都写了一遍,包括上传和下载封装的方法
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('Asia/shanghai');
/**
* 文件创建操作
* @param $fileName //需要创建的文件名
* @return string //提示信息
*/
function create_file($fileName)
{
if (file_exists($fileName)) {
return '文件已经存在';
} elseif (file_exists(dirname($fileName))) {
touch($fileName);
return '文件创建成功';
} elseif (!file_exists(dirname($fileName))) {
mkdir(dirname($fileName), 0777, true);
touch($fileName);
return '文件创建成功';
} else {
return '文件创建失败';
}
}
//echo create_file('text5.txt');
//echo create_file('pp/text5.txt');
/**
* 文件删除操作
* @param $fileName //需要删除的文件名
* @return string //提示信息
*/
function del_file($fileName)
{
if (!file_exists($fileName) && !is_writeable($fileName)) {
return '文件无法删除';
} elseif (unlink($fileName)) {
return '文件删除成功';
} else {
return '文件删除失败';
}
}
//echo del_file('text5.txt');
//echo del_file('pp/text5.txt');
/**
* 文件复制操作
* @param $fileName //需要复制的文件名
* @param $dest //复制到的目标目录文件夹
* @return string //提示信息
*/
function copy_file($fileName,$dest)
{
if (!file_exists($fileName) && is_writeable($fileName)) {
return '文件无法复制';
} elseif (file_exists($dest)) {
$destName = $dest .'/' .basename($fileName);
if (file_exists($destName)) {
return '文件已经存在';
} else {
copy($fileName,$destName);
return '文件复制成功';
}
} elseif (!file_exists($dest)) {
mkdir($dest,0777,true);
$destName = $dest .'/' .basename($fileName);
if (file_exists($destName)) {
return '文件已经存在';
} else {
copy($fileName,$destName);
return '文件复制成功';
}
} else {
return '文件复制失败';
}
}
//echo copy_file('text3.txt','pp2');
/**
* 文件重命名操作
* @param $oldName //需要重命名的文件名
* @param $newName //新文件名
* @return string //提示信息
*/
function rename_file($oldName,$newName)
{
$path = dirname($oldName);
$destName = $path .'/' .$newName;
if (!file_exists($oldName) && !is_writeable($oldName)) {
return '文件无法重命名';
} elseif (file_exists($destName)) {
return '文件名已经存在';
} elseif (!file_exists($destName)) {
rename($oldName,$newName);
return '文件重命名成功';
} else {
return '文件重命名失败';
}
}
//echo rename_file('text1.txt','text5.txt');
/**
* 文件剪切操作
* @param $fileName //要剪切的文件
* @param $dest //目标文件目录
* @return string //提示信息
*/
function cut_file($fileName,$dest)
{
$destName = $dest .'/' .basename($fileName);
if (!is_file($fileName)) {
return '文件无法剪切';
} elseif (!is_dir($dest)) {
mkdir($dest,0777,true);
if (is_file($destName)) {
return '文件已经存在';
} else {
rename($fileName,$destName);
return '文件剪切成功';
}
} elseif (is_dir($dest)) {
if (is_file($destName)) {
return '文件已经存在';
} else {
rename($fileName,$destName);
return '文件剪切成功';
}
} else {
return '文件剪切失败';
}
}
//echo cut_file('text3.txt','pp3');
/**
* 文件信息查询操作
* @param $fileName //需要查询的文件名
* @return array|string //文件信息
*/
function file_get_info($fileName)
{
if (!is_file($fileName) && is_writeable($fileName)){
return '文件无法查询信息';
} else {
return [
'type' => filetype($fileName),
'ctime' => date('Y-m-d H:i:s',filectime($fileName)),
'mtime' => date('Y-m-d h:i:s',filemtime($fileName)),
'atime' => date('Y-m-d H:i:s',fileatime($fileName)),
'size' => trans_byte(filesize($fileName))
];
}
}
//var_dump(file_get_info('text3.txt'));
/**
* 字节转换操作
* @param $byte //字节大小
* @param int $precision //保留小数位数
* @return string
*/
function trans_byte($byte,$precision = 2)
{
$KB = 1024;
$MB = 1024 * $KB;
$GB = 1024 * $MB;
$TB = 1024 * $GB;
if ($byte < $KB) {
return $byte .'B';
} elseif ($byte < $MB) {
return round($byte / $KB, $precision) . 'KB';
} elseif ($byte < $GB) {
return round($byte / $MB, $precision) . 'MB';
} elseif ($byte < $TB) {
return round($byte / $GB, $precision) . 'GB';
} else {
return round($byte / $TB, $precision) . 'TB';
}
}
/**
* 文件内容读取操作
* @param $fileName //需要读取的文件名
* @return bool|false|string //文件内容或者提示信息
*/
function read_file($fileName)
{
if (is_file($fileName) && is_writeable($fileName)) {
return file_get_contents($fileName);
} else {
return '文件无法读取';
}
}
//echo read_file('text3.txt');
/**
* 文件内容读取操作(数组返回)
* @param $fileName //需要读取的文件名
* @param bool $skip_empty_lines //是否有空行
* @return array|bool|string //文件内容或者提示信息
*/
function read_file_array($fileName,$skip_empty_lines = false)
{
if (is_file($fileName) && is_writeable($fileName)) {
if ($skip_empty_lines = true) {
return file($fileName,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
} else {
return file($fileName);
}
} else {
return '文件无法读取';
}
}
//var_dump(read_file_array('text3.txt'));
/**
* 文件写入操作
* @param $fileName //写入的目标文件
* @param $data //写入的数据
* @param bool $clear //是否清空目标文件
* @return string //提示信息
*/
function write_file($fileName,$data,$clear = false)
{
$dirname = dirname($fileName);
if (!is_dir($dirname)) {
mkdir($dirname,0777,true);
}
if (is_array($data) || is_object($data)) {
$data = serialize($data);
}
if ($clear == false) {
if (is_file($fileName) && is_readable($fileName)) {
if (filesize($fileName) > 0) {
$srcData = file_get_contents($fileName);
echo $srcData;
$data = $srcData .$data;
}
}
}
if (file_put_contents($fileName,$data)) {
return '文件写入成功';
}
return '文件写入失败';
}
$data = [
'name' => '灭绝',
'age' => 18
];
//var_dump(write_file('text3.txt',$data));
/**
* 文件下载操作
* @param $fileName //需要下载的文件名
*/
function down_file($fileName)
{
header('Accept-Length:' .filesize($fileName));
header('Content-Disposition:attachment;filename=' .basename($fileName));
readfile($fileName);
}
//down_file('text3.txt');
/**
* 单文件上传操作
* @param $fileInfo //上传的文件信息
* @param string $uploadPath //上传的指定目录
* @param array $allowExt //上传的文件类型
* @param int $maxSize //上传文件最大值
* @return string //提示信息
*/
function upload_file($fileInfo,$uploadPath = './upload',$allowExt = ['png', 'jpg', 'jpeg', 'gif', 'txt', 'html'],$maxSize = 1000000)
{
if ($fileInfo['error'] === 0) {
$ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
if (!in_array($ext,$allowExt)) {
return '非法文件类型';
}
if ($fileInfo['size'] > $maxSize) {
return '超出文件上传最大值!';
}
if (!is_uploaded_file($fileInfo['tmp_name'])) {
return '非法上传';
}
if (!is_dir($uploadPath)) {
mkdir($uploadPath,0777,true);
}
$fileName = md5(uniqid(microtime(true),true)) .'.' .$ext;
$dest = $uploadPath .'/' .$fileName ;
if (!move_uploaded_file($fileInfo['tmp_name'],$dest)) {
return '文件上传失败';
}
return '文件上传成功';
} else {
switch ($fileInfo['error']) {
case 1:
$res = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!';
break;
case 2:
$res = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!';
break;
case 3:
$res = '文件只有部分被上传!';
break;
case 4:
$res = '没有文件被上传!';
break;
case 6:
$res = '找不到临时文件夹';
break;
case 7:
$res = '文件写入失败';
break;
}
return $res;
}
}
批改老师:查无此人批改时间:2019-07-27 11:19:58
老师总结:完成的不错。文件上传,后缀名要判断,文件大小要判断。以免别人上传故意上传垃圾文件。继续加油。