Developing Smarty PHP Knowledge accumulation

Source: Internet
Author: User
 
  
 <title>Insert Title here</title>
 Uploading files。 The validated file is saved to the specified directory after validation. * Can returnUploading FilesUseful information for use by other programs.  (such as file name, type, size, save path) */class uploadfile {protected $user _post_file = Array ();    User uploaded files protected $save _file_path; Store usersUploading FilesThe path of protected $max _file_size;     Maximum file size protected $last _error;  Record the last error message//default allows users to upload file types protected $allow _type = array (' gif ', ' jpg ', ' png ', ' zip ', ' rar ', ' txt ', ' Doc ', ' pdf ', ' docx ');  protected $final _file_path; The final saved file name protected $save _info = Array ();  Returns a set of useful information that is used to prompt the user. /** * Constructor */function __construct ($file, $path, $size = 2097152, $type = ") {$this->user_post_file = $file; if (!is_d IR ($path)) {//storage path file does not exist to create mkdir ($path);chmod($path, 0777);}  $this->save_file_path = $path; $this->max_file_size = $size;  If the user does not fill in the file size, the default is 2m.if ($type! = ") $this->allow_type = $type;} /** * Storage UsersUploading Files, the verification of legality passed, stored to the specified location.   */function upload () {for ($i = 0; $i < count ($this->user_post_file[' name '); $i + +) {//If the current file upload function, perform the next step.    if ($this->user_post_file[' error '] [$i] = = 0) {//Take the current file name, temporary file name, size, extension, which will be used later.    $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]); Detects the currentUploading FilesWhether the size is legal. if (! $this->checksize ($size)) {$this->last_error = "The file size is too big.     File name is: ". $name;     $this->halt ($this->last_error);    Continue }//Detect currentUploading FilesThe extension is legal. if (! $this->checktype ($type)) {$this->last_error = "unallowable file type:.". $type. "     File name is: ". $name;     $this->halt ($this->last_error);    Continue }//Detect currentUploading Filesis not submitted illegally. if (!is_uploaded_file ($tmpname)) {$this->last_error = "Invalid Post file method.     File name is: ". $name;     $this->halt ($this->last_error);    Continue    }//After moving the file, rename the file with. $basename = $this->getbasename ($name, ".".    $type);    To prevent garbled file names $basename = Iconv ("UTF-8", "gb2312", $basename); The file name after the move $saveas = $basename. Time (). ".".    $type; Save the new file name to the specified directory, format: Store path + file name + time + extension $this->final_file_path = $this->save_file_path. "    /". $saveas; if (!move_uploaded_file ($tmpname, $this->final_file_path)) {$this->last_error = $this->user_post_file['     Error '] [$i];     $this->halt ($this->last_error);    Continue    }//stores information about the current file for other programs to call.                             $this->save_info[] = Array ("name" = = $name, "type" = = $type, "mime_type" = = $mime _type, "Size" = $size, "saveas" = $saveas, "path" = $this->final_file   _path); }} return count ($this->savE_info);  Returns the number of files successfully uploaded}/** * Return some useful information for use elsewhere.  */function Getsaveinfo () {return $this->save_info;}  /** * Detect if user submitted file size is valid */function checksize ($size) {if ($size > $this->max_file_size) {return false;  } else {return true; }}/** * Detects whether a user-submitted file type is valid */function Checktype ($extension) {foreach ($this->allow_type as $type) {if (strcasecm  P ($extension, $type) = = 0) return true; } return false; }/** * Display error message */function halt ($msg) {printf (" %s
\ n ", $msg); }/** * Fetch file extension */function Getfileext ($filename) {$stuff = PathInfo ($filename); return $stuff [' extension ']; }/** * takes the given file file name, not including the extension. */function Getbasename ($filename, $type) {$basename = basename ($filename, $type); return $basename; }} if (!empty ($_post[' submit ')) {$upload = new UploadFile ($_files[' files '], './tmp ');//upload user file, return int value, the number of files successfully uploaded. $num = $upload->upload (); if ($num! = 0) {echo "Upload succeeded
"; echo $num. " File upload Success "; } else {echo ' upload failed
"; }}? >

The above describes the development of smarty PHP knowledge accumulation, including the uploading of files, chmod aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.