完整代码如下。
<?php /**************************************************** * 返回值:失败:false; 成功:路径. * UpLoadFileOne(file元素名, 文件夹, 文件类型, 大小). * 完成一个文件上传功能的函数 * site http://www. ****************************************************/ function UpLoadFileOne($input, $path='upload', $ftype='gif,jpg,png', $fsize=2){ if(strrpos($path, '/') < strlen($path)-1) $path .= '/'; //上传文件夹. $Atype = explode()(',', $ftype); //文件类型. $fsize = $fsize*1048576; //(1024*1024==1048576=1M)限文件大小,按字节. $fileInfo = $_FILES[$input]; //文件信息 $name = $fileInfo['name']; //客户端机器文件的原名称。 $size = $fileInfo['size']; //上传文件的大小,单位为字节。 $type = $fileInfo['type']; //上传文件类型. $tmp_name = $fileInfo['tmp_name']; //文件被上传后在服务端储存的临时文件名。 $error = $fileInfo['error']; //结果错误信息. if($error == 0){ $type = MyFileType($type); //检测上传文件类型 $myfile = CreatMyFile($path); //创建文件夹 if($myfile==false) return false; else $path = $myfile.MakeFname($type); //文件路径.文件名 if(in_array($type,$Atype) && $size<=$fsize && is_uploaded_file($fileInfo['tmp_name'])){ if(@move_uploaded_file($tmp_name, $path)) return str_replace()(array('../','./'), '', $path); else return false; }else return false; }else return false; } /***************************************************** * 完成多个文件上传功能的函数: http://www. * UpLoadFileAll(file元素名,路径,类型,大小) * <input name='pic[]' id='pic' type='file' size='25'> * <input name='pic[]' id='pic' type='file' size='25'> ******************************************************/ function UpLoadFileAll($input='UpPic', $path='upload', $ftype='jpg,gif,png', $fsize=2){ $fileInfo = $_FILES[$input]; //文件信息 if(strrpos($path, '/') < strlen($path)-1) $path .= '/'; //上传文件夹. $myfile = CreatMyFile($path); //创建文件夹 if($myfile==false) return false; $Atype = explode(',', $ftype); //文件类型. $fsize = $fsize*1048576; //(1024*1024==1048576=1M)限文件大小,按字节. $js = "以下文件上传成功:\\n\\n"; if(is_array($fileInfo["error"])){ foreach ($fileInfo["error"] as $key => $error){ if ($error == 0) { $name = $fileInfo["name"][$key]; //客户端机器文件的原名称. $size = $fileInfo["size"][$key]; //上传文件的大小,单位为字节. $type = $fileInfo["type"][$key]; //上传文件类型. $tmp_name = $fileInfo["tmp_name"][$key]; //文件被上传后在服务端储存的临时文件名. $type = MyFileType($type); //检测上传文件类型. $path = $myfile.MakeFname($type); //文件路径包括文件名. if(in_array($type, $Atype) && $size<=$fsize){ if(@move_uploaded_file($tmp_name, $path)){ $array[] = $path; $js .= " ".$name." 上传成功 !\\n"; } } } } } echo "<script>alert('$js');</script>"; return $array; } /***************************************************************************** * 重设图片尺寸大小:ResizeImage(原图片路径,缩略图(最大)宽度,缩略图(最大)高度) * 返回值: * 失败返回: FLASH. * 成功返回:缩略图路径. *****************************************************************************/ function ResizeImage($path, $maxwidth, $maxheight){ $picS = substr($path, -3); $name = substr($path, 0, strrpos($path, '.')).'_S'; switch($picS){ case 'jpg': $im = @imagecreatefromjpeg($path); break; case 'gif': $im = @imagecreatefromgif($path); break; default: $im = @imagecreatefrompng($path); } $width = imagesx($im); $height = imagesy($im); if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){ if($maxwidth && $width > $maxwidth){ $widthratio = $maxwidth/$width; $RESIZEWIDTH=true; }//end if if($maxheight && $height > $maxheight){ $heightratio = $maxheight/$height; $RESIZEHEIGHT=true; }//end if if($RESIZEWIDTH && $RESIZEHEIGHT){ if($widthratio < $heightratio){ $ratio = $widthratio; }else{ $ratio = $heightratio; } }elseif($RESIZEWIDTH){ $ratio = $widthratio; }elseif($RESIZEHEIGHT){ $ratio = $heightratio; }//end if $newwidth = $width * $ratio; $newheight = $height * $ratio; if(function_exists("imagecopyresampled")){ $newim = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); }else{ $newim = imagecreate($newwidth, $newheight); imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); }//end if }else{ $newim = $im; }//end if switch($picS){ case 'jpg': $PicPath = $name.".jpg"; if(imagejpeg($newim, $PicPath)){ imagedestroy($newim); return str_replace(array('../','./'), '', $PicPath); }else{ return false; } break; case 'gif': $PicPath = $name.".gif"; if(imagegif($newim, $PicPath)){ imagedestroy($newim); return str_replace(array('../','./'), '', $PicPath); }else{ return false; } break; default: $PicPath = $name.".png"; if(imagepng($newim, $PicPath)){ imagedestroy($newim); return str_replace(array('../','./'), '', $PicPath); }else{ return false; } }//end switch }//end function /************************** * 文件属性 $type = 文件属性 ***************************/ function MyFileType($type) { $type = strtolower()($type); switch($type) { //OFFICE case 'application/msword' : $type = 'doc'; break; case 'application/vnd.ms-excel': $type = 'xls'; break; case 'application/vnd.ms-powerpoint': $type = 'ppt'; break; //压缩 case 'application/octet-stream': $type = 'rar'; break; //文本 case 'text/plain': $type = 'txt'; break; //图片 case 'image/pjpeg': $type = 'jpg'; break; case 'image/gif': $type = 'gif'; break; case 'image/x-png': $type = 'png'; break; case 'image/bmp': $type = 'bmp'; break; default : $type = 'err'; } return $type; //返回文件类型. } /****************** * 创建文件夹(路径) *******************/ function CreatMyFile($fname=''){ switch($fname){ case '': break; default: if(strrpos($fname, '/') < strlen($fname)-1) $fname .= '/'; } $fname .= date("Y-m"); if(is_dir($fname)) return $fname.'/'; if(mkdir($fname, 0755)==false) return false; //if(chmod($fname, 0777)==false) return false; return $fname.'/'; } /***************************** * 生成文件名 * $fname ==> 文件名称 * $ftype ==> 文件类型 *****************************/ function MakeFname($ftype) { $fname = date("mdHis").'_'.rand(100000, 999999); return $fname.'.'.$ftype; } ?>
php 文件格式 mime类型对照表。
$mime_types = array(
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'pict' => 'image/x-pict',
'pic' => 'image/x-pict',
'pct' => 'image/x-pict',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'psd' => 'image/x-photoshop',
'swf' => 'application/x-shockwave-flash',
'js' => 'application/x-javascript',
'pdf' => 'application/pdf',
'ps' => 'application/postscript',
'eps' => 'application/postscript',
'ai' => 'application/postscript',
'wmf' => 'application/x-msmetafile',
'css' => 'text/css',
'htm' => 'text/html',
'html' => 'text/html',
'txt' => 'text/plain',
'xml' => 'text/xml',
'wml' => 'text/wml',
'wbmp' => 'image/vnd.wap.wbmp',
'mid' => 'audio/midi',
'wav' => 'audio/wav',
'mp3' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'avi' => 'video/x-msvideo',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'lha' => 'application/x-lha',
'lzh' => 'application/x-lha',
'z' => 'application/x-compress',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'bz2' => 'application/bzip2',
'zip' => 'application/zip',
'arj' => 'application/x-arj',
'rar' => 'application/x-rar-compressed',
'hqx' => 'application/mac-binhex40',
'sit' => 'application/x-stuffit',
'bin' => 'application/x-macbinary',
'uu' => 'text/x-uuencode',
'uue' => 'text/x-uuencode',
'latex'=> 'application/x-latex',
'ltx' => 'application/x-latex',
'tcl' => 'application/x-tcl',
'pgp' => 'application/pgp',
'asc' => 'application/pgp',
'exe' => 'application/x-msdownload',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'mdb' => 'application/x-msaccess',
'wri' => 'application/x-mswrite',
);
<?php //获取用户操作系统信息 //by http://www. function osinfo() { $os=""; $Agent = $GLOBALS["HTTP_USER_AGENT"]; if (eregi('win',$Agent) && strpos($Agent, '95')) { $os="Windows 95"; } elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) { $os="Windows ME"; } elseif (eregi('win',$Agent) && ereg('98',$Agent)) { $os="Windows 98"; } elseif (eregi('win',$Agent) && eregi('nt 5\.0',$Agent)) { $os="Windows 2000"; } elseif (eregi('win',$Agent) && eregi('nt',$Agent)) { $os="Windows NT"; } elseif (eregi('win',$Agent) && eregi('nt 5\.1',$Agent)) { $os="Windows XP"; } elseif (eregi('win',$Agent) && ereg('32',$Agent)) { $os="Windows 32"; } elseif (eregi('linux',$Agent)) { $os="Linux"; } elseif (eregi('unix',$Agent)) { $os="Unix"; } elseif (eregi('sun',$Agent) && eregi('os',$Agent)) { $os="SunOS"; } elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) { $os="IBM OS/2"; } elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) { $os="Macintosh"; } elseif (eregi('PowerPC',$Agent)) { $os="PowerPC"; } elseif (eregi('AIX',$Agent)) { $os="AIX"; } elseif (eregi('HPUX',$Agent)) { $os="HPUX"; } elseif (eregi('NetBSD',$Agent)) { $os="NetBSD"; } elseif (eregi('BSD',$Agent)) { $os="BSD"; } elseif (ereg('OSF1',$Agent)) { $os="OSF1"; } elseif (ereg('IRIX',$Agent)) { $os="IRIX"; } elseif (eregi('FreeBSD',$Agent)) { $os="FreeBSD"; } if ($os=='') $os = "Unknown"; return $os; } //调用方法$os=os_infor() ; ?>