westdc-zf1/application/models/files.php

337 lines
9.0 KiB
PHP

<?php
class files{
/**
* upload
*
* 文件上传
*
* @param String $path e.g. could be the relative path of web htdocs directory
* @param Array $files e.g. $_FILES['Filedata']
* @param String $dir e.g. $_POST['dir']
*
* return Array $msg e.g. if($msg['error'])
*/
static function upload($path,$files,$dir)
{
$msg=array();
$general = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','gif', 'jpg', 'jpeg', 'png', 'bmp','pdf','odt');
//文件保存目录路径
$save_path = $path;
//文件保存目录URL
$save_url = $path;
//定义允许上传的文件扩展名
$ext_arr = array(
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
'flash' => array('swf', 'flv'),
'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
'file' => array('pdf','doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
'reviewatt'=>$general,
'md'=>$general,
'literature'=>$general,
'document'=>$general,
'heihe'=>$general,
'reference' => $general
);
//最大文件大小
$max_size = 10485760;
//$save_path = realpath($save_path). '/';
//有上传文件时
if (empty($files) === false) {
//原文件名
$file_name = $files['name'];
//服务器上临时文件名
$tmp_name = $files['tmp_name'];
//文件大小
$file_size = $files['size'];
//目录名
$dir_name = empty($dir) ? 'image' : trim($dir);
//检查文件名
if (!$file_name) {
$msg['error'] = "请选择文件。";
}
//检查目录
else if (@is_dir($save_path) === false) {
$msg['error'] = "上传目录不存在。请联系管理员";
}
//检查目录写权限
else if (@is_writable($save_path) === false) {
$msg['error'] = "上传目录没有写权限。请联系管理员";
}
//检查是否已上传
else if (@is_uploaded_file($tmp_name) === false) {
$msg['error'] = "临时文件可能不是上传文件。或者文件类型不在允许的范围内,请重新上传";
}
//检查文件大小
else if ($file_size > $max_size) {
$msg['error'] = "上传文件大小超过限制。";
}
//检查目录名
else if (empty($ext_arr[$dir_name])) {
$msg['error'] = "目录名不正确。";
}
else
{
//获得文件扩展名
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//检查扩展名
if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
$msg['error'] = "上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。";
}
else
{
//创建文件夹
$dbsave = ""; //数据库中存放的路径
if ($dir_name !== '') {
$save_path .= $dir_name . "/";
$save_url .= $dir_name . "/";
$dbsave = $dir_name.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}
}
$y = date("Y");
$m = date("m");
$d = date("d");
$save_path .= $y . "/";
$save_url .= $y . "/";
$dbsave .= $y.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}
$save_path .= $m . "/";
$save_url .= $m . "/";
$dbsave .= $m.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}
$save_path .= $d . "/";
$save_url .= $d . "/";
$dbsave .= $d.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}
//新文件名
$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
//移动文件
$file_path = $save_path . $new_file_name;
if (move_uploaded_file($tmp_name, $file_path) === false) {
$msg['error'] = "上传文件失败。";
}
//数据库最终存储的文件
$dbsave .= $new_file_name;
@chmod($file_path, 0644);
$file_url = $save_url . $new_file_name;
$msg['file_url'] = $file_url;
$msg['file_size'] = $file_size;
$msg['db_path'] = $dbsave;
$msg['realname'] = $file_name;
$msg['file_type'] = $dir_name;
}//检查扩展名
}//目录正确性
return $msg;
}
}//文件上传
/**
* getOne()
*
* 从数据库获取单个文件的信息
*
* @param int id
*
* return array
*/
static function getOne($db,$id){
//下载单个附件
$sql = $db->quoteInto("select a.* from attachments a where a.id=? order by a.ts_created desc",$id);
$att = $db->fetchRow($sql);
if(empty($att['id']))
{ $att['error']=='文件不存在';}
return $att;
}
/**
* getImageType()
*
* 判断文件是否为图片,如果是图片,返回图片类型、创建函数、扩展名、质量
*
* return array
*/
static function getImageType($filename)
{
//通过mime判断是否是真正的图片
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filename);
finfo_close($finfo);
$type = $mime;
if(!in_array($type,array('image/jpeg','image/png')))
{
return array('error'=>'F');
}else{
switch ($type){
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext='jpg';
$quality = 100;
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
$quality = 100;
break;
default:
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext='jpg';
$quality = 100;
}
$downloadcfg = array('creatfunc'=>$image_create_func,
'savefunc'=>$image_save_func,
'ext'=>$new_image_ext,
'mime'=>$mime,
'quality'=>$quality);
return $downloadcfg;
}
}//function getimagetype
/**
* thumb
*
*
*
*/
static function thumb($filename,$w,$h){
$imgct = files::getImageType($filename);
if(isset($imgct['error'])) exit("Error");
//var_dump($imgct);exit();
$src = $imgct['creatfunc']($filename);
list($width_orig, $height_orig) = getimagesize($filename);
if($width_orig>$w)
{
$width = $w;
$ratio = $width_orig/$w;
$height = $height_orig/$ratio;
}else{
$width = $width_orig;
$height = $height_orig;
}
// 重置图片尺寸
$image_p = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image_p, 255, 255, 255);
imagefilledrectangle($image_p, 0, 0, $width, $height, $white);
imagecopyresampled($image_p, $src, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
header("Content-type: image/jpeg");
imagejpeg($image_p);
imagedestroy($src);
imagedestroy($image_p);
}
/**
*/
static function imageMark($filename,$w,$h){
}
/**
* downloadFile
*
* 文件下载
*
* @param String $fullPath e.g. Zend_Registry::get('upload').$fileurl
*
* return file
*/
static function downloadFile( $fullPath ){
//判断是否已经输出头部
if( headers_sent() )
{
return false;
}
else{
// Required for some browsers
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header('Content-Description: File Transfer');
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary\n");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Content-Length: ".$fsize);
ob_clean();
flush();
if(readfile($fullPath))
return true;
else
return false;
} else
{return false;}
}
} //文件下载
}