有用的函数-文件处理及其它
来源:互联网 发布:js 深度克隆 编辑:程序博客网 时间:2024/05/18 03:13
有用的函数-文件处理及其它
1.文件尺寸大小转换复制代码2.按比例改变图片大小复制代码3.创建多级目录复制代码4.判断是绝对路径还是相对路径复制代码5.得到文件类型复制代码6.遍历文件夹中文件复制代码7.判断爬虫函数复制代码8.判断远程文件是否存在复制代码9.多重判断文件函数删除复制代码
- /**
- 转换附件大小单位
- @param string $filesize 文件大小 kb
- */
- function changeFileSize($filesize)
- {
- if($filesize >= 1073741824)
- {
- $filesize = round($filesize / 1073741824 ,2) . ' Gb';
- } elseif($filesize >= 1048576)
- {
- $filesize = round($filesize / 1048576 ,2) . ' Mb';
- } elseif($filesize >= 1024)
- {
- $filesize = round($filesize / 1024, 2) . ' Kb';
- } else
- {
- $filesize = $filesize . ' Bytes';
- }
- return $filesize;
- }
- /**
- 按照比例改变图片大小(非生成缩略图)
- @param string $img 图片路径
- @param int $max_w 最大缩放宽
- @param int $max_h 最大缩放高
- */
- function chImageSize ($img,$max_w,$max_h)
- {
- $size = @getimagesize($img);
- $w = $size[0];
- $h = $size[1];
- //计算缩放比例
- @$w_ratio = $max_w / $w;
- @$h_ratio = $max_h / $h;
- //决定处理后的图片宽和高
- if( ($w <= $max_w) && ($h <= $max_h) )
- {
- $tn['w'] = $w;
- $tn['h'] = $h;
- }
- else if(($w_ratio * $h) < $max_h)
- {
- $tn['h'] = ceil($w_ratio * $h);
- $tn['w'] = $max_w;
- }
- else
- {
- $tn['w'] = ceil($h_ratio * $w);
- $tn['h'] = $max_h;
- }
- $tn['rc_w'] = $w;
- $tn['rc_h'] = $h;
- return $tn ;
- }
- function mk_dir( $target ) {
- $target = str_replace( '//', '/', $target );
- if ( file_exists( $target ) )
- return @is_dir( $target );
- if ( @mkdir( $target ) ) {
- $stat = @stat( dirname( $target ) );
- $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
- @chmod( $target, $dir_perms );
- return true;
- } elseif ( is_dir( dirname( $target ) ) ) {
- return false;
- }
- // If the above failed, attempt to create the parent node, then try again.
- if ( ( $target != '/' ) && ( mk_dir( dirname( $target ) ) ) )
- return mk_dir( $target );
- return false;
- }
- function path_is_absolute( $path ) {
- // this is definitive if true but fails if $path does not exist or contains a symbolic link
- if ( realpath($path) == $path )
- return true;
- if ( strlen($path) == 0 || $path{0} == '.' )
- return false;
- // windows allows absolute paths like this
- if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
- return true;
- // a path starting with / or \ is absolute; anything else is relative
- return (bool) preg_match('#^[/\\\\]#', $path);
- }
- function get_filetype($filename) {
- // Accepted MIME types are set here as PCRE unless provided.
- $mimes = array(
- 'jpg|jpeg|jpe' => 'image/jpeg',
- 'gif' => 'image/gif',
- 'png' => 'image/png',
- 'bmp' => 'image/bmp',
- 'tif|tiff' => 'image/tiff',
- 'ico' => 'image/x-icon',
- 'asf|asx|wax|wmv|wmx' => 'video/asf',
- 'avi' => 'video/avi',
- 'divx' => 'video/divx',
- 'mov|qt' => 'video/quicktime',
- 'mpeg|mpg|mpe|mp4' => 'video/mpeg',
- 'txt|c|cc|h' => 'text/plain',
- 'rtx' => 'text/richtext',
- 'css' => 'text/css',
- 'htm|html' => 'text/html',
- 'mp3|m4a' => 'audio/mpeg',
- 'ra|ram' => 'audio/x-realaudio',
- 'wav' => 'audio/wav',
- 'ogg' => 'audio/ogg',
- 'mid|midi' => 'audio/midi',
- 'wma' => 'audio/wma',
- 'rtf' => 'application/rtf',
- 'js' => 'application/javascript',
- 'pdf' => 'application/pdf',
- 'doc|docx' => 'application/msword',
- 'pot|pps|ppt|pptx' => 'application/vnd.ms-powerpoint',
- 'wri' => 'application/vnd.ms-write',
- 'xla|xls|xlsx|xlt|xlw' => 'application/vnd.ms-excel',
- 'mdb' => 'application/vnd.ms-access',
- 'mpp' => 'application/vnd.ms-project',
- 'swf' => 'application/x-shockwave-flash',
- 'class' => 'application/java',
- 'tar' => 'application/x-tar',
- 'zip' => 'application/zip',
- 'gz|gzip' => 'application/x-gzip',
- 'exe' => 'application/x-msdownload',
- // openoffice formats
- 'odt' => 'application/vnd.oasis.opendocument.text',
- 'odp' => 'application/vnd.oasis.opendocument.presentation',
- 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
- 'odg' => 'application/vnd.oasis.opendocument.graphics',
- 'odc' => 'application/vnd.oasis.opendocument.chart',
- 'odb' => 'application/vnd.oasis.opendocument.database',
- 'odf' => 'application/vnd.oasis.opendocument.formula',
- );
- $type = false;
- $ext = false;
- foreach ( $mimes as $ext_preg => $mime_match ) {
- $ext_preg = '!\.(' . $ext_preg . ')$!i';
- if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
- $type = $mime_match;
- $ext = $ext_matches[1];
- break;
- }
- }
- return compact( 'ext', 'type' );
- }
- function list_files( $folder = '', $levels = 100 ) {
- if( empty($folder) )
- return false;
- if( ! $levels )
- return false;
- $files = array();
- if ( $dir = @opendir( $folder ) ) {
- while (($file = readdir( $dir ) ) !== false ) {
- if ( in_array($file, array('.', '..') ) )
- continue;
- if ( is_dir( $folder . '/' . $file ) ) {
- $files2 = list_files( $folder . '/' . $file, $levels - 1);
- if( $files2 )
- $files = array_merge($files, $files2 );
- else
- $files[] = $folder . '/' . $file . '/';
- } else {
- $files[] = $folder . '/' . $file;
- }
- }
- }
- @closedir( $dir );
- return $files;
- }
- function isCrawler() {
- if(ini_get('browscap')) {
- $browser= get_browser(NULL, true);
- if($browser['crawler']) {
- return true;
- }
- } else if (isset($_SERVER['HTTP_USER_AGENT'])){
- $agent= $_SERVER['HTTP_USER_AGENT'];
- $crawlers= array(
- "/Googlebot/",
- "/Yahoo! Slurp;/",
- "/msnbot/",
- "/Mediapartners-Google/",
- "/Scooter/",
- "/Yahoo-MMCrawler/",
- "/FAST-WebCrawler/",
- "/Yahoo-MMCrawler/",
- "/Yahoo! Slurp/",
- "/FAST-WebCrawler/",
- "/FAST Enterprise Crawler/",
- "/grub-client-/",
- "/MSIECrawler/",
- "/NPBot/",
- "/NameProtect/i",
- "/ZyBorg/i",
- "/worio bot heritrix/i",
- "/Ask Jeeves/",
- "/libwww-perl/i",
- "/Gigabot/i",
- "/bot@bot.bot/i",
- "/SeznamBot/i",
- );
- foreach($crawlers as $c) {
- if(preg_match($c, $agent)) {
- return true;
- }
- }
- }
- return false;
- }
- php
- /*
- 函数:remote_file_exists
- 功能:判断远程文件是否存在
- 参数: $url_file -远程文件URL
- 返回:存在返回true,不存在或者其他原因返回false
- */
- function remote_file_exists($url_file){
- //检测输入
- $url_file = trim($url_file);
- if (empty($url_file)) { return false; }
- $url_arr = parse_url($url_file);
- if (!is_array($url_arr) || empty($url_arr)){return false; }
- //获取请求数据
- $host = $url_arr['host'];
- $path = $url_arr['path'] ."?".$url_arr['query'];
- $port = isset($url_arr['port']) ?$url_arr['port'] : "80";
- //连接服务器
- $fp = fsockopen($host, $port, $err_no, $err_str,30);
- if (!$fp){ return false; }
- //构造请求协议
- $request_str = "GET ".$path."HTTP/1.1\r\n";
- $request_str .= "Host:".$host."\r\n";
- $request_str .= "Connection:Close\r\n\r\n";
- //发送请求
- fwrite($fp,$request_str);
- $first_header = fgets($fp, 1024);
- fclose($fp);
- //判断文件是否存在
- if (trim($first_header) == ""){ return false;}
- if (!preg_match("/200/", $first_header)){
- return false;
- }
- return true;
- }
- function delete_file($file)
- {
- if (file_exists($file))
- {
- $delete = chmod ($file, 0777);
- $delete = unlink($file);
- if(file_exists($file))
- {
- $filesys = eregi_replace("/","\\",$file);
- $delete = system("del $filesys");
- clearstatcache();
- if(file_exists($file))
- {
- $delete = chmod ($file, 0777);
- $delete = unlink($file);
- $delete = system("del $filesys");
- }
- }
- clearstatcache();
- if(file_exists($file))
- {
- return 'Delete Faile : <font color=\'#ff0000\'>'.$file.'</font><br>';
- }
- else
- {
- return 'Delete Successs : <font color=\'#6699cc\'>'.$file.'</font><br>';
- }
- }
- else
- {
- return 'Delete Successs : <font color=\'#6699cc\'>'.$file.'</font><br>';
- }
- }
1.删除目录(含文件)复制代码2.复制目录复制代码
- function removeDir($dirName)
- {
- $result = false;
- if(! is_dir($dirName))
- {
- trigger_error('Invalid Parameter', E_USER_ERROR);
- }
- $handle = opendir($dirName);
- while(($file = readdir($handle)) !== false)
- {
- if($file != '.' && $file != '..')
- {
- $dir = $dirName . DIRECTORY_SEPARATOR . $file;
- is_dir($dir) ? removeDir($dir) : unlink($dir);
- }
- }
- closedir($handle);
- $result = rmdir($dirName) ? true : false;
- return $result;
- }
- function copyDir($source, $destination)
- {
- $result = true;
- if(! is_dir($source))
- {
- trigger_error('Invalid Parameter', E_USER_ERROR);
- }
- if(! is_dir($destination))
- {
- if(! mkdir($destination, 0700))
- {
- trigger_error('Invalid Parameter', E_USER_ERROR);
- }
- }
- $handle = opendir($source);
- while(($file = readdir($handle)) !== false)
- {
- if($file != '.' && $file != '..')
- {
- $src = $source . DIRECTORY_SEPARATOR . $file;
- $dtn = $destination . DIRECTORY_SEPARATOR . $file;
- if(is_dir($src))
- {
- copyDir($src, $dtn);
- }
- else
- {
- if(! copy($src, $dtn))
- {
- $result = false;
- break;
- }
- }
- }
- }
- closedir($handle);
- return $result;
- }
3.图像文件判别函数复制代码
- function imageType($file){
-
- $fp = fopen($file, "rb");
- $data = fread($fp,20);
- fclose($fp);
-
- $sign = substr($data,0,2);
- switch ($sign) {
-
- case "\x42\x4d":
- return "bmp";
- break;
-
- case "\x47\x49":
- if(substr($data,0,3) == "\x47\x49\x46")
- return "gif";
- break;
- case "\x89\x50":
- if(substr($data,0,8) == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
- return "png";
- break;
- case "\xFF\xD8":
- if(substr($data,0,4) == "\xFF\xD8\xFF\xE0" && substr($data,6,5) == "\x4A\x46\x49\x46\x00")
- return "jpg";
- break;
-
- }
- return 'unknown';
- }
- 有用的函数-文件处理及其它
- 有用的函数-字符串处理
- 有用的函数-xml处理
- 有用的函数-日期/日历处理函数
- 几个非常有用的字符串处理函数!
- 6. linux文件处理的有用工具
- Bash 内置函数及其它
- Python类,实例及其它对象的内建函数
- 查找今天改过的文件及其它 (linux find)
- 很有用的文件操作php函数
- pthread_join函数及其它线程终止函数
- mod_timer函数及其它定时器函数
- android中的MotionEvent 及其它事件处理
- android中的MotionEvent 及其它事件处理
- android中的MotionEvent 及其它事件处理
- android中的MotionEvent 及其它事件处理
- android中的MotionEvent 及其它事件处理
- android中的MotionEvent 及其它事件处理
- POJ1177 HDU1828 Picture,线段树求矩形并周长
- 有用的函数-字符串处理
- 有用的函数-系统采集
- 有用的函数-日期/日历处理函数
- 如何利用互联网工具深入调研你仰慕已久的网站
- 有用的函数-文件处理及其它
- JavaEE应用程序性能调优案例分析
- 模拟javascript函数:
- 有用的函数-xml处理
- 模板路径常量传址到js文件中的小技巧
- 2012_01_25
- POJ 1118,2606,2780,3512
- Basic Management Theory
- 使用ob_gzhandler函数有3种方法让它对php进行压缩