Skip to content

Commit

Permalink
Merge pull request #233 from llvdl/gh-195
Browse files Browse the repository at this point in the history
gh-195 added example slim application to handle file uploads
  • Loading branch information
akrabat committed Jun 24, 2017
2 parents fa1a7a1 + fb4b226 commit 4513a37
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docs/cookbook/uploading-files.md
Expand Up @@ -44,3 +44,87 @@ Below is an example HTML form that contains both single and multiple file upload
{% endhighlight %}
<figcaption>Figure 1: Example HTML form for file uploads</figcaption>
</figure>

Uploaded files can be moved to a directory using the `moveTo` method. Below is an example application
that handles the uploaded files of the HTML form above.

<figure>
{% highlight php %}
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\UploadedFile;
$app = new \Slim\App();
$container = $app->getContainer();
$container['upload_directory'] = __DIR__ . '/uploads';

$app->post('/', function(Request $request, Response $response) {
$directory = $this->get('upload_directory');

$uploadedFiles = $request->getUploadedFiles();

// handle single input with single file upload
$uploadedFile = $uploadedFiles['example1'];
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}


// handle multiple inputs with the same key
foreach ($uploadedFiles['example2'] as $uploadedFile) {
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
}

// handle single input with multiple file uploads
foreach ($uploadedFiles['example3'] as $uploadedFile) {
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
}

// handle single input with multiple file uploads
foreach ($uploadedFiles['example3'] as $uploadedFile) {
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
}
});

/**
* Moves the uploaded file to the upload directory and assigns it a unique name
* to avoid overwriting an existing uploaded file.
*
* @param string $directory directory to which the file is moved
* @param UploadedFile $uploaded file uploaded file to move
* @return string filename of moved file
*/
function moveUploadedFile($directory, UploadedFile $uploadedFile)
{
$extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$basename = bin2hex(random_bytes(8)); // see http://php.net/manual/en/function.random-bytes.php
$filename = sprintf('%s.%0.8s', $basename, $extension);

$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);

return $filename;
}

$app->run();
{% endhighlight %}
<figcaption>Figure 2: Example Slim applcation to handle the uploaded files</figcaption>
</figure>

See also
--------
* [https://akrabat.com/psr-7-file-uploads-in-slim-3/](https://akrabat.com/psr-7-file-uploads-in-slim-3/)

0 comments on commit 4513a37

Please sign in to comment.