PHP应用
PHP文件操作功能函数
```php <?php /* 转换字节大小 */ function transByte( $size ){ $arr = array ( "B" , "KB" , "MB" , "GB" , "TB" , "EB" ); $i =0; while ( $size >=1024){ $size /=1024; $i ++; } return round ( $size ,2). $arr [ $i ]; } /* 检测文件名称是否合法 */ function checkFilename( $filename ){ $pattern = "/[\/,\*,<>,\?\|]/" ; if (preg_match( $pattern , $filename )){ return false; } else { return true; } } /* 创建文件 */ function createFile( $filename ){ if (checkFilename( $filename )){ //检测当前目录下是否存在同名的文件 if (! file_exists ( $filename )){ //通过touch($filename)来创建 if (touch( $filename )){ $mes = "文件创建成功" ; } else { $mes = "文件创建失败" ; } } else { $mes = "文件已经存在,请重新命名后创建" ; } } else { $mes = "非法文件名" ; } return $mes ; } /* 重命名文件 */ function renameFile($oldname, $newname){ //验证文件名的合法性 if (checkFilename($newname)) { //检测当前目录下是否存在同名的文件 $path = dirname($oldname); if (!file_exists($path . "/" . $newname)) { //进行重命名 if (rename($oldname, $path . "/" . $newname)) { $mes = "重命名成功"; } else { $mes = "重命名失败"; } } else { $mes = "存在同名文件,请重新命名"; } } else { return $mes = "非法文件名"; } return $mes; } /* 删除文件 */ function delFile($filename){ if (unlink($filename)) { $mes = "文件删除成功"; } else { $mes = "文件删除失败"; } return $mes; } /* 下载文件操作 */ function downFile($filename){ header("Content-disposition:attachment;filename=" . basename($filename)); header("Content-length:" . filesize($filename)); readfile($filename); } /* 复制文件 */ function copyFile($filename, $dstname){ if (file_exists($dstname)) { if (!file_exists($dstname . "/" . basename($filename))) { if (copy($filename, $dstname . "/" . basename($filename))) { $mes = "文件复制成功"; } else { $mes = "文件复制失败"; } } else { $mes = "存在同名文件"; } } else { $mes = "目标目录不存在"; } return $mes; } /* 剪切文件 */ function cutFile($filename, $dstname){ if (file_exists($dstname)) { if (!file_exists($dstname . "/" . basename($filename))) { if (rename($filename, $dstname . "/" . basename($filename))) { $mes = "文件剪切成功"; } else { $mes = "文件剪切失败"; } } else { $mes = "存在同名文件"; } } else { $mes = "目标目录不存在"; } return $mes; } /* 上传文件 */ function uploadFile($fileInfo, $path, $allowExt = array("gif", "jpeg", "jpg", "png", "txt"), $maxSize = 10485760){ //判断错误号 if ($fileInfo ['error'] == UPLOAD_ERR_OK) { //文件是否是通过HTTP POST方式上传上来的 if (is_uploaded_file($fileInfo ['tmp_name'])) { //上传文件的文件名,只允许上传jpeg|jpg、png、gif、txt的文件 //$allowExt=array("gif","jpeg","jpg","png","txt"); $ext = getExt($fileInfo ['name']); $uniqid = getUniqidName(); $destination = $path . "/" . pathinfo($fileInfo ['name'], PATHINFO_FILENAME) . "_" . $uniqid . "." . $ext; if (in_array($ext, $allowExt)) { if ($fileInfo ['size'] <= $maxSize) { if (move_uploaded_file($fileInfo ['tmp_name'], $destination)) { $mes = "文件上传成功"; } else { $mes = "文件移动失败"; } } else { $mes = "文件过大"; } } else { $mes = "非法文件类型"; } } else { $mes = "文件不是通过HTTP POST方式上传上来的"; } } else { switch ($fileInfo ['error']) { case 1: $mes = "超过了配置文件的大小"; break; case 2: $mes = "超过了表单允许接收数据的大小"; break; case 3: $mes = "文件部分被上传"; break; case 4: $mes = "没有文件被上传"; break; } } return $mes; } /** * 写文件函数 * * @param string $filename 文件名 * @param string $text 要写入的文本字符串 * @param string $openmod 文本写入模式('w':覆盖重写,'a':文本追加) * @return boolean */ function write_file($filename, $text, $openmod = 'w') { if (@$fp = fopen($filename, $openmod)) { flock($fp, 2); fwrite($fp, $text); fclose($fp); return true; } else { return false; } } ?> ```
顶部
收展
底部
[TOC]
目录
PHP中常用的header头部定义
PHP压缩包下载
PHP文件下载
PHP常用加密函数总结
URL请求参数加解密
PHP文件操作功能函数
PHP判断远程文件是否存在
PHP生成GUID的函数
PHP通用请求函数CURL封装
PHP获取访问者IP地址
PHP图片操作
PHP实现发红包程序
PHP各种验证代码
PHP防XSS 防SQL注入的代码
PHP遍历目录下的全部文件
PHP获取中文字符拼音首字母
PHP判断输入数据是否合法常用的类
相关推荐
PHP底层
PHP面向对象
PHP工具类
PHP编程经验
PHP框架