php tutorial file upload program
The article provides a complete PHP file upload program example code. It can upload pictures and save them to 1: Save to the directory on a daily basis 2: Save to the directory on a monthly basis. You can also set the uploaded image to generate a watermark,
*/
?>
Upload file program
/***********************
Procedure: Upload files
Functions: Upload files, thumbnails, add watermarks
****************************/
include("common/upfiles.class.php");
$path="../upload/coolsite"; //File upload path
$mix="smallimg"; //Thumbnail path (created under upload)
$mark="markimg"; //The image storage path for adding water (created under upload)
$text = array("www.bKjia.c0m"); //Watermark content
$oupload= new upfiles($path,$mix,$mark); //Instantiate class filesif(isset($_post['up'])){
if($_post['urlid']=='1'){ //Upload pictures Parameter urlid 1: Upload pictures 2: Upload other files..
$oupload->tofile = true; //If turned on, only the thumbnail or watermark image will be saved, and the original image will be deleted
$photo = $oupload->upload("upfile"); //Uploaded file domain
$photourl = $oupload->fileurl."/".$photo;
$newsmallimg = $oupload->smallimg($photo); //Thumbnail function
//$newmarkimg = $oupload->watermark($photo,$text); //Watermark function//echo $newsmallimg; //Output thumbnail path
//echo $newmark; //Output watermark path
//echo ""; //Output thumbnail
//echo ""; //Output watermark image
}else{
$upfilename = $oupload->upload("upfile"); //Uploaded file domain
}
$strjs = "n";
echo $strjs; //Append the last file path to upfile1 and upfile2
}else{
?>
//upfiles.class.php/*==========================
Upload class upfiles.class.php
===========================*/
class upfiles {
/*==========================
//Basic parameter settings
===========================*/
protected $annexfolder = "upload"; //Attachment storage point, default is: upload
protected $dirtype = 2; //1: Save to the directory on a daily basis 2: Save to the directory on a monthly basis
protected $smallfolder = "smallimg"; // Thumbnail storage path, note: it must be a subdirectory under $upload, the default is: smallimg
protected $markfolder = "markimg"; // Watermark image storage path, note: it must be a subdirectory under $upload, the default is: markimg
protected $upfiletype = "jpg gif png rar zip"; //Upload type, the default is: jpg gif png rar zip
protected $upfilemax = 102400; //Upload size limit, unit is "kb", default is: 1024kb
protected $fonttype = "common/equinoxstd.otf"; // Watermark font library
Protected $ maxwidth = 800; // The maximum width of the picture
protected $maxheight = 600; //Maximum height of image
/*==========================
//Initialize upload class
===========================*/
Public function __construct($annexfolder,$smallfolder,$includefolder) {
Switch($this->dirtype)
{
case 1: $attach_subdir = 'day_'.date('ymd'); break;
case 2: $attach_subdir = 'month_'.date('ym'); break;
}
$attach_dir = $annexfolder.'/'.$attach_subdir;
$attach_dir_small = $attach_dir.'/'.$smallfolder;
$attach_dir_mark = $attach_dir.'/'.$includefolder;
$this->rootfolder = $annexfolder;
$this->annexfolder = $attach_dir;
$this->smallfolder = $attach_dir_small;
$this->markfolder = $attach_dir_mark;
//$this->fonttype = $includefolder."/nasaliza.ttf";
}
public function __get($fileurl){
$fileurl = $this->annexfolder;
Return $fileurl;
}
/*=========================
//Upload file
===========================*/
public function upload($inputname) {
//Check if the folder exists
If(!file_exists($this->annexfolder)){
If(!file_exists($this->rootfolder)) @mkdir($this->rootfolder);
If(!file_exists($this->annexfolder)) @mkdir($this->annexfolder);
If(!file_exists($this->smallfolder)) @mkdir($this->smallfolder);
If(!file_exists($this->markfolder)) @mkdir($this->markfolder);
}
If(!file_exists($this->smallfolder)){
@mkdir($this->smallfolder);
}
If(!file_exists($this->markfolder)){
@mkdir($this->markfolder);
}
$this->uptype = $_files[$inputname]["type"];
$this->upname = $_files[$inputname]["name"];
$this->uptmp_name = $_files[$inputname]["tmp_name"];
$this->ups tutorialize = $_files[$inputname]["size"];
$this->uperror = $_files[$inputname]["error"];if($this->uptype){
switch ($this->uptype)///检查上传的类型
{
case "image/pjpeg":
$fileextname = "jpg";
break;
case "image/jpeg":
$fileextname = "jpg";
break;
case "image/gif":
$fileextname = "gif";
break;
case "image/x-png":
$fileextname = "png";
break;
case "application/x-shockwave-flash":
$fileextname = "swf";
break;
case "text/plain":
$fileextname = "txt";
break;
case "application/msword":
$fileextname = "doc";
break;
case "application/vnd.ms-excel":
$fileextname = "xls";
break;
case "application/x-zip-compressed":
$fileextname = "zip";
break;
case "audio/mpeg":
$fileextname = "mp3";
break;
case "audio/x-ms-wma":
$fileextname = "wma";
break;
case "application/pdf":
$fileextname = "pdf";
break;
default: //如果不满足上述类型,那么上传文件被判断为格式不正确!!
//$fileextname =strtolower(substr(strrchr(trim($this->upname), "."),1,4));
//$fileinfo=pathinfo($this->upname);
//$fileextname=$fileinfo['extension'];
$fileextname = "err";
}
}
1 2