Advertisement
vinedfs

Exemplo upload

Jul 9th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  1. $app = new \Slim\App();
  2.  
  3. $app->post('/', function ($request, $response, $args) {
  4.  
  5.     $files = $request->getUploadedFiles();
  6.  
  7.     if (empty($files['image'])) {
  8.         return $response->withStatus(200)->write('No files.');
  9.     }
  10.  
  11.     $file = $files['image'];
  12.  
  13.     if ($file->getError() === UPLOAD_ERR_OK) {
  14.  
  15.         $extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
  16.         $basename = bin2hex(random_bytes(8)); // see http://php.net/manual/en/function.random-bytes.php
  17.         $filename = sprintf('%s.%0.8s', $basename, $extension);
  18.         $file->moveTo(__DIR__ . '/uploads' . DIRECTORY_SEPARATOR . $filename);
  19.  
  20.         return $response->withStatus(200)->write('File uploaded.');
  21.     }
  22.  
  23.     return $response->withStatus(200)->write('Error to upload.');
  24.  
  25. });
  26.  
  27. $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement