使用php多个上传脚本,我如何检查数组是否为空?
不工作:
-
if(empty($_FILES['images']['name']))
-
if($_FILES['images']['name'] === 'Array')
- 使用for循环作为第一次检查似乎很脏。然后在第一个for循环中有一个嵌套的if语句。然后我在里面有另一个for循环用于额外的错误检查。
最好的方法是什么?这不会检查数组是否为空。第一次检查只检查count($images)是否> 5。
// Begin image validation
if(isset($name)) { // here I want to see if image array is empty
// 5 max images allowed
if(count($name) > 5) {
?>
<script type="text/javascript">
document.getElementById('message').innerHTML += 'Max allowed images is 5';
</script>
<?php
} else {
// Begin for loop
统计成功上传的文件数和尝试上传的文件数:
<?php
$successfully_uploaded_files = 0;
$attempted_files = 0;
foreach ($_FILES as $file) {
if ($file['error'] === 0) {
$successfully_uploaded_files++;
$attempted_files++;
} elseif ($file['error'] !== 4) {
$attempted_files++;
}
}
您应该适当地处理其他错误代码:http://www.php.net/manual/en/features.file-upload.errors.php