Ben Posted April 5, 2012 Share Posted April 5, 2012 I've been working on an automating an import of some pages. For all intents and purposes, it's basically a form submission to a module process that creates/updates a page of a certain template. For the literal minded, I'm importing an existing news archive (headline, story, byline, image) by scripting the form submission. So far, everything has worked great, and I can update the text/textarea fields without any issues, but I'm not able to figure out how to attach an image in the $_FILES superglobal to the $page object. I suspect my thinking on how to do this is backwards, and there may be an API method that handles some of the lifting - but I don't mind getting my hands dirty either if I need to code it closer to the metal. I've starting to look at the FieldtypeImage source and the inheritance thereof, but I'm starting to find myself out in the weeds. Does anyone know of a way to affix an uploaded image (outside of the page admin) to a $page object? The input handling of the submission currently looks a little like this: foreach($page->template->fields as $template_field){ $field_name = $template_field->name; $field_class = $template_field->type->className; $update_value = $this->input->post($field_name); switch($field_class){ case 'TitleFieldtype': case 'TextareaFieldtype': $page->{$field_name} = $update_value; break; case 'ImageFieldtype': // <----- the confounder // not sure what to do here if the field // if name is represented in $_FILES break; ... } ... } EDIT: Simplified code block. Link to comment Share on other sites More sharing options...
Ben Posted April 5, 2012 Author Share Posted April 5, 2012 My suspicions were correct, there's no need to do anything crazy. The API handles it fine - $page->images->add() was what I was looking for. Part of the frustration was that I mislabeled the switch case, it should have been 'FieldtypeImage' as opposed to 'ImageFieldtype'. Now if I could only control the name of the phpRANDOM.tmp filename being created, but that's a different matter. For anyone looking for a solution, here's an updated snippet: foreach($page->template->fields as $template_field){ $field_name = $template_field->name; $field_class = $template_field->type->className; $update_value = $this->input->post($field_name); switch($field_class){ case 'TitleFieldtype': case 'TextareaFieldtype': $page->{$field_name} = $update_value; break; case 'FieldtypeImage': if (array_key_exists($field_name,$_FILES)){ $img_path = $_FILES[$field_name]['tmp_name']; $img_name = $_FILES[$field_name]['name']; // $_FILES array singular vs. multiple // normalize by converting single to array prior to looping. // http://php.net/manual/en/reserved.variables.files.php if (!(is_array($img_path))){ $img_path = array($img_path); $img_name = array($img_name); } for($i=0; $i < count($img_path); $i++){ $page->images->add($img_path[$i]); // TODO, change image name from phpXYZ.tmp } } break; ... } ... } 2 Link to comment Share on other sites More sharing options...
ryan Posted April 9, 2012 Share Posted April 9, 2012 Another approach you could use would be to use PW's WireUpload class: $ul = new WireUpload($field_name); The rest here: 1 Link to comment Share on other sites More sharing options...
Ben Posted April 10, 2012 Author Share Posted April 10, 2012 Ryan, this is exactly what I was looking for. Thank you. 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