Jump to content

jessevl

Members
  • Posts

    7
  • Joined

  • Last visited

Everything posted by jessevl

  1. Thanks for the awesome explanation! Yes I think I fully understand now. I was just setting the OF to false on one instance of the page, writing the image to another instance of the page, and saving yet another (unchanged, non-of'ed) instance of the page... (which failed because OF was not set to false on that specific instance).
  2. That works. But I don't think I fully understand why that makes a difference (although it's clear that it does). I'm assuming it's actually a PHP thing and not Processwire specific, any hint on what keyword to search for to better understand how this works? Thanks for the help, really appreciate it.
  3. OK, so I now changed the function to use wire instead of the processwire variables directly. I assume wire('pages')->get($pageid) should return the object to me and allow me to add and save etc. However, I get an "Error: Exception: Can't save page 1037: /viables/view/test2/: Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved. [profilepicture]". It seems to me like I disabled output formatting already but apparently I cannot do that in this way? function uploadphoto($inputname,$pagefield,$pageid){ $path = wire('config')->paths->assets . 'tmp/'; $newfilename = $pageid."_".$pagefield; $u = new WireUpload($inputname); $u->setMaxFiles(1); $u->setMaxFileSize(2*2048*2048); $u->setOverwrite(true); $u->setDestinationPath($path); $u->setTargetFilename($newfilename); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); // execute upload and check for errors foreach($u->execute() as $filename) { if(!$u->getErrors()){ // add images upload wire('pages')->get($pageid)->of(false); wire('pages')->get($pageid)->$pagefield->deleteAll(); wire('pages')->get($pageid)->$pagefield->add($path . $filename); // save page wire('pages')->get($pageid)->save(); //unlink($path . $filename); } else { // remove files //unlink($path . $filename); // get the errors foreach($u->getErrors() as $error) { $errors[] = $error; } return "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>"; } } } $profilepicture = $_FILES['profilepicture']['name'][0]; $profilebackground = $_FILES['profilebackground']['name'][0]; if ($profilepicture) { uploadphoto('profilepicture','profilepicture',$editpage->id); } if ($profilebackground) { uploadphoto('profilebackground','profilebackground',$editpage->id); }
  4. Thanks a lot that's indeed it! I see I made the same mistake in the add to the page part as well, but at least that does give me error messages I can work with (and the solution is again to use wire()) About the $this part. You're right it doesn't make sense, it is what one of the examples did but I replaced it for $config later on.
  5. Hi there, I've been successfully using WireUpload to upload images to a temp folder and then add them to a page and subsequently remove the temp file. However, since I need to do this in a variety of different pages and multiple times on one page, I'd rather build a function for this. Somehow I cannot get this working in any way while almost the same code works fine outside of a function. Does anybody know what I'm doing wrong? WireUpload should be able to access $_FILES just fine as it is a superglobal variable, but it acts as if it isn't...I can see that nothing is uploaded to the temporary folder (I commented out the temp folder cleanup code). I found one other case of a similar problem but that was solved by directly adding the upload from the PHP tmp dir to the page (skipping wireupload altogether)... which doesn't seem right or safe to me. My code when using a function: function uploadphoto($inputname,$pagefield,$pageid){ //inputname is the input field name, pagefield is the field the image should be added to, pageid is the page it should be added to $path = $config->paths->root . 'site/assets/tmp/'; $newfilename = $pageid."_".$pagefield; //wireupload seems to add an extension by itself so no need to do that? $u = new WireUpload($inputname); $u->setMaxFiles(1); $u->setMaxFileSize(2*2048*2048); $u->setOverwrite(true); $u->setDestinationPath($path); $u->setTargetFilename($newfilename); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); // execute upload and check for errors foreach($u->execute() as $filename) { if(!$u->getErrors()){ // add images upload $pageid->of(false); $pageid->$pagefield->deleteAll(); // I want only 1 image $pageid->$pagefield->add($path . $filename); // save page $pageid->save(); //unlink($path . $filename); commented this out because I want to check if it uploads anything at all... and it doesn't } else { // remove files //unlink($path . $filename); commented this out because I want to check if it uploads anything at all... and it doesn't // get the errors foreach($u->getErrors() as $error) { $errors[] = $error; } return "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>"; } } } $profilepicture = $_FILES['profilepicture']['name'][0]; $profilebackground = $_FILES['profilebackground']['name'][0]; if ($profilepicture) { //check if a file was uploaded uploadphoto('profilepicture','profilepicture',$editpage->id); //pass to the function } if ($profilebackground) { uploadphoto('profilebackground','profilebackground',$editpage->id); } The input field (form itself has proper encoding set): <input type='file' name='profilebackground' accept='image/jpg,image/jpeg,image/gif,image/png'/> The following code (without a function) works perfectly: $profilepicture = $_FILES['profilepicture']['name'][0]; $profilebackground = $_FILES['profilebackground']['name'][0]; if ($profilepicture) { $path = $this->config->paths->root . 'site/assets/tmp/'; $newfilename = $user->id."_profilepicture"; $u = new WireUpload('profilepicture'); $u->setMaxFiles(1); $u->setMaxFileSize(2*2048*2048); $u->setOverwrite(true); $u->setDestinationPath($path); $u->setTargetFilename($newfilename); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); // execute upload and check for errors foreach($u->execute() as $filename) { if(!$u->getErrors()){ // add images upload $user->of(false); $user->profilepicture->deleteAll(); $user->profilepicture->add($path . $filename); // save page $user->save(); unlink($path . $filename); } else { // remove files unlink($path . $filename); // get the errors foreach($u->getErrors() as $error) { $errors[] = $error; } $page->body .= "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>"; } } } if ($profilebackground) { $path = $this->config->paths->root . 'site/assets/tmp/'; $origfilename = $_FILES['profilebackground']['name'][0]; $filenameparts = pathinfo($u); $newfilename = $user->id."_profilebackground".$filenameparts['extension']; $u = new WireUpload('profilebackground'); $u->setMaxFiles(1); $u->setMaxFileSize(2*2048*2048); $u->setOverwrite(true); $u->setDestinationPath($path); $u->setTargetFilename($newfilename); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); // execute upload and check for errors foreach($u->execute() as $filename) { if(!$u->getErrors()){ // add images upload $user->of(false); $user->profilebackground->deleteAll(); $user->profilebackground->add($path . $filename); // save page $user->save(); unlink($path . $filename); } else { // remove files unlink($path . $filename); // get the errors foreach($u->getErrors() as $error) { $errors[] = $error; } $page->body .= "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>"; } } } Thanks in advance!
  6. Makes sense, just wanted to be sure. Thanks for the explanation!
  7. Hi, I'm working on a Processwire powered directory-like website where users can register on a front-end and post new pages to a directory. Users can also list certain preferences (linked as pagefields) in their profile and the website then shows only the pages in the directory that fit these same preferences. So far so good, I'm pretty sure that I set that up the way it's supposed to (or at least a generally acceptable solution). However, I also want users who aren't logged in to be able to filter these pages in the directory. While this in itself is fairly simple I also want to allow the viewer to add and delete some of these filters and show the viewer the currently selected filters on the frontend, so I should store the currently selected filters somewhere. The way I do this now is by having several select elements post the requested filters to the template page and then assigning these to a new user object and simply not save the user object, and consequently return all filters on the non-saved user object to the form. When a viewer selects or deselects certain filters the entire list of filters is posted again and added to a new user object, which then again returns to the form and isn't saved. This works really well, however, I'm wondering if it is acceptable practice to create user objects and not save them just to temporarily store some information? Do non saved objects still leave some traces anywhere in the DB maybe? Thanks!
×
×
  • Create New...