Home >Backend Development >PHP Tutorial >PHP implements multi-file upload program code_PHP tutorial
There is not much difference between PHP file upload and multi-file upload. For multi-file upload, we just change the form name into an array form, and use foreach traversal to achieve multi-file upload. For dynamic multi-file upload, just add a dynamic increase in js. For the multi-file upload box, just traverse the array when processing it in PHP.
The simplest example is as follows
The code is as follows | Copy code | ||||
if ($ error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][ $key ]; > |
Share examples from other friends below
Example 1
代码如下 | 复制代码 |
|
If we want dynamic and uncertain multi-file upload, how can we implement it? There are also examples below
File upload code
代码如下 | 复制代码 |
view plaincopy to clipboardprint? Submit File code view plaincopy to clipboardprint? if ($_POST["submitfile"]!="") { $Path="./".date('Ym')."/"; if (!is_dir($Path))//Create path { mkdir($Path); } echo " "; for ($i=0;$i< ;count($filelist);$i++) { //The order of $_FILES["filelist"]["size"][$i] cannot be changed because fileist is a two-dimensional array if ($_FILES["filelist"]["size"][$i]!=0) { $File=$Path.date('Ymdhm')."_".$_FILES["filelist "]["name"][$i]; if (move_uploaded_file($_FILES["filelist"]["tmp_name"][$i],$File)) { echo "File uploaded successfully file Type: ".$_FILES["filelist"]["type"][$i]." "."File name:" .$_FILES["filelist"]["name"][$i]. " "; } else { echo "File name:".$_FILES["filelist"]["name"][$i]."Upload failed"; } } } echo " return} ?> |
Another: Error message description
From Starting with PHP 4.2.0, PHP will return a corresponding error code along with the file information array. This code can be found in the error field in the file array generated when the file is uploaded, that is, $_FILES['userfile']['error'].
UPLOAD_ERR_OK
The value is 0, no error occurred, and the file was uploaded successfully.
UPLOAD_ERR_INI_SIZE
The value is 1, and the uploaded file exceeds the value limited by the upload_max_filesize option in php.ini.
UPLOAD_ERR_FORM_SIZE
With a value of 2, the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. UPLOAD_ERR_PARTIAL
The value is 3, the file is only partially uploaded.
UPLOAD_ERR_NO_FILE
The value is 4, no file was uploaded.
UPLOAD_ERR_NO_TMP_DIR
The value is 6 and the temporary folder cannot be found. Introduced in PHP 4.3.10 and PHP 5.0.3.
UPLOAD_ERR_CANT_WRITE
The value is 7, file writing failed. Introduced in PHP 5.1.0.
Note: The above values became PHP constants after PHP 4.3.0.