我有一个上传表单,只能上传六张JPG和PNG格式的图片。
问题是:每当我选择两个GIF图像和四个JPG图像时,它都会上传JPG并拒绝GIF。如何对此进行验证,如果只有一个GIF图像,则会出现错误消息,而不应上传其他五个JPG?
以下是完整代码:
<?php
if (empty($_FILES['Upload_Property_Images']['name']))
{
echo 'You have\'nt Entered Value for upload field';
}
else
{
foreach($_FILES['Upload_Property_Images']['name'] as $file)
{
// Allowed file types
$whitelist = array("jpg","png");
$temp = explode('.', $file);
// Match uploaded file extension
if (in_array(end($temp), $whitelist ))
{
// Count total uploads
if (count($_FILES['Upload_Property_Images']['name'])==6)
{
// Code for uploading here
echo 'Image <b>' .$file.'</b> uploaded successfully<br>';
}
else
{
// Count error
echo 'You\'r Only Allowed Six Images';
exit();
}
}
else
{
// File extension error
echo 'Image <b>'.$file.'</b> Must be JPG OR PNG<br>';
}
}
}
?>
答案 0 :(得分:0)
$errors = array();
if (empty($_FILES['Upload_Property_Images']['name'][0])) {
$errors[] = 'You have\'nt Entered Value for upload field';
}
else {
if (count($_FILES) > 6)
$errors[] = "Your Only Allowed Six Images";
$whitelist = array("jpg","png");
foreach($_FILES['Upload_Property_Images']['name'] as $file) {
if ($file) {
$temp = explode('.', $file);
if (!in_array(end($temp), $whitelist )) {
$errors[] = "All images must be JPG and PNG";
break;
}
}
}
if (empty($errors)) {
foreach ($_FILES['Upload_Property_Images']['name'] as $file) {
if ($file) {
// Code for uploading here
// ...
echo 'Image <b>' .$file.'</b> uploaded successfully<br>';
}
}
}
}
if (!empty($errors))
foreach ($errors as $e)
echo $e."<br/>";