我的页面上有多种文件类型。我的问题是,如果我从一开始就开始上传文件或图像,那么它就可以正常工作。
如果我随机上传文件,则会出现错误。
这是完整的代码。
HTML
<div class="container">
<form action="process.php" method="post" name="multiplefiles" enctype="multipart/form-data" id="myprofile">
<div class="input_fields_wrap">
<?php
$labelname = array('Label 1','Label 2','Label 3','Label 4','Label 5','Label 6','Label 7');
$labelCount=count($labelname);
?>
<div class="row mt-3">
<?php
for ($i=0; $i < $labelCount; $i++) {
?>
<div class="col-lg-4 mb-4 ">
<div class="documentUploadWrap">
<label><?php echo $labelname[$i];?></label>
<div class="upload_doc">
<input type="hidden" name="docUploadLabel[]" value="<?php echo $labelname[$i];?>">
<input type="file" name="docUpload[]" class="fileupload">
<input type="hidden" name="docUpload[]" value="">
<div class="uploadInfo">
<div class="upload_icon"></div>
<p>Drop your image here, or <span>browse</span></p>
<span>Supports: JPEG, PNG, PPT, PPTX, PDF <br />Max Size: 5MB</span>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<input type="submit" name="send" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
过程
if (isset($_POST['send'])) {
$total = count(array_filter($_FILES['docUpload']['name']));
for($i=0; $i < $total; $i++) {
if(isset($_FILES['docUpload']['name'][$i]) && $_FILES['docUpload']['name'][$i] != "")
{
$foldername="uploads";
$uploadLabel=$_POST['docUploadLabel'][$i];
$image1 = $_FILES['docUpload']['name'][$i];
$filename = basename($image1);
$onlyfile = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$file = mt_rand();// random number
$newname = $onlyfile.'_'.$file.'.'.$extension;
echo $location='images/'.$foldername.'/'.$newname;
if($extension=='png' || $extension=='jpg' || $extension=='jpeg') {
compressImage($_FILES['docUpload']['tmp_name'][$i],$location,60);
}
else{
move_uploaded_file($_FILES['docUpload']['tmp_name'][$i], $location);
}
$uploadDoc=$newname;
}
else{
if (isset($_POST['docUpload'][$i]) && $_POST['docUpload'][$i]!= "") {
$uploadLabel=$_POST['docUploadLabel'][$i];
$uploadDoc=$_POST['docUpload'][$i];
}
}
$dataupload=array(
'upload_label'=>$uploadLabel,
'upload_name'=>$uploadDoc,
);
echo "<pre>";
print_r($dataupload);
}
}
echo"<pre>"; var_dump($_FILES);
输出。我上传了标签 4 和标签 6 中的图片
答案 0 :(得分:0)
发生这种情况是因为如果条件相同,则存在 double if,使 $uploadname 和 $uploadlable 变为未定义
<?php
$filedemo=array('docUpload'=>array(
array('label'=>'Label1','file'=>null),
array('label'=>'Label2','file'=>'file2'),
array('label'=>'Label3','file'=>null),
array('label'=>'Label4','file'=>'file4'),
array('label'=>'Label5','file'=>null),
array('label'=>'Label6','file'=>null),
array('label'=>'Label7','file'=>null),
)
);
$total = count($filedemo['docUpload']);
for($i=0; $i < $total; $i++) {
if(isset($filedemo['docUpload'][$i]['file']) && !empty($filedemo['docUpload'][$i]['file']))
{
$uploadLabel=$filedemo['docUpload'][$i]['label'];
$uploadDoc=$filedemo['docUpload'][$i]['file'];
}
else{
$uploadLabel=$filedemo['docUpload'][$i]['label'];
$uploadDoc=$filedemo['docUpload'][$i]['file'];
}
$dataupload=array(
'upload_label'=>$uploadLabel,
'upload_name'=>$uploadDoc,
);
echo "<pre>";
print_r($dataupload);
}
答案 1 :(得分:0)
array_filter()
在删除空值后不会重新索引数组的简单演示:
$array = ["1", "", "3", "", "4"];
var_export(array_filter($array));
输出:
array (
0 => '1',
2 => '3',
4 => '4',
)
所以,您看,如果您使用 count()
和 for()
(我不会)进行迭代,那么 PHP 会抱怨您正在尝试访问 {{1} 处不存在的数据}.
相反,忘记使用 $i == 1
- 无论如何它都有过度过滤的坏习惯。
只需用 array_filter()
遍历整个数组并检查名称字符串是否有长度。
foreach()