Fix #17875: Revert move_uploaded_file() function instead of copy() and unlink() for saving uploaded files when POST request

This commit is contained in:
Yusup Hambali
2020-02-20 09:22:01 +00:00
committed by GitHub
parent 7ec7fd11ee
commit 55793471ea
4 changed files with 16 additions and 55 deletions

View File

@ -11,6 +11,8 @@ Yii Framework 2 Change Log
- Bug #17829: `yii\helpers\ArrayHelper::filter` now correctly filters data when passing a filter with more than 2 levels (rhertogh)
- Enh #7622: Allow `yii\data\ArrayDataProvider` to control the sort flags for `sortModels` through `yii\data\Sort::sortFlags` property (askobara)
- Bug #17863: `\yii\helpers\BaseInflector::slug()` doesn't work with an empty string as a replacement argument (haruatari)
- Bug #17875: Revert `move_uploaded_file()` function instead of `copy()` and `unlink()` for saving uploaded files when POST request (sup-ham)
2.0.32 January 21, 2020
-----------------------

View File

@ -178,10 +178,14 @@ class UploadedFile extends BaseObject
if ($this->hasError) {
return false;
}
if (false === $this->copyTempFile(Yii::getAlias($file))) {
return false;
$targetFile = Yii::getAlias($file);
if (is_resource($this->_tempResource)) {
$result = $this->copyTempFile($targetFile);
return $deleteTempFile ? @fclose($this->_tempResource) : (bool) $result;
}
return !$deleteTempFile || $this->deleteTempFile();
return $deleteTempFile ? move_uploaded_file($this->tempName, $targetFile) : copy($this->tempName, $targetFile);
}
/**
@ -193,9 +197,6 @@ class UploadedFile extends BaseObject
*/
protected function copyTempFile($targetFile)
{
if (!is_resource($this->_tempResource)) {
return $this->isUploadedFile($this->tempName) && copy($this->tempName, $targetFile);
}
$target = fopen($targetFile, 'wb');
if ($target === false) {
return false;
@ -207,32 +208,6 @@ class UploadedFile extends BaseObject
return $result;
}
/**
* Delete temporary file
*
* @return bool if file was deleted
* @since 2.0.32
*/
protected function deleteTempFile()
{
if (is_resource($this->_tempResource)) {
return @fclose($this->_tempResource);
}
return $this->isUploadedFile($this->tempName) && @unlink($this->tempName);
}
/**
* Check if file is uploaded file
*
* @param string $file path to the file to check
* @return bool
* @since 2.0.32
*/
protected function isUploadedFile($file)
{
return is_uploaded_file($file);
}
/**
* @return string original file base name
*/

View File

@ -89,12 +89,11 @@ class UploadedFileTest extends TestCase
public function testSaveAs()
{
$tmpImage = UploadedFileMock::getInstance(new ModelStub(), 'temp_image');
$tmpImage = UploadedFile::getInstance(new ModelStub(), 'temp_image');
$targetFile = '@runtime/test_saved_uploaded_file_' . time();
$this->assertEquals(true, $tmpImage->saveAs($targetFile, $deleteTempFile = false));
$this->assertEquals(true, $tmpImage->saveAs($targetFile, $deleteTempFile = true));
$this->assertEquals(false, $tmpImage->saveAs($targetFile)); // temp file should not exist
$this->markTestIncomplete("`$deleteTempFile` flag simply uses php's move_uploaded_file() method, so this not work in test");
@unlink($targetFile);
}
@ -110,7 +109,11 @@ class UploadedFileTest extends TestCase
$_FILES['ModelStub'] = $_FILES['Item']; // $_FILES[Item] here from testParse() above
$tmpFile = UploadedFile::getInstance($model, 'file');
$this->assertEquals(true, $tmpFile->saveAs($targetFile));
$this->assertEquals($tmpFile->saveAs($targetFile, $deleteTempFile = false), true);
$this->assertEquals($tmpFile->saveAs($targetFile), true);
$this->assertEquals($tmpFile->saveAs($targetFile), false); // has deleted before
@unlink($targetFile);
}
}

View File

@ -1,19 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\web\mocks;
class UploadedFileMock extends \yii\web\UploadedFile
{
/**
* @inheritDoc
*/
protected function isUploadedFile($file)
{
return is_file($file); // is_uploaded_file() won't work in test
}
}