Created
January 14, 2016 19:28
-
-
Save frostbitten/c1dce70023321158a2fd to your computer and use it in GitHub Desktop.
simple upload integration with dropzone and sirius\upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UserFrosting\Uploader; | |
// add require "codeguy/upload":"*" to composer | |
use Sirius\Upload\Handler as UploadHandler; | |
// Register CSS and JS includes for the pages | |
$app->hook('includes.css.register', function () use ($app){ | |
// Register common CSS files | |
$app->schema->registerCSS("upload", "https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/dropzone.css"); | |
}, 1); | |
$app->hook('includes.js.register', function () use ($app){ | |
// Register common JS files | |
$app->schema->registerJS("upload", "https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/dropzone.js"); | |
}, 1); | |
$app->get('/upload(/:what)/?', function ($what='') use ($app) { | |
// if (!$app->user->checkAccess('uri_')){ | |
// $app->notFound(); | |
// } | |
$app->render('upload.twig', ['what'=>$what]); | |
}); | |
$app->post('/upload(/:what)/?', function ($what='') use ($app) { | |
$save_location = '../userfiles/'.$app->user->id; | |
if(!is_dir($save_location)){mkdir($save_location);} | |
$storage = new \Upload\Storage\FileSystem($save_location); | |
$file = new \Upload\File('file', $storage); | |
// Optionally you can rename the file on upload | |
$new_filename = uniqid(); | |
$file->setName($new_filename); | |
// Validate file upload | |
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml | |
$file->addValidations(array( | |
// Ensure file is of type "image/png" | |
//new \Upload\Validation\Mimetype('image/png'), | |
//You can also add multi mimetype validation | |
new \Upload\Validation\Mimetype(array('image/png', 'image/jpg', 'image/jpeg', 'image/gif')), | |
// Ensure file is no larger than 5M (use "B", "K", M", or "G") | |
new \Upload\Validation\Size('5M') | |
)); | |
// Access data about the file that has been uploaded | |
$data = array( | |
'name' => $file->getNameWithExtension(), | |
'extension' => $file->getExtension(), | |
'mime' => $file->getMimetype(), | |
'size' => $file->getSize(), | |
'md5' => $file->getMd5(), | |
'dimensions' => $file->getDimensions() | |
); | |
error_log('try to save file'); | |
// Try to upload file | |
try { | |
// Success! | |
$file->upload(); | |
} catch (\Exception $e) { | |
// Fail! | |
$errors = $file->getErrors(); | |
error_log('failed saving file:'.print_r($errors,1)); | |
} | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layouts/layout-dashboard.twig" %} | |
{% set page_group = "upload" %} | |
{% block page %} | |
{# By putting this in a special block, we ensure that it will be set AFTER the default values are set in the parent template, | |
but BEFORE the page itself is rendered. #} | |
{% set page = page | merge({ | |
"title" : "A secure, modern user management system for PHP.", | |
"description" : "Main landing page for public access to this website.", | |
"active_page" : "" | |
}) %} | |
{{ parent() }} | |
{% endblock %} | |
{% block page_styles %} | |
{{ parent() }} | |
{% endblock %} | |
{% block page_scripts %} | |
<script> | |
Dropzone.autoDiscover = false; | |
var uploader = new Dropzone("#uploader"); | |
uploader.on("sending", function(file, xhr, formData) { | |
console.log(file); | |
// Will send the filesize along with the file as POST data. | |
formData.append("filesize", file.size); | |
}); | |
</script> | |
{% endblock %} | |
{% block content %} | |
<section> | |
<form action="{{site.uri.public}}/upload" class="dropzone" id="uploader"> | |
<input name="{{csrf_key}}" value="{{csrf_token}}" type="hidden"> | |
<div class="fallback"> | |
<input name="file" type="file" multiple /> | |
</div> | |
</form> | |
</section> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment