我想count
个已上传的文件,但如果没有上传文件,我就无法收到错误消息。这是我的参考代码:
html
<input type="file" name="file[]" class="filestyle" data-buttonText="Quotation 1" multiple="multiple">
PHP
$total = count($_FILES['file']['name']);
if($total > '2'){
for($i=0; $i<$total; $i++){
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
if($tmpFilePath != ""){
$shortname = $_FILES['file']['name'][$i];
$filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['file']['name'][$i];
if(!$msgError && move_uploaded_file($tmpFilePath, $filePath)){
// insert to db and success msg
}
}
} elseif($total < '4') {
$msgError[] = "Need to upload 3 Quotations";
}
if(isset($msgError)){
$msgErrorString = implode(",",$msgError);
header("Location: pr_form.php?msgError=".$msgErrorString."");
}
如果用户上传的文件少于3个,则不会显示错误。我有其他用户输入验证。除文件验证外,一切正常。我可以知道为什么吗?
答案 0 :(得分:1)
首先:删除'
$total > '2'
应为$total > 2
与total < 4
第二
应该是
count($_FILES)
不是count($_FILES['file']['name'])
所以,在你的问题中..
如果没有上传文件。
$total = count($_FILES);
if($total > 2){
for($i=0; $i<$total; $i++){
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
if($tmpFilePath != ""){
$shortname = $_FILES['file']['name'][$i];
$filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['file']['name'][$i];
if(!$msgError && move_uploaded_file($tmpFilePath, $filePath)){
// insert to db and success msg
}
}
} elseif($total < 4 && $total > 0) {
$msgError = "Need to upload 3 Quotations";
}
elseif($total === 0){ //This condition
$msgError = "No chosen file.";
}
if(isset($msgError)){
header("Location: pr_form.php?msgError=".$msgError."");
}
然后在你的pr_form.php中,你应该有类似这样的东西..
<?php
echo $_GET['msgError'];
?>