Jump to content

Recommended Posts

Posted

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
Posted

try.replayce des:

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

weong des:

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

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

cd ..

Posted

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
Posted

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!

Posted

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

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 :)

  • 2 months later...
Posted

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?

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
×
×
  • Create New...