Importing New Files

Permalink
I am trying to create a single page for site users to import images to their own fileset.
I have been using the information on this C5 Document.
http://documentation.concrete5.org/developers/working-with-files-an...
But I can't get it to upload an image, I can get the image selected but it does not upload, instead I get a 'Page Not Found' error?
Has anybody got a working example of this code?

ConcreteOwl
 
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
I have modified my question to remove the code sample.
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Looks like nobody managed to get this working?
hutman replied on at Permalink Reply
hutman
What version of C5 are you using? Can you provide examples to your code so that people can help you try to debug?
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Hi Hutman
I am developing on Concrete 5.8.0.3
I put the code together as per the instructions on the page I linked to but have since deleted it.
This was why I was asking if anyone had managed to create code that worked as per the instructions?
hutman replied on at Permalink Reply
hutman
Unfortunately there is little or no documentation for 5.8 at this time, so it's unlikely somebody would have done this so far. It should be similar to 5.7 though which is why posting code is a good idea to get help.
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
The code I put together was this, although it is obviously wrong, I would welcome your input..
<?php
function submit()
{
    $error = \Concrete\Core\File\Importer::E_PHP_FILE_ERROR_DEFAULT;
    if (isset($_FILES['photo']) && is_uploaded_file($_FILES['photo']['tmp_name'])) {
        $file = $_FILES['photo']['tmp_name'];
        $filename = $_FILES['photo']['name'];
        $importer = new \Concrete\Core\File\Importer();
        $result = $importer->import($file, $filename);
        if ($result instanceof \Concrete\Core\File\Version) {
            $this->redirect('/my/success/page');
        } else {
            $error = $result;
        }
    } else if (isset($_FILES['photo'])) {
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
I am quite happy to roll back to 5.7 if I can get a working example for that version..
hutman replied on at Permalink Best Answer Reply
hutman
Your code seems to import the file as-is in 5.7 but I get the same 404 issue with 5.8 that you are getting.

I tested this by creating an External Form and putting the contents of your submit function into an action_submit function in the form's controller and it worked perfectly.
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Thanks Hutman
At least I know that if I roll back to 5.7 I can get it working.
I will settle for that until 5.8 documentation is available.

Once again thank you for your help..
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Yep
I rolled back to 5.7 and got it working with your help..
Just need to get the image to load into a users fileset.
I have tried creating a page attribute with the fileset name but so far no joy.
Any thoughts on this?
hutman replied on at Permalink Reply
hutman
Is this only available to users who are logged in? If so you could store the Fileset ID as a User Attribute.

What code do you have that isn't working?
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Hi Hutman
Yes it is for users who are logged in and I am open to any suggestions you might have.
I have hacked various bit of code into the controller file to no avail.
If I created a user attribute called fileset_name how would I add that to the existing controller code listed below..
<?php
namespace Concrete\Block\ExternalForm\Form\Controller;
use Concrete\Core\Controller\AbstractController;
use FileSet;
use FileList;
class UploadImage extends AbstractController
{
function action_submit()
{
    $error = \Concrete\Core\File\Importer::E_PHP_FILE_ERROR_DEFAULT;
    if (isset($_FILES['photo']) && is_uploaded_file($_FILES['photo']['tmp_name'])) {
        $file = $_FILES['photo']['tmp_name'];
        $filename = $_FILES['photo']['name'];
        $importer = new \Concrete\Core\File\Importer();
        $result = $importer->import($file, $filename);
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Just read your comment again 'User ID' so I can create an attribute that holds the User 'ID',
Yes, that makes perfect sense..
Still not sure how to implement it though.
hutman replied on at Permalink Reply
hutman
Try something like this

<?php
namespace Concrete\Block\ExternalForm\Form\Controller;
use Concrete\Core\Controller\AbstractController;
use FileSet;
use FileList;
use Concrete\Core\User\User;
use Concrete\Core\User\UserInfo;
class UploadImage extends AbstractController
{ 
    function action_submit()
    {
        //this is based on the assumption that the user will never be a guest
        $u = new User();
        $ui = UserInfo::getByID($u->getUserID());
        $fileSetId = $ui->getAttribute('file_set_id'); //user attribute handle 'file_set_id' exists and is set to a number
ConcreteOwl replied on at Permalink Reply
ConcreteOwl
Yes
You Beauty! it works perfectly!
I can't thank you enough..