Jump to content

Image upload via WireUpload


briangroce
 Share

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
Link to comment
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
Link to comment
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!

Link to comment
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
Link to comment
Share on other sites

  • 5 years later...
  • 2 months 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

×
×
  • Create New...