Paperyard
Upload.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
11 class Upload
12 {
14  protected $logger;
15 
16  public function __construct(LoggerInterface $logger)
17  {
18  $this->logger = $logger;
19  }
20 
21  public function __invoke(Request $request, Response $response, $args)
22  {
23  $directory = '/data/scan';
24 
25  $uploadedFiles = $request->getUploadedFiles();
26 
27  // handle multiple inputs
28  foreach ($uploadedFiles as $uploadedFile) {
29  if ($uploadedFile->getError() === UPLOAD_ERR_OK && $uploadedFile->getClientMediaType() == 'application/pdf') {
30  $filename = $this->moveUploadedFile($directory, $uploadedFile);
31  $response->withStatus(200);
32  }
33  }
34  }
35 
44  private function moveUploadedFile($directory, UploadedFile $uploadedFile)
45  {
46  $extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
47  $basename = bin2hex(random_bytes(8)); // see http://php.net/manual/en/function.random-bytes.php
48  $filename = sprintf('%s.%0.8s', $basename, $extension);
49 
50  $uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
51 
52  return $filename;
53  }
54 
55 }
__construct(LoggerInterface $logger)
Definition: Upload.php:16
moveUploadedFile($directory, UploadedFile $uploadedFile)
Definition: Upload.php:44
__invoke(Request $request, Response $response, $args)
Definition: Upload.php:21