Limit each image to 1 MB.

UPLOAD_MAX_MB changes the limit, and UPLOAD_MAX_MB=none removes it.
This commit is contained in:
tim
2026-09-26 15:27:51 +02:00
parent c4fcef21aa
commit 504d363642
5 changed files with 86 additions and 5 deletions
+47 -2
View File
@@ -59,8 +59,53 @@ class ValidateDataTest extends TestCase {
}
public function testUploadErrorsAreReported(): void {
$_FILES['upload'] = array('name' => 'a.png', 'tmp_name' => '', 'error' => UPLOAD_ERR_INI_SIZE);
$this->assertSame(array('upload' => 'There was an error uploading the file (1)'), $this->validate(array()));
$_FILES['upload'] = array('name' => 'a.png', 'tmp_name' => '', 'error' => UPLOAD_ERR_PARTIAL);
$this->assertSame(array('upload' => 'There was an error uploading the file (3)'), $this->validate(array()));
}
public function testAFileThatPhpRefusedAsTooLargeIsReported(): void {
foreach (array(UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE) as $error) {
$_FILES['upload'] = array('name' => 'big.png', 'tmp_name' => '', 'error' => $error);
$this->assertSame(array('upload' => 'big.png is larger than 1 MB. Please choose a smaller image.'), $this->validate(array()));
}
}
// An image of $bytes bytes: a PNG with padding after its end
private function imageOf(int $bytes): void {
$file = $this->tmp . '/upload';
file_put_contents($file, str_pad(self::png(), $bytes, "\0"));
$_FILES['upload'] = array('name' => '<b>big</b>.png', 'tmp_name' => $file, 'error' => UPLOAD_ERR_OK);
}
public function testImagesCanBeOneMegabyte(): void {
$this->imageOf(1024 * 1024);
$this->assertSame(array(), $this->validate(array()));
$this->imageOf(1024 * 1024 + 1);
$this->assertSame(array('upload' => '&lt;b&gt;big&lt;/b&gt;.png is larger than 1 MB. Please choose a smaller image.'), $this->validate(array()));
}
public function testTheImageLimitComesFromTheConfiguration(): void {
$this->setEnv('UPLOAD_MAX_MB', '2.5');
$this->imageOf(2 * 1024 * 1024);
$this->assertSame(array(), $this->validate(array()));
$this->imageOf((int)(2.5 * 1024 * 1024) + 1);
$this->assertSame(array('upload' => '&lt;b&gt;big&lt;/b&gt;.png is larger than 2.5 MB. Please choose a smaller image.'), $this->validate(array()));
}
public function testNoneMeansNoImageLimit(): void {
$this->setEnv('UPLOAD_MAX_MB', 'None');
$this->assertNull(upload_max_bytes());
$this->imageOf(3 * 1024 * 1024);
$this->assertSame(array(), $this->validate(array()));
$_FILES['upload'] = array('name' => 'big.png', 'tmp_name' => '', 'error' => UPLOAD_ERR_INI_SIZE);
$this->assertSame(array('upload' => 'big.png is larger than the server accepts. Please choose a smaller image.'), $this->validate(array()));
}
public function testABadImageLimitMeansTheDefault(): void {
foreach (array('', 'lots', '0', '-2') as $value) {
$this->setEnv('UPLOAD_MAX_MB', $value);
$this->assertSame(1024 * 1024, upload_max_bytes(), $value);
}
}
public function testNoFileIsNotAnError(): void {