Below us is a file image upload class, call method Upfile ($path = "./", $format = "", $maxsize = 0, $over = 0): constructor parameters (save path, upload format, upload maximum number of bytes, overwrite files with the same name), if you want to upload Multiple files on the foreach ($_files as $key + $val) again $filear [] = $f->upload ($key);//Returns an array of file names after uploading.
Below us is a file image upload class, call method Upfile ($path = "./", $format = "", $maxsize = 0, $over = 0): constructor parameters (save path, upload format, upload maximum number of bytes, overwrite files with the same name), if you want to upload Multiple files on the foreach ($_files as $key + $val) again $filear [] = $f->upload ($key);//Returns an array of file names after uploading.
*/
Class upfile{
Uploading file information
var $filename;
Save Name
var $savename;
Save path
var $savepath;
File format qualified, no limit format when empty
var $format = "";
Overlay mode
var $overwrite = 0;
/* $overwrite = 0 o'clock do not overwrite files with the same name
* $overwrite = 1 o'clock overwrite files with the same name
*/
File Maximum bytes
var $maxsize = 210000000;
File name extension
var $ext;
/* Constructor
* $path Save Path
* $format file format (separated by commas)
* $maxsize file maximum limit, 0 is the default value
* $over Cover parameters
*/
function Upfile ($path = "./", $format = "", $maxsize = 0, $over = 0) {
if (!file_exists ($path)) {
$this->halt ("Specified directory [". $path. "] does not exist. ");
}
if (!is_writable ($path)) {
$this->halt ("Specified directory [". $path. "] Not writable. ");
}
$path = Str_replace ("", "/", $path);
$this->savepath = substr ($path,-1) = = "/"? $path: $path. " /";//save path
$this->overwrite = $over;//whether to cover the same name file
$this->maxsize =! $maxsize? $this->maxsize: $maxsize;//File Max bytes
$this->format = $format;
}
/*
* Function: Detect and organize files
* $form file name
* $filename upload file Save name, empty or upload multiple files automatically generated by the system name
* $filename = 1, and upload more than one file name file, the file is saved as the original upload file name.
*/
function upload ($form, $filename = "") {
if (!isset ($_files[$form])) {
$this->halt ("The specified file domain name does not exist. ");
}else{
$filear = $_files[$form];
}
if (Is_array ($filear ["name"])) {//upload the same file name multiple files
$outfile = Array ();//return the file name arrays
for ($i = 0; $i < count ($filear ["name"]), $i + +) {
$ar ["name"] = $filear ["name"] [$i];
$ar ["tmp_name"] = $filear ["Tmp_name"] [$i];
$ar ["size"] = $filear ["Size"] [$i];
$ar ["error"] = $filear ["Error"] [$i];
$this->getext ($ar ["name"]);//Get extension
$this->set_savename ($filename = = 1? $ar ["Name"]: "");//Set Save file name
$outfile [] = $this->copyfile ($ar);
}
return $outfile;
}else{//uploading a single file
$this->getext ($filear ["name"]);//Get extension
$this->set_savename ($filename);//Set Save file name
return $this->copyfile ($filear);
}
return false;
}
/*
* Function: Detect and copy upload file
* $filear An array of uploaded file data
*/
function CopyFile ($filear) {
if ($filear ["size"] > $this->maxsize) {
$this->halt ("Upload file". $filear ["name"]. "Size exceeds system qualified value [". $this->maxsize. "bytes], cannot be uploaded. ");
}
if (! $this->overwrite && file_exists ($this->savename)) {
$this->halt ($this->savename. "FileName already exists. ");
}
if (! $this->chkext ()) {
$this->halt ($this->ext. "File format does not allow uploading. ");
}
if (!copy ($filear ["Tmp_name"], $this->savepath. $this->savename)) {
$errors = Array (0=> "file upload succeeded",
1=> "The uploaded file exceeds the value of the Upload_max_filesize option limit in the PHP tutorial. ini. ",
2=> "The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. ",
3=> "files are only partially uploaded. ",
4=> "No files were uploaded. ");
$this->halt ($errors [$filear ["error]]);
}else{
@unlink ($filear ["Tmp_name"]);//Delete temporary files
Return $this->savename;//Returns the upload file name
}
}
/*
* Function: Get the file name extension
* $filename as file name
*/
function Getext ($filename) {
if ($filename = = "") return;
$ext = Explode (".", $filename);
$this->ext = $ext [Count ($ext)-1];
}
/*
* Function: Detect whether the file type is allowed
*/
function Chkext () {
if ($this->format = = "" | | In_array (Strtolower ($this->ext), Explode (",", Strtolower ($this->format))) return true;
else return false;
}
/*
* Function: Set File Save name
* $savename save name, if empty, the system automatically generates a random file name
*/
function Set_savename ($savename = "") {
if ($savename = = "") {//If the file name is not set, a random file name is generated
Srand (Double) microtime () * 1000000);
$rnd = rand (100,999);
$name = Date (' u ') + $rnd;
$name = $name. ".". $this->ext;
} else {
$name = $savename;
}
$this->savename = $name;
}
/*
* Function: Error prompt
* $msg for output information
*/
function Halt ($msg) {
echo "Note:". $msg;
Exit
}
/*
*
* Mainly used to delete uploaded files, do not return
* Parameter $file: file path
*/
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 ();
}
}
}
/*
How to use
* File Upload Class
Upfile ($path = "./", $format = "", $maxsize = 0, $over = 0): constructor parameters (save path, upload format, upload maximum number of bytes, overwrite files with the same name)
Instance
On the flyer file
if (Isset ($_files["Files"]))
{
$filear =array ();
$filear = $f->upload ("files");//Returns the file name after uploading
Echo $filear;
}
Upload multiple different file name files
if (isset ($_files) {
foreach ($_files as $key = $val)
$filear [] = $f->upload ($key);//Returns an array of file names after uploading
}
*/
http://www.bkjia.com/PHPjc/631721.html www.bkjia.com true http://www.bkjia.com/PHPjc/631721.html techarticle below us is a file image upload class, called method Upfile ($path = "./", $format = "", $maxsize = 0, $over = 0): constructor parameters (save path, upload format, upload maximum word ...)