Skip to content

Instantly share code, notes, and snippets.

@erikaheidi
Created April 20, 2013 14:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save erikaheidi/5426089 to your computer and use it in GitHub Desktop.
Save erikaheidi/5426089 to your computer and use it in GitHub Desktop.
upload test with silex
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app['debug'] = true;
$app->get('/', function () use ($app) {
$form = '<h2>Silex Form Upload Test</h2>';
$form .= '<form method="post" action="/silex/upload" enctype="multipart/form-data">';
#$form .= '<input type="hidden" name="MAX_FILE_SIZE" value="500">';
$form .= '<input type="file" name="file" />';
$form .= '<input type="submit" value="Submit"/>';
return $form;
});
$app->post('/upload', function (Request $request) use ($app) {
$file = $request->files->get('file');
if ($file !== null) {
$path = 'uploads/';
$file->move($path, $file->getClientOriginalName());
$response = "file uploaded successfully: " . $file->getClientOriginalName();
$response .= '<br>size: ' . filesize($path . '/' . $file->getClientOriginalName()) / 1024 . ' kb';
return new Response($response);
} else {
return new Response("An error ocurred. Did you really send a file?");
}
});
$app->get('/phpinfo', function () use ($app) {
phpinfo();
exit;
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment