Examples of php file upload and details of parameters

  • 2020-12-05 17:07:40
  • OfStack

1. Upload form upload. html

The program code
HTML


<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 a file ">  
</form>  

1. Pay attention to < form enctype="multipart/form-data"...... > This is a tag. To upload a file, we must specify multipart/ ES15en-ES16en, otherwise the server will not know what to do.
2. Note the hidden range of the form option MAX_FILE_SIZE in file upload.html, which limits the size of the uploaded file by setting its Value(value).
3. The value of MAX_FILE_SIZE is just one suggestion for the browser, and it can actually be easily bypassed. So don't put your browser restrictions on this value. In fact, the PHP setting will not invalidate the maximum uploaded file. But it's better to add MAX_FILE_SIZE to the form because it avoids the hassle of waiting for a large file to be uploaded before it turns out to be too large.

Parameters involved in the PHP upload file

The program code
PHP


$f=&$HTTP_POST_FILES['Myfile'];  
$dest_dir='uploads';// Set upload directory   
$dest=$dest_dir.'/'.date("ymd")."_".$f['name'];// Set the file name to date plus the file name to avoid duplication   
$r=move_uploaded_file($f['tmp_name'],$dest);  
chmod($dest, 0755);// Sets the properties of the uploaded file   

or


<?copy($_FILES[MyFile][tmp_name],$_FILES[MyFile][name]);?>

The contents of the $_FILES array in the above example are shown below. Let's assume the file upload field name is userfile (name is optional)

$_FILES['userfile']['name'] the original name of the client machine file.
$_FILES['userfile']['type'] file, which requires the browser to support this information, e.g. "image/gif".
$_FILES['userfile']['size'] the size of the uploaded file, in bytes.
$_FILES['userfile']['tmp_name'] temporary file name saved on server after the file has been uploaded.
$_FILES['userfile']['error'] and the error code associated with this file upload
Values: 0; No error, file upload successful.
Value: 1; The uploaded file exceeds the value restricted by the upload_max_filesize option in ES76en.ini.
Value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
Value: 3; Only part of the file is uploaded.
Value: 4; No files have been uploaded.

The default upload limit of PHP is 2M. If you want to upload files beyond this limit, you need to adjust some parameters of PHP, apache, etc. Below, we briefly introduce 1 of the parameters involved in PHP file upload:

file_uploads
Whether to allow files to be uploaded via HTTP, ON is on by default

upload_tmp_dir
upload_tmp_dir describes the temporary directory where PHP uploads files. To upload files, make sure that the server has not turned off temporary files and has write permission to the folder. If not specified, PHP uses the system default value

upload_max_filesize
The maximum allowed file size is 2M by default

PHP


<?php  
define('MUILTI_FILE_UPLOAD', '10'); // most 10 Three files uploaded at the same time   
define('MAX_SIZE_FILE_UPLOAD',  '500000' ); // File size does not exceed 5MB  
define('FILE_UPLOAD_DIR', 'd:/'); // The directory where the file was uploaded   
// Filenames allowed to be uploaded   
$array_extention_interdite = array( '.php' , '.php3' , '.php4' , '.exe' , '.msi' , '.htaccess' , '.gz' ); // The extension for the upload file   

// A public function that displays information   
function func_message($message='', $ok=''){  
echo '<table width="100%" cellspacing="0" cellpadding="5">';  
if($ok == true){  
echo '<tr bgcolor="#99FF99" ><td width="100"> </td><td class= "text"> '.$message.'</td></tr>' ;  
}  // www.ofstack.com
if($ok == false){  
echo '<tr bgcolor="#FF99CC" ><td width="100"> </td><td class="text"> '.$message.'</td></tr>';  
}  
echo '</table>';     
}  
// Handling 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);     
for($nb = 1 ; $nb <= MUILTI_FILE_UPLOAD ; $nb ++ ){      
if( $_FILES['file_'.$nb]['size'] >= 10 ){   
  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] !=''){  
                        $file_name_final = $_POST['file_name_'.$nb].$extension ;  
                    }else {  
                        $file_name_final = $_FILES['file_'.$nb]['name'] ;  
                    }  
                    // Filename modification   
                    $file_name_final = strtr($file_name_final, 'aaaaaa',      'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');  
                    $file_name_final = preg_replace('/([^.a-z0-1]+)/i', '_', $file_                                    name_final );  

                    $_FILES['file_'.$nb]['name'] = $file_name_final;         
                     move_uploaded_file( $_FILES['file_'.$nb]['tmp_name'] , FILE_UPLOAD_ DIR .                            $file . $file_name_final );  

                    $message_true .= ' Uploaded file  : '.$_FILES['file_'.$nb]['name'] .'<br>';  
                }else{  
                    $message_false .= ' File upload failed  : '.$_FILES['file_'.$nb]['name'] .' <br>';  
                }  
                }else{  
                    $message_false .= ' File size exceeds '.MAX_SIZE_FILE_UPLOAD/1000 . 'KB : "                               '.$_FILES['file_'.$nb]['tmp_name'].'" <br>';}  
                }  
    }//end for  
    break;  
}  
?>  
<html>  
<head>  
<title> Multi-file upload </title>  
<style>  
.border{background-color:#000000}  
.box{background-color:#f8f8f9;}  
.text{  color:#000000;  
font-family:  Song typeface ;  
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'] ; ?>">  
<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, false); }  
?>  
<table width="100%" border="0" cellspacing="5" cellpadding="2" align="center" class="box">  
<?php  
for($nb = 1 ; $nb <= MUILTI_FILE_UPLOAD ; $nb ++ ){   
?>  
<tr class="text">  
<td> Upload file:  <?php echo $nb; ?></td> <td><input type="file" name="file_<?php echo $nb; ?>"></td>  
<td> New file name (with extension) : <?php echo $nb; ?> </td><td><input type="text" name="file_name_<?php echo $nb; ?>"></td>  
</tr>  
<?php } ?>  
<tr>  
<td colspan="2" align="right" class="text"> Upload destination address: <?php echo FILE_UPLOAD_DIR ;?>  
  <select name="file">         
<option value=""></option>  
<?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=" Simultaneous upload <?php echo $nb-1; ?>  A file  "></td>  
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>


Related articles: