Skip to content

Commit

Permalink
Merge pull request #1302 from bangbangda/develop
Browse files Browse the repository at this point in the history
Exception:No Formatter defined for mime type ''
  • Loading branch information
lonnieezell authored Oct 17, 2018
2 parents 890f31f + 7f38d0f commit 51291c9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion system/API/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
21 changes: 21 additions & 0 deletions system/HTTP/Files/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

//--------------------------------------------------------------------
}
32 changes: 32 additions & 0 deletions tests/system/HTTP/Files/FileMovingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 26 additions & 1 deletion user_guide_src/source/libraries/uploaded_files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </libraries/files>` 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:
Expand Down Expand Up @@ -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::

<input type="file" name="userfile" />

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)

0 comments on commit 51291c9

Please sign in to comment.