PHP File Upload examples and detailed parameters

Source: Internet
Author: User
Tags chmod error code file size file upload html form php file php file upload

  This article mainly introduces the PHP file upload examples and parameters, the need for friends can refer to the

1. Upload Form upload.html   program code HTML     code as follows: <form enctype= "Multipart/form-data" action= "upload.php" method = "POST" >   <input type= "hidden" name= "max_file_size" value= "100000" >   <input name= "UserFile" Type= "File" >   <input type= "Submit" value= "Upload file" >   </form>       1. Note <form E Nctype= "Multipart/form-data" ......> This is a label, we want to implement file upload, must be specified as Multipart/form-data, otherwise the server will not know what to do. 2. It is noteworthy that the hidden value range of the form options max_file_size in file upload.html can limit the size of the uploaded file by setting its value (values). The value of 3.max_file_size is only a suggestion to the browser, and it can actually be bypassed simply. So do not put a limit on the browser to that value. In fact, the maximum value of the uploaded file in the PHP settings will not expire. But it's best to add max_file_size to the form because it avoids the hassle of a user spending time waiting to upload a large file before discovering the file is too big.   PHP upload file involves the parameters   Program code PHP     code as follows: $f =& $HTTP _post_files[' Myfile '];   $dest _dir= ' uploads '//Set upload directory   $dest = $dest _dir. '/'. Date ("Ymd"). " _ ". $f [' name '];//setting file name is date plus filename to avoid duplication   $r =move_uploaded_file ($f [' Tmp_name '], $dest);   chmod ($dest, 0755)//Set the properties of uploaded files       or &NBsp The code is as follows: <?copy ($_files[myfile][tmp_name],$_files[myfile][name]);? >     The contents of the $_files array in the above examples are shown below. We assume that the File upload field name is UserFile (name can be optionally named)   $_files[' userfile ' [' Name '] client machine file's original name. The MIME type of the $_files[' userfile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif". $_files[' userfile ' [' size '] the size of the uploaded file, in bytes. $_files[' UserFile ' [' tmp_name '] files are stored in the server-side temporary file name after being uploaded. $_files[' userfile ' [' Error '] and the file upload related error code value: 0; No error occurred, file upload succeeded. Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini. Value: 2; The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form. Value: 3; The file is only partially uploaded. Value: 4; No files were uploaded.   PHP Default upload limit is the maximum 2M, want to upload more than this set of files, need to adjust PHP, Apache and some other parameters. Below, we briefly introduce some of the parameters of PHP file upload:   file_uploads is allowed to upload files via HTTP switch, the default is on that is open   Upload_tmp_dir upload_tmp_ Dir is used to describe the temporary directory of files placed in PHP upload, to upload files, you have to ensure that the server does not turn off temporary files and have write permissions on the folder, if not specified then PHP use system defaults   upload_max_filesize allowed to upload the maximum size of the file, The default is 2M   PHP     code as follows: <?php   define (' muilti_file_upload ', ' 10 '); Up to 10 files at the same time upload   define (' Max_size_file_upload ',   ' 500000 '); File size not exceeding 5MB  Define (' File_upload_dir ', ' d:/'); Upload file directory  //allow uploaded file name   $array _extention_interdite = Array ('. php ', '. PhP3 ', '. PhP4 ', '. '), '. msi ', '. HT Access ', '. gz '); Upload file extension    //Display information public function   function func_message ($message = ', $ok = ') {  echo ' <table width= ' 100 % "cellspacing=" 0 "cellpadding=" 5 ">";   if ($ok = = true) {  echo ' <tr bgcolor= "#99FF99" ><td width= "M" > </td><td class= "text" &G T '. $message. ' </td></tr> ';  }  //www.jb51.net if ($ok = = False) {  echo ' <tr bgcolor= "#FF99CC" ><td width= "M" > </td& GT;<TD class= "text" > '. $message. ' </td></tr> ';  }   echo ' </table> ';     }  //Process form submission   $action = (isset ($_post[' action '))? $_post[' action ']: ';   $file = (isset ($_post[' file '))? $_post[' file ']: '  ;   if ($file!= ') {      $file = $file. ' /';  }   $message _true = '';   $message _false = ';     Switch ($action) {  case ' upload ':      chmod (file_upload_dir,0777);    &NBSP ; for ($NB = 1; $nb <= muilti_file_upload $nb + +) {      if ($_files[' File_ '. $nb] [' Size '] >= 10) {& nbsp;    if ($_files[' File_ '. $nb] [' size '] <= max_size_file_upload) {  If!in_array (ereg_replace (' ^[ [: Alnum:]] ([-_.]? [[: Alnum:]] *. ', '. ', $_files[' File_ '. $nb] [' name ']], $array _extention_interdite)) {                      if ($_post[' file_name_ '. $nb]!= ') {                &N Bsp         $file _name_final = $_post[' file_name_ '. $nb]. $extension;                      }else {          &N Bsp               $file _name_final = $_files[' File_ '. $nb] [' name '];                       {                & nbsp    //filename modification                       $file _name_final = str TR ($file _name_final, ' aaaaaa ',       ' aaaaaaceeeeiiiiooooouuuuyaaaaaaceeeeiiiioooooouuuuyy ');                       $file _name_final = preg_replace ('/[^.a-z0-1] +)/I ', ' _ ', $file _                             &NBSP ;      name_final);                         $_files[' file_ '. $nb [' name '] = $file _n ame_final;                               move_uploade D_file ($_files[' File_ '. $nb] [' tmp_name '], File_upload_ DIR.             &nbsp               $file. $file _name_final);                         $message _true. = ' uploaded file: '. $_files[' File_ '. $nb [' name ']. ' <br> ';                  }else{              &NB Sp       $message _false. = ' File upload failed: '. $_files[' File_ '. $nb] [' name ']. ' <br> ';                   {                & nbsp }else{                      $message _false. = ' File size exceeds '. max_size_file_upload/1000. ' KB: '                               '. $_files [' File_ '. $nb] [' Tmp_name ']. ' " <br> ';}                   {     }//end for   &NBSp   break;    ?>   <html>   <head>   <title> multi-File upload </title>   <STYLE&GT ;   Border{background-color: #000000}   Box{background-color: #f8f8f9;}   text{ color: #000000;   font-family: Song body;   FONT-SIZE:12PX;   Font-weight:bold}   input, select{font-size:12px}   body {      margin-top:8px;   }   </style>   <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "></head>   <body marginwidth=" 0 "bottommargin=" 0 "leftmargin=" 0 "rightmargin=" 0 ">   <form name= "form" enctype= "Multipart/form-data" method= "post" action= "<?php echo $_server[' php_self ']; gt; " >   <input type= "hidden" name= "action" value= "upload" >   <table  border= "0" cellspacing= "1" cellpadding= "0" align= "center" class= "Border" >   <tr>   <td>   <?php   if ($message _true!= ') { func_message ($message _true, True);}   if ($message _false!= ') { func_message ($message _false, FAL SE); }  ?>   <table width= "100%" border= "0" cellspacing= "5" cellpadding= "2" align= "Center" class= "box" > &N Bsp <?php   for ($NB = 1 $nb <= muilti_file_upload $NB + +) {  ?>   <tr class= "text" > & nbsp <td> uploading files: <?php echo $nb;?></td> <td><input type= "file" Name= "file_<?php echo $nb;?>" ></td>   <td> new file name (including extension): <?php echo $nb;?> </td><td><input type= "text" name= "File_name_<?php Echo $nb?>" ></td>   </tr>   <?php}?>   <tr>   &L T;TD colspan= "2" align= "right" class= "text" > Upload destination Address: <?php echo file_upload_dir;? >     <select name= "file" >          <option value= "" ></option> &N Bsp <?php   $repertoire = Opendir (File_uploaD_DIR);    while ($file = Readdir ($repertoire)) {  $file = Str_replace ('. ', ', $file];       if (IS _dir ($file)) {?> <option value= "<?php echo $file;?>" > <?php echo $file;?>/</option> <?php    } closedir ($repertoire)?> </select> </td> <td colspan= "2" align= "right" ><input Type= "Submit" value= "can be uploaded simultaneously <?php echo $nb-1;?> files" ></td>   </tr> </table> </td> & lt;/tr> </table> </form> </body> </html>    

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.