Created
February 23, 2014 04:47
-
-
Save adityapooniya/9166974 to your computer and use it in GitHub Desktop.
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
<!-- Assume a file is uploaded with this HTML form --> | |
<form method="POST" enctype="multipart/form-data"> | |
<input type="file" name="foo" value=""/> | |
<input type="submit" value="Upload File"/> | |
</form> | |
<!-- When the HTML form is submitted, the server-side PHP code can validate and upload the file like this: --> | |
<?php | |
$storage = new \Upload\Storage\FileSystem('/path/to/directory'); | |
$file = new \Upload\File('foo', $storage); | |
// Optionally you can rename the file on upload | |
$new_filename = uniqid(); | |
$file->setName($new_filename); | |
// Validate file upload | |
// MimeType List => http://www.webmaster-toolkit.com/mime-types.shtml | |
$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/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() | |
); | |
// Try to upload file | |
try { | |
// Success! | |
$file->upload(); | |
} catch (\Exceptions $e) { | |
// Fail! | |
$errors= $file->getErrors(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment