Php file Image Upload program

Source: Internet
Author: User
Tags file upload php tutorial

<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = utf-8"/>
<Title> php Tutorial File Image Upload program </title>
</Head>

<Body>

<Form enctype = "multipart/form-data" action = "upx. php" method = "post">
<Input name = "swfile" type = "file">
<Input type = "submit" value = "upload">
</Form>
</Body>
</Html>
Upx. Php file
<? Php
// Upload operation
Require_once './libs/uploadx. Php ';
$ Upx = new uploadx ();
$ Upx-> uploadx_form = 'swfile ';
$ Upx-> uploadx_save = "upload ";
$ Upx-& gt; uploadx_size = "1024 ";
$ Upx-> uploadx_name = time ();
$ Upx-> file ();
Print_r ($ upx-> file );

?>
Uploadx. php files
<? Php
/*

Usage:

Html table single page
-------------------------------------------------------------------------
<Form enctype = "multipart/form-data" action = "upload. php" method = "post">
<Input name = "swfile" type = "file">
<Input type = "submit" value = "upload">
</Form>
-------------------------------------------------------------------------


Upload. php processing page
-------------------------------------------------------------------------
<? Php
Require_once './uploadx. Php ';
$ Upx = new uploadx ();
$ Upx-> uploadx_form = 'swfile'; // form control name (name of the form Upload control <input name = "swfile" type = "file"/>)
$ Upx-> uploadx_save = "temp"; // Save the file directory (the Upload file storage directory can be a relative or absolute path)
$ Upx-> uploadx_type = 'jpg | gif | png | swf '; // specifies the upload type based on the suffix. Each suffix is separated by "|)
$ Upx-> uploadx_size = "1024"; // The upload size (unit: kb. Example: 1024 = KB)
$ Upx-> uploadx_name = time (); // the uploaded file name (which can be customized. Example: date ("y-m-d", time ()))

If ($ upx-> file ()){
Echo "uploaded <br/> ";
Echo "name->". $ upx-> file ['name']. "<br/> ";
Echo "path->". $ upx-> file ['path']. "<br/> ";
Echo "size->". $ upx-> file ['size']. "<br/> ";
Echo "type->". $ upx-> file ['type']. "<br/> ";
Echo "time->". $ upx-> file ['Time']. "<br/> ";
Echo "result->". $ upx-> file ['info']. "<br/> ";
 
} Else {
Echo $ upx-> file ['info'];
}

 

-------------------------------------------------------------------------
*/
Class uploadx {

       
Public $ uploadx_form; // form control name
Public $ uploadx_save; // Save the file directory
Public $ uploadx_type; // Upload type allowed
Public $ uploadx_size; // upload size allowed
Public $ uploadx_name; // name of the uploaded file
      
Function _ construct () {// initialize the function
$ This-> uploadx_form = 'Attach ';
$ This-> uploadx_save = 'temp ';
$ This-> uploadx_type = 'jpg | gif | png | swf | flv | rar | 7z | zip | doc | docx | ppt | pptx | xls | xlsx | txt | pdf | wav | mp3 | wma | rm | rmvb | wmv ';
$ This-> uploadx_size = '000000 ';
$ This-> uploadx_info = false;
       }
      
Function mkdirs ($ path, $ mode = 0777 ){
$ Rootdir = '';
If (substr ($ path, 0, 1) = '/') $ rootdir = $ _ server ['document _ root'];
$ Path = $ rootdir. $ path;
If (! Is_dir ($ path )){
$ This-> mkdirs (dirname ($ path), $ mode );
Mkdir ($ path, $ mode );
          }
Return true;
      }

 

Function file (){
If (! Isset ($ _ files [$ this-> uploadx_form]) {
$ This-> file = array ('file' => false, 'info' => 'Upload error! Check whether the form Upload control name ['. $ this-> uploadx_form.'] is correct! ');
Return false;
              }
             
Switch ($ _ files [$ this-> uploadx_form] ['error']) {
                
Case 1:
$ This-> file = array ('file' => false, 'info' => 'specifies that the size of the uploaded file exceeds the server limit! ');
Return false;
Break;
                
Case 2:
$ This-> file = array ('file' => false, 'info' => 'indicates that the size of the uploaded file exceeds the size limit of the form! ');
Return false;
Break;
                                            
Case 3:
$ This-> file = array ('file' => false, 'info' => 'only some files are uploaded, and the files are incomplete! ');
Return false;
Break;
                                                     
Case 4:
$ This-> file = array ('file' => false, 'info' => 'you have not selected to upload any files! ');
Return false;
              }
             
$ Postfix = pathinfo ($ _ files [$ this-> uploadx_form] ['name'], pathinfo_extension );
If (stripos ($ this-> uploadx_type, $ postfix) === false ){
$ This-> file = array ('file' => false, 'info' => 'indicates that the file type to be uploaded has exceeded the limit, and the file type can be uploaded :'. $ this-> uploadx_type );
Return false;
               
              }
         
If (round ($ _ files [$ this-> uploadx_form] ['size']/1024)> $ this-> uploadx_size ){
$ This-> file = array ('file' => false, 'info' => 'specifies that the size of the uploaded file exceeds the size limit. The file upload range is :'. $ this-> uploadx_size. 'KB ');
Return false;
              }               
           
If ($ this-> mkdirs ($ this-> uploadx_save )){
$ This-> uploadx_name = isset ($ this-> uploadx_name )? $ This-> uploadx_name. '.'. $ postfix: $ _ files [$ this-> uploadx_form] ['name'];
If (! @ Move_uploaded_file ($ _ files [$ this-> uploadx_form] ['tmp _ name'], $ this-> uploadx_save. '/'. $ this-> uploadx_name )){
$ This-> file = array ('file' => false, 'info' => 'an error occurs during file upload and storage. Check the path or directory permission .');
Return false;
                }
} Else {
$ This-> file = array ('file' => false, 'info' => 'The server directory does not exist. Automatic Directory creation fails. Check whether you have the permission! ');
Return false;
              }                
             
@ Chmod ($ this-> uploadx_save. '/'. $ this-> uploadx_name, 0777 );
$ This-> file = array (
'File' => true,
'Name' => $ this-> uploadx_name,
'Path' => $ this-> uploadx_save. '/'. $ this-> uploadx_name,
'Size' = >$ _ files [$ this-> uploadx_form] ['size'],
'Type' => $ postfix,
'Time' => time (),
'Info' => 'the upload is successful! '
);
Return true;
                        
     }


}

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.