我想知道在上传时使我的 3MB 照片变小的最佳方法是什么 我有这个上传表格,但由于某种原因它不会上传超过 2MB 的图像,我总是得到"文件太小或太大"仅限 PHP
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 2000000 * 1234567;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ( isset($_FILES['image']) ) {
if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size) {
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
if (in_array($ext, $valid_exts)) {
if (!file_exists('photos/' )) {
mkdir('photos/' , 0777, true);
}
$path = 'photos/' . uniqid() . '.' . $ext;
$size = getimagesize($_FILES['image']['tmp_name']);
$x = (int) $_POST['x'];
$y = (int) $_POST['y'];
$w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
$h = (int) $_POST['h'] ? $_POST['h'] : $size[1];
$data = file_get_contents($_FILES['image']['tmp_name']);
$vImg = imagecreatefromstring($data);
$dstImg = imagecreatetruecolor($w, $h);
imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $w, $h, $w, $h);
imagejpeg($dstImg, $path);
imagedestroy($dstImg);
/*print_r($path);*/
} else {
echo 'unknown problem!';
}
} else {
echo 'file is too small or large';
}
} else {
echo 'file not set';
}
} else {
echo 'bad request!';
}
$_FILES['image']['error']
是一个整数代码,其中UPLOAD_ERR_OK
是0
。文档
取代:
if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size)
跟:
if ( ($_FILES['image']['error'] == UPLOAD_ERR_OK) && ($_FILES['image']['size'] < $max_file_size) )
如果您不在乎文件的大小,我建议甚至不要费心检查文件的大小。当您已经受到以下限制时,像这样任意设置不可能的最大文件大小是没有意义的:
-
post_max_size
和upload_max_size
在php.ini
. - 可能是您的 HTTP 服务器/CGI 网关的帖子大小组合。[IIS浮现在脑海中]
- 2GB的上传限制是由PHP核心的缺点强加的。[无论您的体系结构如何,都会以有符号的 32 位整数跟踪文件大小。