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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Max Esnée
      Hi,
      When I upload a large(ish) SVG (≈120ko in this case) into an image field, it tends to get stuck at 100% and block the whole website. If I force refresh the admin page, the svg has not been uploaded. The file is correctly copied into the assets folder though. Other svg tends to be uploaded fine albeit I've noticed that pages containing svg images in the admin are very slow to load generally. Other pages using the same template but with jpeg or png images are much faster to load.
      I get the following error in the JS console:

      I develop locally using MAMP on macOS and I'm using Firefox. I'm using the last ProcessWire version (3.0.165).
      I also tested in Chrome and got the same error.
    • By Marvin
      Hello,
      I'm new at process wire and i want to make an web using upload file and showing it at the table as a link to open it at the new tab. I wa succed while upload a file, but how i showing it as a link at the table to open it at the new tab of my browser? Any suggestion may helpfull
      Here i attach my code below :
      This code is for upload it to back-end (process wire)
      <?php $note = $note2 = $hidden =""; if($input->post->submit){ $upload_path = $config->paths->assets.'files/upload/'; if(!is_dir($upload_path)){ if(!wireMkdir($upload_path)) throw new WireException("No upload path"); } $original = $sanitizer->text($input->post->original); $indonesia = $sanitizer->text($input->post->indonesia); $other = $sanitizer->text($input->post->other); $composer = $sanitizer->text($input->post->composer); if(!$original || !$indonesia || !$other || !$composer){ $note = "Data tidak lengkap"; } else { $newFile = new WireUpload("song_files"); $newFile->setMaxFiles(1); $newFile->setOverwrite(false); $newFile->setDestinationPath($upload_path); $newFile->setValidExtensions(array('pdf','docx','doc')); $files = $newFile->execute(); if(!count($files)) { $newFile->error("No files received, so not creating page."); return false; } $newImg = new WireUpload("img_files"); $newImg->setMaxFiles(1); $newImg->setOverwrite(false); $newImg->setDestinationPath($upload_path); $newImg->setValidExtensions(array('jpeg','jpg','png','gif')); $files = $newImg->execute(); if(!count($files)) { $newImg->error("No files received, so not creating page."); return false; } $newPage = new Page(); $newPage->template = "files"; $newPage->parent = $pages->get("/files/"); $newPage->title = $original; $newPage->text_1 = $indonesia; $newPage->text_2 = $other; $newPage->text_3 = $composer; $newPage->of(false); $newPage->save(); foreach($files as $filename) { $filepath = $upload_path . $filename; $newPage->files->add($filepath); $newPage->message("Add file : $filename"); unlink($filepath); } $newPage->save(); } } ?> and this code to showing it as a link at the table
      <table class="border"> <tr> <th>No.</th> <th>Original Song Title</th> <th>Indonesia Song Title</th> <th>Other Song Title</th> <th>Composer</th> <th>File (pdf)</th> </tr> <?php $num = 1; $song; foreach($pages->get("/files/")->children as $child) { //showing every child at files parent directory $page == $child; $song = $pages->get("/files/".$child->id."/")->files; //showing uploaded files at child directory echo $child->id; echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$song->httpUrl."'</a>".$song->name."</td></tr>"; } ?> </table> Thank you for any suggestion
    • By markus_blue_tomato
      We have created a module to create BlurHash strings for images while uploading in ProcessWire. This blurry images will be saved in the database because they are very small (20-30 characters) and can be used for Data-URL's  as placeholders for image-lazy loading.
      https://github.com/blue-tomato/ImageBlurhash
      E.g. where we use this in production:
      https://www.blue-tomato.com/en-INT/blue-world/ https://www.blue-tomato.com/en-INT/blue-world/products/girls-are-awesome/ https://www.blue-tomato.com/en-INT/buyers-guides/skateboard/skateboard-decks/ https://www.blue-tomato.com/en-INT/team/anna-gasser/
    • 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!
       


×
×
  • Create New...