Block File Upload... grrr (5.6)
Permalink
Hi i'm trying to add a file upload to a block, the site guest needs to be able to upload a file when submitting a custom form without being logged in.
I can't use the standard form block for a number of reasons, I have tried many solutions and in a perfect world. I need the user to select a file from their system and when the form is submitted for it to upload to file-manger and assign a set to it (maybe even resize on upload to a maximum 800px dimension).
So far I have the following code, and have tried many other things, with no joy ( I have set the file-manger permissions to guest allowed to upload too):
Help greatly appreciated.
Other things i've tried without success.
I can't use the standard form block for a number of reasons, I have tried many solutions and in a perfect world. I need the user to select a file from their system and when the form is submitted for it to upload to file-manger and assign a set to it (maybe even resize on upload to a maximum 800px dimension).
So far I have the following code, and have tried many other things, with no joy ( I have set the file-manger permissions to guest allowed to upload too):
$postdata = $_POST; if ($fp->canAddFiles()) { Loader::library("file/importer"); $fi = new FileImporter(); $upimage = $fi->import($_FILES['image']['tmp_name'], $_FILES['image']['name']); Loader::model('file_set'); $fs = FileSet::getByName('Bear Adventure Uploads'); $fsf = $fs->addFileToSet($newImage); $postdata['image'] = $upimage->getFileID(); } Loader::model('adventure', 'adventures'); $res = Adventure::add($postdata);
Help greatly appreciated.
Other things i've tried without success.
if (!is_dir(DIR_FILES_UPLOADED_STANDARD.'/incoming')) { mkdir(DIR_FILES_UPLOADED_STANDARD.'/incoming', 0777); } $destFile = DIR_FILES_UPLOADED_STANDARD. '/incoming/' . $_FILES['image']['name']; if(substr($_FILES['image']['name'],-4,-3) != '.'){ $destFile = $destFile.'.jpg'; } move_uploaded_file($_FILES['image']['tmp_name'], $destFile); chmod($destFile, 0777); //--// $temppath = $_FILES['image']['tmp_name']; // get the temp path $filename = $_FILES['image']['name']; //get the temp name Loader::library("file/importer"); $fi = new FileImporter(); $newFile = $fi->import($temppath, $filename); //upload the file to the file manager
Viewing 15 lines of 21 lines. View entire code block.
Hi Hutman, thanks for the reply but nope it doesn't seem to work for me, I get nothing at all in the file manager.
Code in full:
Code in full:
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); $form = Loader::helper('form'); $dth = Loader::helper('form/date_time'); $bears = unserialize(Config::get('ADVENTURES_BEARS')); $charities = unserialize(Config::get('ADVENTURES_CHARITIES')); $google_api = '**********'; $fp = FilePermissions::getGlobal(); if($_POST){ $block = Block::getByID($_POST['bID']); $cnt = $block->getController(); $postdata = $_POST; $fp = FilePermissions::getGlobal(); if ($fp->canAddFiles()) { Loader::library("file/importer"); $fi = new FileImporter();
Viewing 15 lines of 173 lines. View entire code block.
I'm sorry I'm really confused about how exactly you are trying to do this.
If it was me I would make this an external form with a controller and do all of the post stuff in the controller.
Since I don't have the package or the config variables I can't test this, but from what I'm seeing I don't think that this section at the top of the page is even being hit, are you getting emails from this? If you add some echo statements in there is it hitting the code you think it is?
If it was me I would make this an external form with a controller and do all of the post stuff in the controller.
Since I don't have the package or the config variables I can't test this, but from what I'm seeing I don't think that this section at the top of the page is even being hit, are you getting emails from this? If you add some echo statements in there is it hitting the code you think it is?
Hi Hutman
Its code based of the studio testimonials block, it all works, but the image upload. Happy to send you more files if you'd be interested in helping me.
Thanks
Its code based of the studio testimonials block, it all works, but the image upload. Happy to send you more files if you'd be interested in helping me.
Thanks
I'm happy to help if you want to send me the package, or whatever I need. Also if you could verify what version of C5 so I'm testing on the same that would be helpful.
You just had the wrong variable in the addFileToSet call, other than that everything seems right.