我想限制可以上传到远程服务器的图像的文件大小。我编写了以下代码,它允许我成功上传在我的图库中正常工作的文件。
但是,我想将文件大小限制为大约2 MB。此代码适用于上载文件,但不限制文件大小。此外,我认为PHP具有内置的最大上传文件大小。使用此代码,我甚至可以上传一个31.4MB .mp4文件。这是代码:
<?php
$max = 2000 * 1024;
$message = '';
if (isset($_POST['upload'])) {
$destination = __DIR__ . '/_images/';
if ($_FILES['filename']['error'] == 0) {
$result = move_uploaded_file($_FILES['filename']['tmp_name'], $destination
. $_FILES['filename']['name']);
if ($result) {
$message = $_FILES['filename']['name'] ;
} else {
$message = 'Sorry, there was a problem uploading '
.$_FILES['filename']['name'];
}
} else {
switch ($_FILES['filename']['error']) {
case 2:
$message = $_FILES['filename']['name'] . ' is too big to upload.';
break;
case 4:
$message = 'No file selected.';
break;
default:
$message = 'Sorry, there was a problem uploading '
.$_FILES['filename']['name'];
break;
}
}
}
?>