Skip to content

Commit

Permalink
v2.0.8
Browse files Browse the repository at this point in the history
* Fix that `setWebSafeFileName()` can replace the name until it is empty.
  • Loading branch information
ve3 committed Nov 5, 2020
1 parent 1ac6319 commit 984b144
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Rundiz/Upload/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PHP upload class that is able to validate requirements and limitations, real file's mime type check, detect the errors and report.
*
* @package Upload
* @version 2.0.7
* @version 2.0.8
* @author Vee W.
*
* @property-read array $predefinedErrorMessages Pre-defined error messages.
Expand Down Expand Up @@ -701,6 +701,12 @@ protected function setWebSafeFileName()
$this->new_file_name = preg_replace('#[^\da-z\-_]#iu', '', $this->new_file_name);
// replace multiple dashes to one dash.
$this->new_file_name = preg_replace('#-{2,}#', '-', $this->new_file_name);

// use random name if it becomes empty name.
// @since 2.0.8
if (empty($this->new_file_name)) {
$this->setNewFileNameToRandom();
}
}// setWebSafeFileName


Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ public function testWebSafeFileName()
$this->assertEquals($expect_new_file_name, $Upload->new_file_name);

unset($expect_new_file_name, $the_new_file_name, $Upload);

$Upload = new \Rundiz\Upload\Tests\ExtendedUploadForTest('filename');
$_FILES = array(
'filename' => array(
'name' => 'ชื่อไฟล์ภาษาไทย.jpg',
'type' => 'image/jpeg',
'tmp_name' => __DIR__.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR . 'ชื่อไฟล์ภาษาไทย.jpg',
'error' => 0,
'size' => 51000,
),
);
$Upload->setFilesPropertyForCheck();
$Upload->new_file_name = null;
$Upload->setNewFileName();
$Upload->setWebSafeFileName();
$this->assertNotSame('', $Upload->new_file_name);

unset($Upload);
}// testWebSafeFileName


Expand Down

0 comments on commit 984b144

Please sign in to comment.