Home > Article > Backend Development > PHP100 essence: PHP upload class supports thumbnails_PHP tutorial
Example:
1. Create the file upfile.php with the following code
//If the parameters from the form are received, upload them, otherwise the form will be displayed
if(isset($_FILES[uploadinput])){
//Create directory function, the parameter $directoryName does not have "/" at the end,
//If there is, when / is broken into an array, a null value will appear at the end
Function makeDirectory($directoryName) {
$directoryName = str_replace("","/",$directoryName);
$dirNames = explode(/, $directoryName);
$total = count($dirNames) ;
$temp = ;
for($i=0; $i<$total; $i++) {
$temp .= $dirNames[$i]./;
if (!is_dir($temp)) {
$oldmask = umask(0);
if (!mkdir($temp, 0777)) exit("Cannot create directory $temp");
umask($oldmask);
}
}
return true;
}
if($_FILES[uploadinput][name] <> ""){
// Contains upload file class
require_once (class_upload.php);
//Set the file upload directory
$savePath = "upload";
//Create directory
makeDirectory($savePath);
//Allowed file types
$fileFormat = array(gif,jpg,jpge,png);
//File size limit, unit: Byte, 1KB = 1000 Byte //2cto.com
//0 means no limit, but is affected by the upload_max_filesize setting in php.ini
$maxSize = 0;
// Overwrite the original file? 0 not allowed 1 allowed
$overwrite = 0;
//Initialize upload class
$f = new clsUpload( $savePath, $fileFormat, $maxSize, $overwrite);
// If you want to generate a thumbnail, call the member function $f->setThumb();
//Parameter list: setThumb($thumb, $thumbWidth = 0, $thumbHeight = 0)
//$thumb=1 means to generate thumbnails. When not called, its value is 0
//$thumbWidth thumbnail width, unit is pixel (px), leave it blank and use the default value of 130
//$thumbHeight The height of the thumbnail in pixels (px). If left blank, the default value of 130 is used
$f->setThumb(1);
//The uploadinput in the parameter is the name of the input box for uploading files in the form
//The following 0 means the file name will not be changed. If it is 1, the system will generate a random file name
if (!$f->run(uploadinput,0)){
// Only the last error message can be obtained through $f->errmsg(),
//Detailed information can be obtained in $f->getInfo().
echo $f->errmsg()."
";
}
//The upload results are stored in the array returnArray. //2cto.com
echo "
";<p> </p> <p> print_r($f->getInfo());</p> <p> echo "</p>
";
}
}else{
?>
Send this file:
2. Create the file class_upload.php with the following code
class clsUpload{
var $saveName;//save name
var $savePath; // Save path
var $fileFormat = array(gif,jpg,doc,application/octet-stream);//File format & MIME limitation
var $overwrite = 0; // Overwrite mode
var $maxSize = 0;//Maximum bytes of file
var $ext; // File extension
var $thumb = 0; // Whether to generate thumbnails
var $thumbWidth = 130;//Thumbnail width
var $thumbHeight = 130; // Thumbnail height
var $thumbPrefix = "_";//Thumbnail prefix
var $errno;// Error code
var $returnArray= array();//Return information of all files
var $returninfo= array(); // Return information for each file
// Constructor
// @param $savePath file save path
// @param $fileFormat file format limit array
// @param $maxSize Maximum file size
// @param $overwriet Whether to overwrite 1 allows overwriting 0 prohibits overwriting
Function clsUpload($savePath, $fileFormat=,$maxSize = 0, $overwrite = 0) {
$this->setSavepath($savePath);
$this->setFileformat($fileFormat);
$this->setMaxsize($maxSize);
$this->setOverwrite($overwrite);
$this->setThumb($this->thumb, $this->thumbWidth,$this->thumbHeight);
$this->errno = 0;
}
// Upload
// @param $fileInput The name of the input in the webpage Form
// @param $changeName Whether to change the file name
Function run($fileInput,$changeName = 1){
if(isset($_FILES[$fileInput])){
$fileArr = $_FILES[$fileInput];
if(is_array($fileArr[name])){//Upload multiple files with the same file domain name
for($i = 0; $i < count($fileArr[name]); $i++){
$ar[tmp_name] = $fileArr[tmp_name][$i];
$ar[name] = $fileArr[name][$i];
$ar[type] = $fileArr[type][$i];
$ar[size] = $fileArr[size][$i];
$ar[error] = $fileArr[error][$i];
$this->getExt($ar[name]);//Get the extension and assign it to $this->ext, it will be updated next time in the loop
$this->setSavename($changeName == 1 ? : $ar[name]);//Set the save file name
if($this->copyfile($ar)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo[error] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
}
return $this->errno ? false : true;