


PHP file upload examples and detailed explanation of parameters_PHP tutorial
Jul 13, 2016 pm 05:18 PM
1. Upload form upload.html
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 file">
</form>
1. Note <form enctype="multipart/form-data"...> This is a tag. If we want to upload files, we must specify multipart/form-data, otherwise the server will Don't know what to do.
2. It is worth noting that the hidden value field of the form option MAX_FILE_SIZE in the file upload.html can limit the size of the uploaded file by setting its Value.
3. The value of MAX_FILE_SIZE is just a suggestion for browsers. In fact, it can be easily bypassed. So don't rely on this value to limit browser restrictions. In fact, the maximum upload file size in PHP settings will not be invalid. But it's better to include MAX_FILE_SIZE in the form, as it saves users the trouble of spending time waiting for a large file to be uploaded only to find out that the file is too big.
Parameters involved in PHP uploading files
Program code
PHP
$f=&$HTTP_POST_FILES['Myfile '];
$dest_dir='uploads';//Set the upload directory
$dest=$dest_dir.'/'.date("ymd")."_".$f['name'] ;//Set the file name to the date plus the file name to avoid duplication
$r=move_uploaded_file($f['tmp_name'],$dest);
chmod($dest, 0755);//Set upload Attributes of the file
or
<?copy($_FILES[MyFile][tmp_name], $_FILES[MyFile][name]);?>
The contents of the $_FILES array in the above example are as follows. We assume that the name of the file upload field is userfile (the name can be whatever you want)
$_FILES['userfile']['name'] The original name of the client machine file.
$_FILES['userfile']['type'] The MIME type of the file, which requires browser support for this information, such as "image/gif".
$_FILES['userfile']['size'] The size of the uploaded file, in bytes.
$_FILES['userfile']['tmp_name'] The temporary file name stored on the server after the file is uploaded.
$_FILES['userfile']['error'] Error code related to the file upload
Value: 0; No error occurs, the file upload is successful.
Value: 1; The uploaded file exceeds the value limited by the upload_max_filesize option in php.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 was uploaded.
Value: 4; No files were uploaded.
The default upload limit of PHP is a maximum of 2M. If you want to upload files exceeding this setting, you need to adjust some parameters of PHP, apache, etc. Below, we briefly introduce some parameters involved in PHP file upload:
file_uploads
Whether to allow file uploading via HTTP switch, the default is ON
upload_tmp_dir
upload_tmp_dir is used to describe the temporary directory where files uploaded by PHP are placed. If you want to upload files, you must ensure that the server has not closed the temporary files and has write permissions on the folder. If not specified, PHP will use the system default value.
upload_max_filesize
The maximum allowed file size for uploading, the default is 2M
PHP
<?php
define('MUILTI_FILE_UPLOAD', '10'); //Up to 10 files can be uploaded at the same time
define('MAX_SIZE_FILE_UPLOAD', '500000' ); //File size No more than 5MB
define('FILE_UPLOAD_DIR', 'd:/'); //Directory for uploaded files
//File names allowed to be uploaded
$array_extention_interdite = array( '.php' , '. php3' , '.php4' , '.exe' , '.msi' , '.htaccess' , '.gz' ); //Extension of uploaded file
//Public function to display 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.jb51.net
if($ok == false){
echo '<tr bgcolor= "#FF99CC" ><td width="100"> </td><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' : $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]. on; <}>} else {
> $file_name_final = strtr($file_name_final, 'aaaaaa' , name_final );
$_FILES[ 'file_'.$nb]['name'] = $file_name_final; $file . $file_name_final );
$message_true .= 'File has been uploaded: '.$_FILES['file_'.$nb]['name'] .'<br>'; $message_false .= 'File upload failed: '.$_FILES['file_'.$nb]['name'] .' <br>';
}
}else{
$message_false .= '文件尺寸超过'.MAX_SIZE_FILE_UPLOAD/1000 . 'KB : " '.$_FILES['file_'.$nb]['tmp_name'].'" <br>';}
}
}//end for
break;
}
?>
<html>
<head>
<title>多文件上传</title>
<style>
.border{background-color:#000000}
.box{background-color:#f8f8f9;}
.text{ color:#000000;
font-family: 宋体;
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>上传文件: <?php echo $nb; ?></td> <td><input type="file" name="file_<?php echo $nb; ?>"></td>
<td>新文件名(包括扩展名):<?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">上传目的地址:<?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="可同时上传<?php echo $nb-1; ?> 个文件 "></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian
