Exemple #1
0
 /**
  * /
  * @param  [type] $app [description]
  * @return [type]      [description]
  */
 public function route($app)
 {
     /*
      * UPLOAD
      */
     $app->get('/upload', function () use($app) {
         $app->render('upload');
     });
     $app->post('/upload', function () use($app) {
         $validator = new \FileUpload\Validator\Simple(1024 * 1024 * 2, $app->allowedFileTypes);
         $pathresolver = new \FileUpload\PathResolver\Simple('/var/www/html/files/');
         $filesystem = new \FileUpload\FileSystem\Simple();
         $fileupload = new \FileUpload\FileUpload($_FILES['files'], $_SERVER);
         $fileupload->setPathResolver($pathresolver);
         $fileupload->setFileSystem($filesystem);
         $fileupload->addValidator($validator);
         list($files, $headers) = $fileupload->processAll();
         foreach ($headers as $header => $value) {
             $app->response->headers->set($header . ': ' . $value);
         }
         $body = json_encode(array('files' => $files));
         $app->response->setBody($body);
         $app->stop();
     });
     return $app;
 }
<?php

require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "src/FileUpload/FileUpload.php";
header("Content-Type: text/html; charset=utf-8");
// Start
$file = new FileUpload\FileUpload();
// set input( name of input file: show html )
$file->setInput("my_file");
// Directory output
$file->setDestinationDirectory("upload");
//Use "copy" function( Default: move_uploaded_file )
$file->setUploadFunction("copy");
//Set file size limit( Formats: 10M , 10MB, 10mb, 10485760, etc )
$file->setMaxFileSize("10M");
// Callback in before upload
$file->setCallbackInput(function ($data) {
    echo "<h3>Start!</h3>";
});
// Callback in after upload
$file->setCallbackOutput(function ($data) {
    echo "<h3>End!</h3>";
    if ($data->status === true) {
        echo "<p>The " . $data->filename . " file is upload</p>";
    } else {
        echo "<p>The " . $data->filename . " file could not be uploaded to the server</p>";
    }
});
// Set destination directory( output file )
$file->setDestinationDirectory(dirname(__FILE__));
// Set multiples mime types
$file->setAllowedMimeTypes(array('image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png'));