Skip to content

Instantly share code, notes, and snippets.

@sirmews
Forked from somatonic/inputfieldfile-form.php
Created January 11, 2017 09:16
Show Gist options
  • Save sirmews/44f3cf6e0bb5b412877b084050b3d20c to your computer and use it in GitHub Desktop.
Save sirmews/44f3cf6e0bb5b412877b084050b3d20c to your computer and use it in GitHub Desktop.
ProcessWire front-end upload form example using ProcessWire Inputfields and form processing.
<?php
/**
* ProcessWire (2.5) InputfieldFile front-end upload form example
* Various workarounds to get it working with Errors for WireUpload
* and removing files upload after error
*/
$sent = false;
$upload_path = $config->uploadTmpDir;
$form = $modules->get("InputfieldForm");
$form->attr("action", "./");
$form->attr("name", "uploadform");
$field = $modules->get("InputfieldFile");
$field->label = "Images";
$field->description = "Upload your images maximal 3 files.";
$field->required = 1;
$field->attr("name+id", 'myimages');
$field->destinationPath = $upload_path;
$field->extensions = "jpg jpeg gif png";
$field->maxFiles = 3;
$field->maxFilesize = 2*1024*1024; // 2mb
$form->add($field);
$field = $modules->get("InputfieldSubmit");
$field->label = "Submit";
$field->attr("name+id",'submit');
$form->add($field);
// if sumbitted process form
if($input->post->submit){
// process form input
$form->processInput($input->post);
// Workaround to get errors from the WireUpload used by the file field
// Ask Ryan to implement WireUpload errors handling on form file field
if($form->get("myimages")->getWireUpload()->getErrors()){
$errors = $form->get("myimages")->getWireUpload()->getErrors();
foreach($errors as $error) $form->get("myimages")->error($error);
}
if(!count($form->getErrors())){
// if no error occured
// create new page and save values
$uploadpage = new Page();
$uploadpage->template = "upload-entry";
$uploadpage->parent = $pages->get("/upload-api/");
// add title/name and make it unique with time and uniqid
$uploadpage->title = date("d-m-Y H:i:s") . " - " . uniqid();
$uploadpage->addStatus(Page::statusUnpublished);
$uploadpage->save();
// save uploaded files to new page and remove temp files
$files = explode("|",$form->get("myimages")->value);
foreach($files as $file){
if($file && file_exists($upload_path . $file)){
$uploadpage->myimages->add($upload_path . $file);
unlink($upload_path . $file);
}
}
$uploadpage->save();
$sent = true;
// or better do a redirect here before showing thank you text.
} else {
// form incomplete or errors happened
// we remove files uploaded to temp dir
$files = explode("|",$form->get("myimages")->value);
foreach($files as $file){
if($file && file_exists($upload_path . $file)){
unlink($upload_path . $file);
}
}
// reset value before rendering the form again to prevent PW errors trying render files (as in admin)
$form->get("myimages")->value = ''; // reset field value
}
}
if(!$sent){
$content .= $form->render();
} else {
$content .= "<p>Form submission succeded. Thanks!</p>";
$content .= "<p>Page created: $uploadpage->url</p>";
}
.Inputfields,
.Inputfields li {
list-style: none;
margin: 1em 0;
padding: 0;
}
.Inputfields li label {
font-weight: bold;
}
.Inputfields li p {
margin: 0;
}
.Inputfields li p.description {
font-style: italic;
}
.Inputfields textarea,
.Inputfields .InputfieldMaxWidth {
width: 100%;
}
.Inputfields .InputfieldSubmit label {
display: none;
}
.ui-state-error-text {
color: red;
}
/* hide drag upload */
.AjaxUploadDropHere{
display: none;
}
span.ui-state-error{ color: red;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment