我正在使用 getimagesize 来验证图像类型,但不知道如何为多个文件编写脚本。基本上,我有一个允许上传多个图像文件的表单,如下所示。
<input type="file" name="photo[]" class="file" />
<input type="file" name="photo[]" class="file" />
<input type="file" name="photo[]" class="file" />
然后我用它来验证它并通过 phpmailer 发送。
<?php
ob_start();
require("class.phpmailer.php");
$errors = array();
if ('POST' === $_SERVER['REQUEST_METHOD'])
{
$firstname = sanitize($_POST['firstname']);
$lastname = sanitize($_POST['lastname']);
$email = sanitize($_POST['email']);
if (empty($firstname))
{
$errors['firstname'] = "Please provide first name.";
}
if (empty($lastname))
{
$errors['lastname'] = "Please provide last name.";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors['email'] = "Please provide a valid email address.";
}
if (count($errors) === 0)
{
$imageinfo = array();
$my_files = $_FILES['photo']['tmp_name'];
foreach($my_files as $single_file) {
if(!empty($single_file)) {
$imageinfo[$single_file] = getimagesize($single_file);
if ($single_file['mime'] != 'image/png' && $single_file['mime'] != 'image/jpeg')
{ echo "Invalid Image File";
exit();
} }
}
foreach($_FILES['photo']['tmp_name'] as $photo)
if(!empty($photo)) {
$mail->AddAttachment($photo);
$message = 'some message';
$mail = new PHPMailer();
$mail->SetFrom($email);
$mail->AddAddress($from);
$mail->Subject = "Submitted";
$mail->Body = $message;
$mail->WordWrap = 50;
}
$mail->Send();
header("Location: thankyou.php");
exit();
}}
function sanitize($value)
{
return trim(strip_tags($value, $problem=''));
}
?>
我收到警告的错误消息:getimagesize() 期望参数 1 是字符串,数组给定 我知道这是因为我的表单正在传递一个数组。如何更改脚本以适用于多个文件/数组?请帮忙。谢谢。