froot Posted September 9, 2020 Share Posted September 9, 2020 (edited) could someone walk me through what is required to upload files/images to some assets/files-folder from the frontend? All I can find online are general php-tutorials but no PW-tutorials and the PW-API-documentation is abstract to the point of complete confusion. I actually don't know what all these methods do exactly nor which ones are missing to make it work and how could I know? Here's what I did so far: if (isset($_POST['submit'])) { $path = $pages->get("id=1030"); $path->setOutputFormatting(false); $u = new WireUpload('fileToUpload'); $u->setMaxFiles(1); $u->setOverwrite(false); $u->setDestinationPath($path->images->path()); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); foreach($u->execute() as $filename) {$path->files->add($filename);} $u->save(); } <form action="<?php echo $page->url ;?>" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form> error: Exception: Method WireUpload::save does not exist or is not callable in this context many thanks Edited September 9, 2020 by fruid Link to comment Share on other sites More sharing options...
kongondo Posted September 9, 2020 Share Posted September 9, 2020 The go to classic: And this gist by Soma. https://gist.github.com/somatonic/4150974 2 Link to comment Share on other sites More sharing options...
froot Posted September 20, 2020 Author Share Posted September 20, 2020 Thanks, I made significant progress using the mentioned thread. Still some issues… 1. file sizes Uploading a file (or a total of files) larger than setMaxFileSize (20000000 aka 20MB) defined in my code throws an error as intended and doesn't upload photo_121.tiff - Exceeds max allowed file size Uploading a file (or a total of files) larger than upload_max_filesize (32MB) defined in php settings throws a different error and doesn't upload The uploaded file exceeds the upload_max_filesize directive in php.ini. How to amend this text to be the same as the above? Is this message coming from Apache directly and not PW? Where and how to customize all the notices? Uploading a file (or a total of files) larger than post_max_size (50M) defined in php settings throws no error and doesn't upload 2. file count uploading more files than setMaxFiles (20) defined in PW does upload 20 files. (max_file_uploads in php settings is set to 1000). it lists all the files (how to change that?) and then throws and error: Max file upload limit reached I'd rather have it throw an error and not upload anything at all, how to accomplish that? ___ Here's my contact form code: if($input->post->submit) { if(processContactForm($config, $pages)) { $session->redirect($pages->get("/contact/thanks/")->url); } else { $session->redirect($pages->get("/contact/error/")->url); } here's the function to upload (credits to @ryan) function processContactForm ($config, $pages) { $addedfile = _x('added file', 'contact form'); $input = wire('input'); $sanitizer = wire('sanitizer'); // Set a temporary upload folder where the files are stored during form processing // RC: precede temp dir with a period to ensure it remains non-web accessible $upload_path = $config->paths->assets . "files/temp/"; // RC: create temp path if it isn't there already if(!is_dir($upload_path)) { if(!wireMkdir($upload_path)) throw new WireException("No upload path"); } // New wire upload $user_file = new WireUpload('fileToUpload'); // References name of HTML form field $user_file->setMaxFiles(0); $user_file->setMaxFileSize(20000000); // 20MB $user_file->setOverwrite(false); $user_file->setDestinationPath($upload_path); $user_file->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif', 'pdf', 'tiff', 'zip')); // execute upload and check for errors $files = $user_file->execute(); // RC: use count($files) as your decision whether to proceed not not, rather than getErrors() if(!count($files)) { return false; } // trying to count the number of uploaded files and return false to not upload at all if(count($user_file) > 20) { $user_file->error(_x('too many files!', 'contact form')); return false; } // Save in the ProcessWire page tree; map submission to the template fields $np = new Page(); // create new page object $np->template = "user"; $np->parent = $pages->get("title=userinputs"); $np->status(['hidden']); // RC: for safety, only add user uploaded files to an unpublished page, for later approval // RC: also ensure that using v2.3+, and $config->pagefileSecure=true; in your /site/config.php $np->addStatus(Page::statusUnpublished); if ($input->post->givenname != '') { $np->given_name = $sanitizer->text($input->post->givenname); } else { $np->given_name = 'idle'; } if ($input->post->familyname != '') { $np->family_name = $sanitizer->text($input->post->familyname); } else { $np->family_name = 'idle'; } $np->title = $np->family_name.' '.$np->given_name; // Send all the form's $_POST submissions through ProcessWire's sanitization and/or map to a variable with the same name as the template fields we'll be populating $np->name = $np->title; $np->save(); // Run photo upload foreach($files as $filename) { $pathname = $upload_path . $filename; $np->images->add($pathname); $np->message("$filename"); unlink($pathname); } // save page again $np->save(); return true; } and lastly, here's my success.php template: foreach($notices as $notice) { $class = $notice instanceof NoticeError ? "error" : "message"; echo "<p class='$class'>$notice->text</p>"; } $session->removeNotices(); Thanks for any hints! Link to comment Share on other sites More sharing options...
kongondo Posted September 21, 2020 Share Posted September 21, 2020 13 hours ago, fruid said: The uploaded file exceeds the upload_max_filesize directive in php.ini. This comes from WireUpload.php, here as result of PHP error described here. 13 hours ago, fruid said: Where and how to customize all the notices? You could perhaps use try catch blocks to customise notices. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now