php File Upload classAnd
php upload Filesand PHP upload images, such as PHP upload code is only the form of the expression.
Class Uploader
{
var $_base_dir = null;
var $_rel_dir = null;
var $_random_fname = false;
var $_random_fname_len = 5;
var $_fname_filter = null;
var $_ftype_filter = null;
var $_origin_paths = array ();
function Uploader ($base _dir, $rel _dir)
{
$this->_base_dir = $base _dir;
$this->_rel_dir = $rel _dir;
}
function Setrandomfilename ($random _fname, $random _fname_len=5)
{
$this->_random_fname = $random _fname;
$this->_random_fname_len = $random _fname_len;
}
function Setfiletypefilter ($filter)
{
$this->_ftype_filter = $filter;
}
function AddFile ($file, $origin _path= ")
{
$file = Trim ($file);
$origin _path = Trim ($origin _path);
if (Array_key_exists ($file, $this->_origin_paths))
Return
$this->_origin_paths[$file] = $origin _path;
}
function upload ()
{
foreach ($this->_origin_paths as $file = $origin _path)
{
$result = $this->_uploadfile ($file, $origin _path);
if ($result! = ' Success ')
return $result;
}
Return ' Success ';
}
/*
* @desc Upload Attachments
* @return successfully returned success failed return failure type
* @param $file file name $orgin _path file path
*/
function _uploadfile ($file, $origin _path)//upload attachment
{
$ffile = $_files[$file] [' tmp_name ']; Temporary file names stored on the server after the files have been uploaded.
$fname = $_files[$file [' name ']; The original name of the client machine file.
$fsize = $_files[$file [' size ']; Size of uploaded file
$ftype = $_files[$file [' type ']; MIME type of File
$new _path = ";
if (!empty ($fname) && is_uploaded_file ($ffile))
{
if (!empty ($this->_ftype_filter) &&!is_null ($this->_ftype_filter))
{
$match = false;
$extensions = Explode (', ', $this->_ftype_filter);
foreach ($extensions as $extension)
{
if (Strtolower ($fname, '. ')) = = '. '. Strtolower (Trim ($extension)))
{
$match = true;
Break
}
}
if (! $match)
Return ' Errorfiletypefilternotmatch ';
}
$fpath = $this->_base_dir. $this->_rel_dir;
if ($this->_random_fname)
$fname = $this->_getuniquefilename ($fname, $this->_random_fname_len);
Copy ($ffile, $fpath. $fname) or Die (' upload failed! ');
$new _path = $this->_rel_dir. $fname;
}
if (!empty ($origin _path) &&!empty ($new _path) && $origin _path!= $new _path)
{
$this->delete ($origin _path);
}
if (!empty ($new _path))
$this->_origin_paths[$file] = $new _path;
$Erroe =$_files[$file] [' ERROR '];
Switch ($Erroe) {
Case 1:
Return ' errexceeduploadmaxfilesize ';
Break
Case 2:
Return ' errexceedhtmlmaxfilesize ';
Break
Case 3:
Return ' Errpartfiletrans ';
Break
Case 4:
Return ' Errnofiletrans ';
Break
Default
Return ' Success ';
}
}
/*
* @desc Get the path
* @return Path
* @param No
*/
function GetFilePath ()
{
return $this->_origin_paths;
}
function Getfileabspath ()
{
$paths = Array ();
foreach ($this->_origin_paths as $path)
{
$paths [] = $this->_base_dir. $path;
}
return $paths;
}
function Delete ($fpath)
{
if (!empty ($fpath) && is_file ($this->_base_dir. $fpath))
Unlink ($this->_base_dir $fpath) or Die (' unlink error ');
}
function _getuniquefilename ($fname, $len)
{
$timestamp = Date (' Ymdhis ');
Srand (Double) microtime () *1000000);
For ($i =0, $randfname = "; $i < $len; $i + +)
{
$num = rand (0, 35);
if ($num < 10)
$randfname. = Chr (ord (' 0 ') + $num);
Else
$randfname. = Chr (Ord (' a ') + $num-10);
}
return $timestamp. ' _ '. $randfname. Strtolower (STRRCHR ($fname, '. '));
}
}
?>
http://www.bkjia.com/phpjc/445069.html www.bkjia.com true http://www.bkjia.com/phpjc/445069.html techarticle php File Upload class and PHP upload files and php upload images and other PHP The upload code is just a different form of expression. PHP class Uploader {var $_base_dir = null; var $_rel_dir = nul ...