From f6a1060405262dff40f49de79c9fd7040bf7c7e1 Mon Sep 17 00:00:00 2001 From: HaoLiang Date: Tue, 9 Oct 2018 11:36:06 +0800 Subject: [PATCH 1/2] If HTTP header Accept is not set, will return the first supported format. --- system/API/ResponseTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 426e41a76e34..85c7e0821b7f 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -346,7 +346,7 @@ protected function format($data = null) // Determine correct response type through content negotiation $config = new Format(); - $format = $this->request->negotiate('media', $config->supportedResponseFormats, true); + $format = $this->request->negotiate('media', $config->supportedResponseFormats, false); $this->response->setContentType($format); From 7f38d0fd51241cc408f1fb8d36cc056e87d1e923 Mon Sep 17 00:00:00 2001 From: HaoLiang Date: Mon, 15 Oct 2018 08:25:33 +0800 Subject: [PATCH 2/2] Save the uploaded file to a new location. --- system/HTTP/Files/UploadedFile.php | 21 ++++++++++++ tests/system/HTTP/Files/FileMovingTest.php | 32 +++++++++++++++++++ .../source/libraries/uploaded_files.rst | 27 +++++++++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/system/HTTP/Files/UploadedFile.php b/system/HTTP/Files/UploadedFile.php index aad3e5b2cbc3..1e309616fd10 100644 --- a/system/HTTP/Files/UploadedFile.php +++ b/system/HTTP/Files/UploadedFile.php @@ -373,5 +373,26 @@ public function isValid(): bool return is_uploaded_file($this->path) && $this->error === UPLOAD_ERR_OK; } + /** + * Save the uploaded file to a new location. + * + * By default.upload files are saved in writable/uploads directory. the YYYYMMDD folder + * and random file name will be created. + * + * @param string $folderName the folder name to writable/uploads directory. + * @param string $fileName the name to rename the file to. + * @return string file full path + */ + public function store($folderName = null, $fileName = null) : string + { + $folderName = $folderName ?? date('Ymd'); + $fileName = $fileName ?? $this->getRandomName(); + + // Move the uploaded file to a new location. + if ($this->move(WRITEPATH . 'uploads/' . $folderName, $fileName)) { + return $folderName . DIRECTORY_SEPARATOR . $this->name; + } + } + //-------------------------------------------------------------------- } diff --git a/tests/system/HTTP/Files/FileMovingTest.php b/tests/system/HTTP/Files/FileMovingTest.php index be9cb89dcfa9..fb20eae0dd0f 100644 --- a/tests/system/HTTP/Files/FileMovingTest.php +++ b/tests/system/HTTP/Files/FileMovingTest.php @@ -160,6 +160,38 @@ public function testMoved() //-------------------------------------------------------------------- + public function testStore() + { + $finalFilename = 'fileA'; + + $_FILES = [ + 'userfile1' => [ + 'name' => $finalFilename . '.txt', + 'type' => 'text/plain', + 'size' => 124, + 'tmp_name' => '/tmp/fileA.txt', + 'error' => 0 + ], + ]; + + $collection = new FileCollection(); + + $this->assertTrue($collection->hasFile('userfile1')); + + $destination = 'destination/'; + + // Create the destination if not exists + is_dir($destination) || mkdir($destination, 0777, true); + + $file = $collection->getFile('userfile1'); + + $this->assertInstanceOf(UploadedFile::class, $file); + $path = $file->store($destination, $file->getName(), false); + $this->assertEquals($destination . '/fileA.txt', $path); + } + + //-------------------------------------------------------------------- + public function testAlreadyMoved() { $finalFilename = 'fileA'; diff --git a/user_guide_src/source/libraries/uploaded_files.rst b/user_guide_src/source/libraries/uploaded_files.rst index 9edfcf048edf..bd37a84c6ee5 100644 --- a/user_guide_src/source/libraries/uploaded_files.rst +++ b/user_guide_src/source/libraries/uploaded_files.rst @@ -6,7 +6,7 @@ CodeIgniter makes working with files uploaded through a form much simpler and mo array directly. This extends the :doc:`File class ` and thus gains all of the features of that class. .. note:: This is not the same as the File Uploading class in previous versions of CodeIgniter. This provides a raw - interface to the uploaded files with a few small features. + interface to the uploaded files with a few small features. .. contents:: :local: @@ -235,3 +235,28 @@ Moving an uploaded file can fail, with an HTTPException, under several circumsta - the file has already been moved - the file did not upload successfully - the file move operation fails (eg. improper permissions) + +Store Files +------------ + +Each file can be moved to its new location with the aptly named ``store()`` method. + +With the simplest usage, a single file might be submitted like:: + + + +By default, Upload files are saved in writable/uploads directory. the YYYYMMDD folder +and random file name will be created. return a file path:: + + $path = $this->request->getFile('userfile')->store(); + +You can specify directory to movethe file to as the first parameter.a new filename by +passing it as thesecond parameter:: + + $path = $this->request->getFile('userfile')->store('head_img/', 'user_name.jpg'); + +Moving an uploaded file can fail, with an HTTPException, under several circumstances: + +- the file has already been moved +- the file did not upload successfully +- the file move operation fails (eg. improper permissions)