I am developing a custom module in which I want to:
- Upload an image through a form.
- Move image from default folder to my desired folder.
- And finally display the image under the same form (below the submit button).
I've achieved the first goal by implementing hook_menu and hook_form but I am stucked very badly in the remaining two goals for last 3 days. Whenever I try to move uploaded image from default folder I get the error message of "Invalid location" and for the third point I didn't understand what to do? I want when the user selects the image and submits the form the image is displayed on the same page under the submit button. Any help would be really appreciated. Here's my code:-
function create_ad_form($form, &$form_submit)
{
...
$form['image_file'] = array(
'#title' => t('Upload Banner:'),
'#type' => 'file'
);
$form['#attributes']['enctype'] = 'multipart/form-data';
...
}
function create_ad_form_submit($form, &$form_state)
{
$module=drupal_get_path('module', 'create_ad');
$validators = array();
$file = file_save_upload('image_file', $validators,"public://",FILE_EXISTS_RENAME);
if ($file)
{
$file->status=FILE_STATUS_PERMANENT;
file_save($file);
$result = file_unmanaged_copy($file, $module, FILE_EXISTS_RENAME);
if ($result == 1)
{
}
else
{
drupal_set_message('Couldn\'t copy file: '.$file->name);
}
}
else
{
form_set_error('create_ad', t("Failed to save the file."));
}
}