[英]Validate multi-select fieid in php
我有一個帶有多選文件字段的表單。現在我想檢查是否在單擊提交按鈕時選擇了文件。當前我正在使用以下代碼,
PHP代碼:-
<input type="file" id="files" name="files[]" multiple="multiple" />
<?php
if(isset($_POST['submit']))
{
if((isset($_FILES['files'])) && ($_FILES['files']['size'] > 0))
{
echo "Files is seleced";
}
else
{
echo "Please select file";
}
}
?>
但是在單擊提交按鈕后,如果沒有選擇圖像,那么它還會顯示File is selected meassage.so,請提供一些解決方案。
$_FILES['files']
是一個multidimensional
數組
所以你必須檢查你的狀況
$sizes = $_FILES['files']['size'];
$sizes = array_filter($sizes); //remove all 0 values
if((isset($_FILES['files'])) && (count($sizes) > 0))
例如:
Array
(
[name] => Array
(
[0] => chtbg.jpg
[1] => chtlg.jpg
[2] => ddsports_bg.jpg
[3] => ddsports_logo.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
[3] => image/jpeg
)
[tmp_name] => Array
(
[0] => E:\xampp\tmp\php849B.tmp
[1] => E:\xampp\tmp\php84AC.tmp
[2] => E:\xampp\tmp\php84BC.tmp
[3] => E:\xampp\tmp\php84CD.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[size] => Array
(
[0] => 50891
[1] => 40805
[2] => 16985
[3] => 1800
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.