Skip to content

Instantly share code, notes, and snippets.

@boboldehampsink
Created June 22, 2015 19:19
Show Gist options
  • Save boboldehampsink/2e645655d613574ec4b7 to your computer and use it in GitHub Desktop.
Save boboldehampsink/2e645655d613574ec4b7 to your computer and use it in GitHub Desktop.
Upload Assets with jQuery File Upload - see https://blueimp.github.io/jQuery-File-Upload/
<?php
namespace Craft;
class UploadsController extends BaseController {
/**
* Process uploaded files
*
* @author Bob Olde Hampsink
*/
public function actionProcess() {
// Disable devmode script log
craft()->config->set('devMode', false);
// Get given folder id
$folderId = craft()->request->getParam('folder');
// Process files for source
$response = craft()->uploads->process($folderId);
// Process files as assets
foreach($response['files'] as $key => $file) {
// Index file
$asset = craft()->uploads->index($file, $folderId);
// Add asset to array
$response['files'][$key]->asset = $asset->id;
}
// Return response as JSON
$this->returnJson($response);
}
}
<?php
namespace Craft;
class UploadsService extends BaseApplicationComponent
{
// UploadHandler niet direct initialiseren
protected $initialize = false;
// Source settings
protected $settings = array();
// Extra opties voor UploadHandler
protected $options = array();
// UploadHandler error messages
protected $errorMessages = array(
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk',
8 => 'A PHP extension stopped the file upload',
'post_max_size' => 'The uploaded file exceeds the post_max_size directive in php.ini',
'max_file_size' => 'File is too big',
'min_file_size' => 'File is too small',
'accept_file_types' => 'Filetype not allowed',
'max_number_of_files' => 'Maximum number of files exceeded',
'max_width' => 'Image exceeds maximum width',
'min_width' => 'Image requires a minimum width',
'max_height' => 'Image exceeds maximum height',
'min_height' => 'Image requires a minimum height'
);
// Verwerk geuploade files
public function process($folderId) {
// Include UploadHandler class that comes with jQuery File Upload
require(CRAFT_PLUGINS_PATH.'uploads/libraries/UploadHandler.php');
// Get source settings by ID
$sourceId = craft()->assets->getFolderById($folderId)->sourceId;
$this->settings = craft()->assetSources->getSourceById($sourceId)->settings;
// Stel opties in
$this->options = array(
'upload_dir' => $this->settings['path'],
'upload_url' => $this->settings['url'],
'image_versions' => array(
// The empty image version key defines options for the original image:
'' => array(
// Automatically rotate images based on EXIF meta data:
'auto_orient' => true
)
)
);
// Initialiseer UploadHandler
$handler = new \UploadHandler($this->options, $this->initialize, $this->errorMessages);
// Vang post requests op en return de response
return $handler->post(false);
}
// Indexeer geuploade files
public function index($file, $folderId) {
// Create new AssetFile Model
$asset = new AssetFileModel();
$asset->sourceId = craft()->assets->getFolderById($folderId)->sourceId;
$asset->folderId = $folderId;
$asset->filename = IOHelper::getFileName($file->name);
$asset->kind = IOHelper::getFileKind(IOHelper::getExtension($file->name));
$asset->size = $file->size;
$asset->dateModified = IOHelper::getLastTimeModified($this->settings['path'].$file->name);
if ($asset->kind == 'image')
{
list($width, $height) = getimagesize($this->settings['path'].$file->name);
$asset->width = $width;
$asset->height = $height;
}
// Index file
craft()->assets->storeFile($asset);
return $asset;
}
}
@outhouse
Copy link

Arrived at this gist from this post. Just wondering, how you were able to get this to work with a Sprout Form + Jquery File Upload?

I assume the above code would go inside a custom Craft plugin. I'm just unclear as to how you would handle calling fileupload from the front end with this solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment