ThinkPHP5.0多文件上传临时文件丢失问题的解决方案
以下是修改前的原始代码实现逻辑。
if(!empty($_FILES)){
if(!empty($_FILES['org_positive'])){
$org_positive = request()->file('org_positive');
if($org_positive){
$info = $org_positive->move(ROOT_PATH . 'uploads');
$positive_path="/uploads/".$info->getSaveName();
}else{
$positive_path="";
}
}
if(!empty($_FILES['org_reverse'])){
$org_reverse = request()->file('org_reverse');
if($org_reverse){
$info1=$org_reverse->move(ROOT_PATH . 'uploads');
$reverse_path="/uploads/".$info1->getSaveName();
}else{
$reverse_path="";
}
}
if(!empty($_FILES['org_license'])){
$org_license = request()->file('org_license');
if($org_license){
$info2=$org_license->move(ROOT_PATH . 'uploads');
$license_path="/uploads/".$info2->getSaveName();
}else{
$license_path="";
}
}
}
当程序尝试处理$_FILES数组中的第二个文件元素时,系统抛出了异常报错。
经过调整优化后的新代码结构如下所示。
if(!empty($_FILES)){
if(!empty($_FILES['org_positive'])){
$org_positive = request()->file('org_positive');
}
if(!empty($_FILES['org_reverse'])){
$org_reverse = request()->file('org_reverse');
}
if(!empty($_FILES['org_license'])){
$org_license = request()->file('org_license');
}
if($org_positive){
$info = $org_positive->move(ROOT_PATH . 'uploads');
$positive_path="/uploads/".$info->getSaveName();
}else{
$positive_path="";
}
if($org_reverse){
$info1=$org_reverse->move(ROOT_PATH . 'uploads');
$reverse_path="/uploads/".$info1->getSaveName();
}else{
$reverse_path="";
}
if($org_license){
$info2=$org_license->move(ROOT_PATH . 'uploads');
$license_path="/uploads/".$info2->getSaveName();
}else{
$license_path="";
}
}
作为初学者,查阅资料得知PHP在文件上传后短时间内会自动删除临时文件,即$_FILES[‘file'][‘tmp_name']所指向的路径。修正代码后不再报错,成功将临时文件迁移至目标位置。
要点回顾
上述内容分享了ThinkPHP5.0多文件上传场景下解决临时文件缺失的方法,希望能帮助到大家。如有任何疑问欢迎留言,小编会尽快回复。感谢支持本站!