PHP File Upload Class example detailed _php skills

Source: Internet
Author: User
Tags mkdir php file upload

This article describes the php file upload class. Share to everyone for your reference, specific as follows:

This shows the FileUpload.class.php file upload class, which uses two constants that can be defined in the Web site configuration file:

Define (' Root_path ', DirName (__file__)); Site root directory
define (' Updir ', '/uploads/');//Upload home directory

The specific code is as follows:

<?php//Upload file class FileUpload {private $error; Error code private $maxsize;  Form maximum value private $type; Type private $TYPEARR = Array (' Image/jpeg ', ' image/pjpeg ', ' image/png ', ' image/x-png ', ' image/gif ');  Type collection private $path;  directory path private $today;  Today's directory is private $name;  File name private $tmp; Temporary files private $linkpath; Link path private $linktotay; Today's directory (relative)//construction method, initializing Public function __construct ($_file,$_maxsize) {$this->error = $_files[$_file][' er
       Ror '];
       $this->maxsize = $_maxsize/1024;
       $this->type = $_files[$_file][' type ']; $this->path = Root_path.
       Updir; $this->linktotay = Date (' Ymd '). '
       /';
       $this->today = $this->path. $this->linktotay;
       $this->name = $_files[$_file][' name '];
       $this->tmp = $_files[$_file][' tmp_name '];
       $this->checkerror ();
       $this->checktype ();
       $this->checkpath ();
    $this->moveupload ();
 }//return path   Public Function GetPath () {$_path = $_server["Script_name"];
       $_dir = DirName (dirname ($_path));
       if ($_dir = = ' \ ') $_dir = '/';
       $this->linkpath = $_dir. $this->linkpath;
    return $this->linkpath; }//Move file Private Function Moveupload () {if (Is_uploaded_file ($this->tmp)) {if (!move_uploaded _file ($this->tmp, $this->setnewname ())) {Tool::alertback (' Warning: Upload failed!
         '); } else {Tool::alertback (' Warning: temp file does not exist!
       ');
       }//Set new file name Private function Setnewname () {$_namearr = explode ('. ', $this->name);
       $_postfix = $_namearr[count ($_namearr)-1]; $_newname = Date (' Ymdhis '). Mt_rand (100,1000).
       $_postfix;
       $this->linkpath = updir. $this->linktotay.$_newname;
    return $this->today.$_newname;
         }//Verify directory Private Function Checkpath () {if (!is_dir ($this->path) | |!is_writeable ($this->path)) { if (!mkdir ($this->path)) {Tool::alertback (' Warning: Home directory creation failed!
         '); } if (!is_dir ($this->today) | | |!is_writeable ($this->today)) {if (!mkdir ($this->today)) {Tool::alertback (' Warning: subdirectory creation failed!
         ');
         }}//authentication type Private Function Checktype () {if (!in_array ($this->type, $this->typearr)) { Tool::alertback (' Warning: illegal upload type!
       '); }///Validation error private Function CheckError () {if (!empty ($this->error)) {switch ($this->er ROR) {case 1:tool::alertback (' Warning: Upload value exceeds the agreed maximum value!)
              ');
            Break Case 2:tool::alertback (' Warning: Upload value exceeded '. $this->maxsize. ' kb!
              ');
            Break Case 3:tool::alertback (' Warning: Only part of the file is uploaded!
              ');
            Break Case 4:tool::alertback (' Warning: No files are uploaded!
              ');
            Break Default:tool::alertback (' Warning: unknown error!
      ');   }}}?>

 

Among them, the use of a static tool class Tool.class.php, the code is as follows:

Tool.class.php

<?php
  class Tool {
     //Bounce window returns
     the static public function Alertback ($_info) {
       echo "<script type= ' text/ JavaScript ' >alert (' $_info '); History.back ();</script> ";
       Exit ();
     }     Window assignment close static public
     function Alertopenerclose ($_info,$_path) {
       echo "<script type= ' Text/javascript ' >alert (' $_info ');</script> ";
       echo "<script type= ' text/javascript ' >opener.document.content.thumbnail.value= ' $_path ';</script>";
       echo "<script type= ' Text/javascript ' >opener.document.content.pic.style.display= ' block ';</script>";
       echo "<script type= ' text/javascript ' >opener.document.content.pic.src= ' $_path ';</script>";
       echo "<script type= ' Text/javascript ' >window.close ();</script>";
       Exit ();
     } }
?>

Here's a demo of an example, take a look at the following steps:

1, first create a index.php page, make a form

index.php

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

3, through the upload.php file call file Upload class implementation upload, and the path assigned to the input label and display picture

<?php
  require ' FileUpload.class.php ';
  if (isset ($_post[' send ')) {
    $_fileupload = new FileUpload (' pic ', $_post[' max_file_size '));
    $_path = $_fileupload->getpath ();
    Tool::alertopenerclose (' File Upload success! ', $_path);
  } else {
    tool::alertback (' Warning: The file is too large or another unknown error caused the browser to crash! ');
  }
? >

More interested in PHP related content readers can view the site topics: "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.