我需要一个帮助。我正在使用finfo in php
进行文件上传。在这里,我可以验证文件类型,但我需要验证最大文件大小和尺寸。我在下面解释我的代码。
upload.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Upload Form</title>
</head>
<body>
<form action="upload-manager.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="photo" id="fileSelect">
<input type="submit" name="submit" value="Upload">
<p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB and dimension should be 750*250 pixels.</p>
</form>
</body>
</html>
上载manager.php:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$finfo = new finfo(FILEINFO_MIME_TYPE);
$fileContents = file_get_contents($_FILES['photo']['tmp_name']);
$mimeType = $finfo->buffer($fileContents);
$whitelist_type = array('image/jpeg', 'image/png','image/gif');
$error = null;
if (!in_array($mimeType,$whitelist_type)) {
echo 'This is not valid file type';
}
}
?>
这里我只能验证文件类型,需要验证最大文件大小和给定维度。
答案 0 :(得分:1)
要获取尺寸,请使用以下代码
$maxsize = 2097152;
if($_FILES['photo']['size'] >= $maxsize){
echo 'File too large. File must be less than 2 megabytes.';
}
图片高度和宽度的使用以下代码
$image_info = getimagesize($_FILES["photo"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];