background image

 function __construct($file, $path, $size = 2097152, $type = '') {

$this->user_post_file = $file;
if(!is_dir($path)){ //存储路径文件不存在就创建

mkdir($path);
chmod($path,0777);

}
$this->save_file_path = $path;
$this->max_file_size = $size;  //如果用户不填写文件大小,则默认为 2M.
if ($type != '')

$this->allow_type = $type;

}
 
 /**
  * 存储用户上传文件,检验合法性通过后,存储至指定位置。
  */
 function upload() {
  
  for ($i = 0; $i < count($this->user_post_file['name']); $i++) {
   //如果当前文件上传功能,则执行下一步。
   if ($this->user_post_file['error'][$i] == 0) {
    //取当前文件名、临时文件名、大小、扩展名,后面将用到。
    $name = $this->user_post_file['name'][$i];
    $tmpname = $this->user_post_file['tmp_name'][$i];
    $size = $this->user_post_file['size'][$i];
    $mime_type = $this->user_post_file['type'][$i];
    $type = $this->getFileExt($this->user_post_file['name'][$i]);
    //检测当前上传文件大小是否合法。
    if (!$this->checkSize($size)) {
     $this->last_error = "The file size is too big. File name is: ".$name;
     $this->halt($this->last_error);
     continue;
    }
    //检测当前上传文件扩展名是否合法。
    if (!$this->checkType($type)) {
     $this->last_error = "Unallowable file type: .".$type." File name is: ".$name;
     $this->halt($this->last_error);
     continue;
    }
    //检测当前上传文件是否非法提交。
    if(!is_uploaded_file($tmpname)) {
     $this->last_error = "Invalid post file method. File name is: ".$name;
     $this->halt($this->last_error);
     continue;
    }