php 上傳類(自家用)
2024-05-04 23:04:25
供稿:網友
<?php
/**
* file up load class
* @version 1.0.0 (thu aug 18 01:32:39 cst 2005)
* @author sanshi
*/
class upload
{
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:00:18 cst 2005
* @param string $info 文件內容
* @param string $filename 生成的文件名
* @return boolean 建立成功返回true
* @deprecated
* 建立html文件
*/
function createhtml( $info ,$filename )
{
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:03:09 cst 2005
* @return void
* @deprecated
* 構造函數
*/
function download()
{}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:03:55 cst 2005
* @param string $filefield 在表單中的字段名
* @param string $length 限制的長度
* @return boolean 成功返回true
* @deprecated
* 功能實現函數
*/
function init($filefield,$length='')
{
$files = $_files[$filefield];
//用戶名需要改動,根據自己的實際情況做改動
$username = 'sanshi';
$filename = $files['name'];
$filetype = $files['type'];
$filetemp = $files['tmp_name'];
$filesize = empty( $length ) ? ($files['size']+10) : $length;
$fileerror= $files['error'];//這塊也許php4中沒有
//改為
//if( $this->_istype( $filename ) || $this->_isbig( $length ) )
if( !$this->_istype( $filename ) || $this->_isbig( $length ) || $fileerror != 0 )
{
//print_r ($files);
return false;
}else{
$path = $this->_createdir( $username );//取得路徑
$createfilename = $username . "_" . time();//設置當前文件名
$createfiletype = $this->getfiletype($filename);//設置文件類別
return @move_uploaded_file($filetemp,$path.$createfilename.'.'.$createfiletype) ? true : false;
}
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:07:43 cst 2005
* @param int $length 上傳限制的大小
* @return boolean 超過返回true
* @deprecated
* 判斷是否超過預定大小
*/
function _isbig($length)
{
$bigest = '';
return $big > $bigest ? true : false;
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:08:55 cst 2005
* @param string $filename 文件名
* @return string $filetype 文件后綴
* @deprecated
* 取得文件后綴(只取得文件的最后一個后綴名)
*/
function getfiletype($filename)
{
return end(explode('.',$filename));
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:10:41 cst 2005
* @param string $filename 文件名
* @param boolean $method 是否檢查多個后綴默認false
* @param int $postfix 后綴個數默認為2
* @return boolean 存在返回true
* @deprecated
* 檢查文件的后綴是否在類別數組中,類別數組自己設置
* 如果$method設置為true則檢查文件有幾個后綴
*/
function _istype($filename,$method='false',$postfix=2)
{
//設置類別數組
$type = array('jpeg',
'gif',
'bmp',
'exe');
$filename = strtolower( $filename );
$filetypearray = explode( '.',$filename );
$filetype = end( $filetypearray );
//判斷是否有一個文件有多個后綴
if($method)
{
if( count( $filetypearray ) > (is_int($postfix) ? $postfix : 2) )
{
return false;
}
}
return in_array($filetype,$type);
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:17:19 cst 2005
* @param string $username
* @return string $path
* @deprecated
* 建立目錄 目錄格式 年/月/日/用戶名/
* 權限為755
*/
function _createdir($username)
{
$root = '';
$pathsign = directory_separator;
$y = date('y').$pathsign;
$m = date('m').$pathsign;
$d = date('d').$pathsign;
$path = $root . $y . $m . $d . $username;
$dirarray = explode( $pathsign,$path);
$tempdir='';
foreach ( $dirarray as $dir)
{
$tempdir.= $dir.$pathsign;
$isfile = file_exists( $tempdir );
clearstatcache();
if( ! $isfile && !is_dir( $tempdir ) )
{
@mkdir($tempdir,0755);
}
}
return $path . $pathsign;
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:19:32 cst 2005
* @param string $dirname 目錄名
* @return boolean 可以操作返回true
* @deprecated
* 判斷操作是否在上傳目錄
*/
function _isdel($dirname)
{
//注意uploaddir,一定要與真正使用目錄相對應
$uploaddir = '';
$uploaddir = preg_replace('/////','//',$uploaddir);
$format = "/^{$uploaddir}/";
return preg_match( $format,$dirname );
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:25:58 cst 2005
* @param string $filename 文件名
* @return boolean 刪除文件成功返回true
* @deprecated
* 刪除文件
*/
function delfile( $filename )
{
$cur_dir = dirname(trim($filename));
if( $this->_isdel( $cur_dir ) )
{
return @unlink( $filename ) ? true : false;
}else{
return false;
}
}
/**
*
* @author sanshi
* @version 1.0.0 thu aug 18 01:27:43 cst 2005
* @param string $diename 目錄名
* @return boolean 刪除成功返回true
* @deprecated
* 刪除目錄 目錄下如果有文件不能刪除
*/
function deldir( $dirname )
{
if( $this->_isdel($dirname) && is_dir( $dirname ) )
{
return @rmdir( $dirname ) ? true : false;
}else{
return false;
}
}
}
?>
<?php
//使用
/*
include 'upload.class.php';
$up = new upload();
if($up->init("file"))
{
echo 'success';
}else{
echo 'failure';
}
*/
?>