PHP文件上传功能 -- 多文件上传
这一节内容主要介绍PHP上传文件的多文件上传功能。
只要以数组形式来命名表单中的文件上传标记,即可实现多个文件同时上传。
下面我们来看一个例子:
---------------------------------------------------------------------
function upload($file_error, $file_tmp_name, $file_name){
$info = "";
if($file_name == "")
return $info;
switch($file_error){
case UPLOAD_ERR_INI_SIZE:
$info = $file_name. ": 文件大小超过了服务器的限制";
break;
case UPLOAD_ERR_FORM_SIZE:
$info = $file_name. ": 文件大小超过了浏览器的限制";
break;
case UPLOAD_ERR_PARTIAL:
$info = $file_name. ": 只上传了部分文件";
break;
case UPLOAD_ERR_NO_FILE:
$info = $file_name. ": 没有文件被上传";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$info = $file_name. ": 找不到临时文件夹";
break;
case UPLOAD_ERR_CANT_WRITE:
$info = $file_name. ": 文件写入失败";
break;
case UPLOAD_ERR_OK:
$upload_dir = './'.iconv("UTF-8","gb2312",$file_name);
if(file_exists($upload_dir)){
$info = $file_name.": 同名文件已经存在";
}else{
if(move_uploaded_file($file_tmp_name,$upload_dir)){
$info = $file_name.": 文件上传成功";
}else{
$info = $file_name.": 文件上传失败";
}
}
break;
}
return $info;
}
if(isset($_POST['submit'])){
$info = '';
$count = count($_FILES['upload_file']['name']);
for($i=0; $i if($_FILES['upload_file']['name'][$i] == "")
continue;
$info = upload(
$_FILES['upload_file']['error'][$i],
$_FILES['upload_file']['tmp_name'][$i],
$_FILES['upload_file']['name'][$i]
);
}
echo $info;
}
?>
--------------------------------------------------------------------------------------
代码执行结果如下:

注意:
1、中, name="upload_file[]"一定要以数组形式命名,不然就会出现错误: “Uninitialized string offset: 0”,这句话的意思是你的数组key值越界了
2、$_FILES['upload_file']['name'][$i]中, upload_file是表单中上传文件标记符的名称,多文件上传时,数组$_FILES的第三维下标会自动从0开始依次编号。

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
