Jump to content
briangroce

Image upload via WireUpload

Recommended Posts

Hi there. First of all, thank you for all of your work on ProcessWire, it is truly an amazing piece of work. I can't imagine developing on anything else anymore...

I am having a bit of trouble uploading images through the API. I want users of the site to be able to upload images into their page. Here is the code that I have:

Get the page that belongs to that user:

$l = $pages->find("user_account={$user}, template=listing")->first();

Then this is the code that I am using to upload the image:

if ($input->post("imagesend") == "1") {
$l->setOutputFormatting(false);

// instantiate the class and give it the name of the HTML field
$u = new WireUpload('userfile');

// tell it to only accept 1 file
$u->setMaxFiles(1);

// tell it to rename rather than overwrite existing files
$u->setOverwrite(false);

// have it put the files in their final destination. this should be okay since
// the WireUpload class will only create PW compatible filenames
$u->setDestinationPath($l->files->path);

// tell it what extensions to expect
$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) $l->files->add($filename);

// save the page
$page->save();
}

The error that I am getting is:

Exception: You must set the destination path for WireUpload before executing it

Any ideas? Thanks!

  • Like 1

Share this post


Link to post
Share on other sites

try.replayce des:

$u->setDestinationPath($l->files->path);

weong des:

$u->setDestinationPath($l->files->path());

Un path(); no path; -el methode no.variables

cd ..

Share this post


Link to post
Share on other sites

Welcome to the forums briangroce. I've begun to understand WillyC's language, but it's taken awhile. I think what he was trying to say is that you need to use $l->files->path(); as a function (method) call, and not as a property. Though that's a good reminder to me that I need to make it work as a property too.

  • Like 1

Share this post


Link to post
Share on other sites

Thank you WillyC and Ryan for the fast help! I have played around with the solutions that you guys have offered... and I am now not getting an error. That is both a great thing and a frustrating thing. Even though I am not getting an error, the image is not making it into the page either. This is what I have now:

if ($input->post("imagesend") == "1") {
$l->setOutputFormatting(false);

// instantiate the class and give it the name of the HTML field
$u = new WireUpload('userfile');

// tell it to only accept 1 file
$u->setMaxFiles(1);

// tell it to rename rather than overwrite existing files
$u->setOverwrite(false);

// have it put the files in their final destination. this should be okay since
// the WireUpload class will only create PW compatible filenames
$u->setDestinationPath($l->images->path());

// tell it what extensions to expect
$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) { $l->images->add($filename); }

// save the page
$l->save();
}

I updated "files" to "images" as well because I realized that I do not actually have a files field on that page. I want the images that are uploaded to make it into the "images" field

My file field looks like this:

<input type="file" name="userfile" />

Does anything here look out of the ordinary?

Thanks!

Share this post


Link to post
Share on other sites

Have you tried putting in the name attribute as "userfile[]" rather than "userfile" ?

  • Like 1

Share this post


Link to post
Share on other sites

Ryan,

Thank you for your help!

It ended up being something different. I changed my form tag to the following at it works now. hooray!

<form action="" method="POST" enctype=multipart/form-data>

I didn't originally have the enctype=multipart/form-data in there. I don't know what I was thinking.

  • Like 3

Share this post


Link to post
Share on other sites

It might be important to note, that while the path can be accessed via ->path, if the field is a multiple-image field. (Pageimages) Otherwise, for single items, it has to be ->pagefiles->path.

At least this is what I have experienced. Not sure if I am doing something wrong :)

Share this post


Link to post
Share on other sites

Hey @ryan I was woundering if you would recommend that one save the Page once, before trying to add files for upload and then save again?

I think you mentioned that in some thread?

Or is this not necessary anymore?

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By rushy
      Hi. I've been using Processwire for a few years now and installed it on a few different shared hosting servers without issues, but I'm now running into an issue on a Fasthosts shared server that I've not seen before. The installation goes ok and I have a working default site profile but I am unable to complete any uploads of images. It just hangs at the progress spinner during the upload. I thought it might be a priviledge issue so have temporarily set the dir / file privs. as 777 / 666 just to test that but it made no difference. I have no errors in the wire log. In server error log I have:
      set_time_limit() has been disabled for security reasons in ....htdocs/wire/core/ImageSizerEngine.php on line 1035 and i note in the assets/file dir. where the image is being stored the date on the file being created has a year of 1970 and 0 length. If I leave the page this file disappears and nothing has been stored. 
      Does anyone know how I can enable set_time_limit if this is likely to be the problem? 
      Many thanks! Paul
    • By Fanni
      Hi all, 
      I am having some issues uploading a PDF to our website (a problem we’ve had before).
      I have tried reducing the size of the PDF but the problem seems to be with generating the thumbnail image because the upload works under the Fallback mode but the thumbnail is terrible (see attached). 
      Does anybody have any advice about this? Please note that I'm not a developer. 
      Thanks!
       


    • By louisstephens
      So I am using ajax to upload an image, but I am getting the error "Method WireUpload:: save does not exist or is not callable". I am not quite sure how to go about fixing this (at the moment).
      elseif($config->ajax && $input->urlSegment1 == "upload-preview") { $u = $config->paths->assets . "files/pdfs/"; $title = $sanitizer->text($_POST['title']); $p = new Page(); $p->template = "preview"; $p->parent = $pages->get("/previews/"); $p->name = $title; $p->title = $title; $p->save(); $p->setOutputFormatting(false); $u = new WireUpload('preview_image'); $u->setMaxFiles(1); $u->setOverwrite(false); $u->setDestinationPath($p->preview_image->path()); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png', 'pdf')); foreach($u->execute() as $filename) { $p->preview_image->add($filename); } $u->save(); } I compared my code to something I did previously (though previously I just posted to the current template file, not through ajax) which works, but this doesnt seem to be working. I have the _init.php file prepending as well. Does anyone have any ideas of what might be happening?
    • By Marcel
      Hey,
      I am about to use the tool Create Users Batcher. We have 450 users. When I tryed it with like 20 test users it worked but it took a while. Now my concerns are that 450 might be to stressful? Does anyone have experience with several hundreds of users? My thoughts are to split it in smaller goups and upload them one group after another.
      best wishes
      marcel
    • By dragan
      All of a sudden image-upload seems broken. No matter if I want to upload one single image or several images.
      Chrome console says:
      Uncaught SyntaxError: Unexpected token in JSON at position 414 at JSON.parse (<anonymous>) at Function.parseJSON (JqueryCore.js?v=1535110864:2) at XMLHttpRequest.<anonymous> (InputfieldImage.js?v=122-1535110864:1973) // File uploaded: called for each file xhr.addEventListener("load", function() { xhr.getAllResponseHeaders(); var response = $.parseJSON(xhr.responseText); // line 1973 The spinning animation GIF keeps on spinning forever...
      I thought I have seen a forum thread about such an error, but I can't find it anymore.
      If I inspect https://dev.mysite.com/backstage/page/edit/?&amp;id=11203&amp;InputfieldFileAjax=1 I only see headers, no content.
      content-type: application/octet-stream (in request header)
      in response headers:
      x-tracy-ajax: 1
      x-xss-protection: 1; mode=block
      Does anyone know what could cause this?
      PW 3.0.111 / PHP 7.2.8
×
×
  • Create New...