Problem
I have a custom form which creates a node. It's based off this link and uses the entity metadata wrapper to create the node.
The node created has an image field and you can use the form to upload an image on creation.
How can I set an upload directory for the image that's uploaded? At the moment they just default to my public folder.
Set-Up
There's more to this form, but these are the important parts. I declare a file field.
function postajob_form($form, &$form_state){
....
$form['file'] = array(
'#type' => 'file',
'#title' => t('Thumbnail/Advert Picture.'),
'#size' => 5,
);
}
I then submit that file using an entity metadata wrapper.
function postajob_form_submit($form, &$form_state) {
global $user;
$values = array(
'type' => 'job_advert',
'uid' => $user->uid,
'status' => 1,
'comment' => 1,
'promote' => 0,
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
if (array_key_exists('storage', $form_state)) {
//if is required incase no image is uploaded.
$ewrapper->field_req_thumbnail_school->file = $form_state['storage']['file'];
//field is unset now it's no longer needed.
unset($form_state['storage']['file']);
}
}
I tried changing the URI inside the form_state
, however this had no effect.
if (array_key_exists('storage', $form_state)) {
//altering the uri where the files are uploaded too
$imagefilename = $form_state['storage']['file']->filename;
$form_state['storage']['file']->uri = "public://user-upload/job-advert/$imagefilename";
//save the file
$ewrapper->field_req_thumbnail_school->file = $form_state['storage']['file'];
}
But this didn't effect anything.