The following is a file and image upload class, calling the method upfile($path = "./", $format = "", $maxsize = 0, $over = 0): constructor parameters (save path, upload format, The maximum number of bytes to upload, whether to cover the file with the same name), if you want to upload multiple files, foreach($_FILES as $key=>$val) and then $filear[] = $f->upload($key);//return Array of file names after uploading.
The following is a file and image upload class, calling the method upfile($path = "./", $format = "", $maxsize = 0, $over = 0): constructor parameters (save path, upload format, The maximum number of bytes to upload, whether to cover the file with the same name), if you want to upload multiple files, just foreach($_files as $key=>$val) and then $filear[] = $f->upload($key);/ /Returns the array of file names after uploading.
*/
class upfile{
//Upload file information
var $filename;
// Save name
var $savename;
// Save path
var $savepath;
// File format limitation, no format restriction when empty
var $format = "";
// Overlay mode
var $overwrite = 0;
/* When $overwrite = 0, the file with the same name will not be overwritten
* When $overwrite = 1, overwrite the file with the same name
*/
//Maximum bytes of file
var $maxsize = 210000000;
//File extension
var $ext;
/* Constructor
* $path save path
* $format file format (separated by commas)
* $maxsize file maximum limit, 0 is the default value
* $over override parameters
*/
function upfile($path = "./", $format = "", $maxsize = 0, $over = 0){
if(!file_exists($path)){
$this->halt("The specified directory [ ".$path." ] does not exist.");
}
if(!is_writable($path)){
$this->halt("The specified directory [ ".$path." ] is not writable.");
}
$path = str_replace("","/", $path);
$this->savepath = substr($path, -1) == "/" ? $path : $path."/";//Save path
$this->overwrite = $over;//Whether to overwrite files with the same name
$this->maxsize = !$maxsize ? $this->maxsize : $maxsize;//Maximum bytes of file
$this->format = $format;
}
/*
* Function: Detect and organize files
* $form File field name
* $filename is the name for saving the uploaded file. If it is empty or the name is automatically generated by the system when uploading multiple files
* $filename = 1, and when multiple files with the same file domain name are uploaded, the files will be saved as the original uploaded 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 multiple files with the same file domain name
$outfile = array();//Return file name array
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 the extension
$this->set_savename($filename == 1 ? $ar["name"] : "");//Set the save file name
$outfile[] = $this->copyfile($ar);
}
Return $outfile;
}else{//Upload a single file
$this->getext($filear["name"]);//Get the extension
$this->set_savename($filename);//Set the save file name
Return $this->copyfile($filear);
}
return false;
}
/*
* Function: Detect and copy uploaded files
* $filear Upload file data array
*/
function copyfile($filear){
if($filear["size"] > $this->maxsize){
$this->halt("The size of the uploaded file ".$filear["name"]." exceeds the system limit [".$this->maxsize." bytes] and cannot be uploaded.");
}
if(!$this->overwrite && file_exists($this->savename)){
$this->halt($this->savename."The file name already exists.");
}
if(!$this->chkext()){
$this->halt($this->ext."The file format does not allow uploading.");
}
if(!copy($filear["tmp_name"], $this->savepath.$this->savename)){
$errors = array(0=>"File uploaded successfully",
1=>"The uploaded file exceeds the value limited by the upload_max_filesize option in 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=>"Only part of the file was uploaded. ",
4=>"No files were uploaded. ");
$this->halt($errors[$filear["error"]]);
}else{
@unlink($filear["tmp_name"]);//Delete temporary files
Return $this->savename;//Return the uploaded file name
}
}
/*
* Function: Get file extension
* $filename is the file name
*/
function getext($filename){
if($filename == "") return;
$ext = explode(".", $filename);
$this->ext = $ext[count($ext)-1];
}
/*
* Function: Check 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 is the save name. If it is empty, the system automatically generates a random file name
*/
function set_savename($savename = ""){
if ($savename == "") { // If the file name is not set, generate a random file name
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 is the output information
*/
function halt($msg){
echo "Note: ".$msg;
exit;
}
/*
*
*Mainly used to delete uploaded files without returning
*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");
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, maximum number of bytes uploaded, whether to overwrite the file with the same name)
* Example:
//Upload multiple files with different file domain names
if(isset($_files){
foreach($_files as $key=>$val)
$filear[] = $f->upload($key);//Return the uploaded file name array
}
*/