Jump to content

user generated content with images


yesjoar
 Share

Recommended Posts

Hi again. ;)

To generate user content is really easy with the API.

But I have a field for images, which contains max 4 images. Upload in the backend is really simple. Drag & Drop. ;D

But how I can handle these in the frontend? I can write a upload script which upload my images and save the correct data in the field, but whats about the correct folder in /assets/files/? I don't have a page-ID, before saving the user generated content…

Hope you can help a little bit. (:

y.

// done.

wireUpload ist my friend for that. Perfect.

Next time I spend some hours for searching before I start a thread. ;)

Link to comment
Share on other sites

Ok, I think I have a problem with user generated content and images in there.

When I already have a page and somewhere try to edit this, already works fine and images can be uploaded.

But now I want to create user generated content from the beginning. So there is no current ID.

The second thing is - The user should be able to upload some more images. (max 4)

1. How can I handle, that the user has the possibility to upload 4 images?

2. How Can I handle, that these images upload to the the folder with the new page id?

Some input would be nice. ;)

y.

Link to comment
Share on other sites

Create the page and save it, (your page is now ready to receive images)

then loop through the uploaded images and add the images one by one to the $page variable you created before.

don't forget to "$createdPage->setOutputFormatting(false);" each time before saving the page.

  • Like 1
Link to comment
Share on other sites

or:

$u = new WireUpload('uploads'); // from $_FILES['uploads']
$u->setMaxFiles(5);
$u->setOverwrite(false);
$u->setDestinationPath($uploadpage->images->path);
$u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));
// execute() returns an array, so we'll foreach() it even though only 1 file
foreach($u->execute() as $filename) {
$img = new Pageimage($uploadpage->images, $filename);
$img->description = "what you want";
$uploadpage->images->add($img);
}

$uploadpage->save();

Think this should work

Link to comment
Share on other sites

Just experimented also a little and made a little example based on Martjin's code. With some improvements and simpler add image routine. No need to create a new PageImage as the page already has PageImages field. Just add the path to the file and save page. This can also be a remote web url.

Also this example shows how error handling can be done.

I also used a "/.tmp_upload" folder to upload images first and only add to the page if everything is valid. Since $u->execute() uploads the file to the destination specified, it could be a security issue, if not dealed correctly with it. As far as security goes this folder should be made publicly not accessible via htaccess (in comment). In 2.3 you could maybe use built in restriction to files on pages, but not sure it would be appropriate here.

Further, I delete the images uploaded in tmp folder in either case.

This is all example code and may even need additional security for real public uploads. (mime types check of files by the WireUpload is sufficient? Ryan?)

I've put the code on gist.github: https://gist.github.com/4150974

  • Like 8
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...