PHP picture upload class, classic way, but upload efficiency is also possible, I used a class, at that time to make some changes to this class to meet their specific needs of the function, PHP familiar with this upload class to do optimization and modification, followed by the call method, Let PHP developers upload pictures easy to do, first class code:
<?php class Fileupload_single {//user define-------------------------------------var $accessPath; var $fileSize =20
0; var $defineTypeList = "Jpg|jpeg|gif|bmp";//string jpg|gif|bmp ... var $filePrefix = "Useruplod_";//file name prefix after upload, can be set to empty Var $
changnamemode;//picture Renaming rule, temporarily only three categories, value range: 0 to 2 any value var $uploadFile;//array upload file attribute var $newFileName;
var $error;
function TODO () {//main main class: Set parameters, you can call directly $pass = true;
if (! $this-> Getfileattri ()) {$pass = false;
} if (! $this-> Checkfilemimetype ()) {$pass = false; $this-> error. = Die ("<script language=\ javascript\" >alert (' picture type is incorrect, allow format: jpg|jpeg|gif|bmp.
'); History.back () </script> ");
} if (! $this-> checkfileattri_size ()) {$pass = false; $this-> error. = Die ("<script language=\" javascript\ ">alert" (' The uploaded file is too large, please make sure it is within 200K.)
'); History.back () </script> ");
return false;
} if (! $this-> Movefiletonewpath ()) {$pass = false; $this-> error. = Die ("<script LANGuage=\ "javascript\" >alert (' upload failed! File Move Error!
'); History.back () </script> ");
return $pass;
function Getfileattri () {foreach ($_files as $tmp) {$this-> uploadfile = $tmp; Return (Empty ($this-> uploadfile[' name '))?
False:true;
function Checkfileattri_size () {if (! empty ($this-> fileSize)) {if (Is_numeric ($this-> fileSize)) {if ($this-> fileSize > 0) {return ($this-> uploadfile[' size '] > $this-> fileSize * 1024)?
False:true;
} else {return false;
} else {return false; } function Changefilename ($prefix = NULL, $mode) {//String $prefix, int $mode $fullName = (isset ($prefix))? $prefix. "
_ ": NULL; Switch ($mode) {case 0: $fullName. = rand (0, 100). "_". Strtolower (Date ("Ldsffyhisa"));
Break Case 1: $fullName. = rand (0, 100). "_". Time ();
Break Case 2: $fullName. = rand (0, 10000). Time ();
Break Default: $fuLlname. = rand (0, 10000). Time ();
Break
return $fullName;
function Movefiletonewpath () {$newFileName = NULL; $newFileName = $this-> changefilename ($this-> fileprefix, 2). "." .
$this-> getfiletypetostring ();
Check whether the directory exists, does not exist to create, when I used the time added this function, feel useless on the comment off IT/* $isFile = file_exists ($this-> accesspath);
Clearstatcache ();
if (! $isFile &&!is_dir ($this-> Accesspath)) {echo $this-> Accesspath;
@mkdir ($this-> accesspath); }*/$array _dir=explode ("/", $this-> Accesspath);//place multilevel directories in the array for ($i =0; $i <count ($array _dir); $i + +) {$path. = $a rray_dir[$i]. "
/";
if (!file_exists ($path)) {mkdir ($path); }/////////////////////////////////////////////////////////////////////////////////////////////////if (MOVE_UPL Oaded_file ($this-> uploadfile[' tmp_name '), Realpath ($this-> accesspath).
"/". $newFileName)) {$this-> newfilename = $newFileName;
return true; }else{
return false; }/////////////////////////////////////////////////////////////////////////////////////////////////} function
Checkfileexist ($path = null) {return ($path = = null)? False: ((file_exists ($path))? true:false);
function Getfilemime () {return $this->getfiletypetostring ();
function Checkfilemimetype () {$pass = false;
$defineTypeList = Strtolower ($this->definetypelist);
$MIME = Strtolower ($this-> getfilemime ()); if (!empty ($defineTypeList)) {if (!empty ($MIME)) {foreach (Explode ("|", $defineTypeList) as $tmp) {if ($tmp
= = $MIME) {$pass = true;
}} else {return false;
} else {return false;
return $pass; function getfiletypetostring () {if (! empty ($this-> uploadfile[' name ')) {return substr (Strtolower (
$this-> uploadfile[' name '), strlen ($this-> uploadfile[' name ')-3, 3);
}}}?>
Here is the PHP upload class call method, the PHP code is as follows:
<?php
include ' up.class.php ';//load PHP upload class file
if empty ($HTTP _post_files[' image_file ' [' tmp_name '])// Determines whether the received data is empty
{
$tmp = new Fileupload_single;
$tmp-> accesspath = ' upload '//Picture upload directory, here is the current directory upload directory, can modify
if ($tmp-> TODO ())
{
$filename =$ TMP-> newfilename;//generated filename
echo "Picture uploaded successfully, path: upload/". $filename;
} else{
echo $tmp-> error;
}
else{
echo "No picture data to upload";
>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.