$file = file_save_upload('file', array( 'file_validate_is_image' => array(), 'file_validate_extensions' => array('png gif jpg jpeg'), )); if ($file) { if ($file = file_move($file, 'public://')) { $form_state['values']['file'] = $file; } else { form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.')); } } else { form_set_error('file', t('No file was uploaded.')); } }
under hook_submit:
$file=$form_state['values']['file']; unset($form_state['values']['file']); $file->status = FILE_STATUS_PERMANENT; file_save($file); drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
function testform_validate($form, &$form_state) { $file = file_save_upload('file', array( 'file_validate_is_image' => array(), 'file_validate_extensions' => array('png gif jpg jpeg'), )); if ($file) { if ($file = file_move($file, 'public://')) { $form_state['values']['file'] = $file; } else { form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.')); } } else { form_set_error('file', t('No file was uploaded.')); } }
function testform_submit($form, &$form_state) { $file=$form_state['values']['file']; unset($form_state['values']['file']); $file->status = FILE_STATUS_PERMANENT; file_save($file); drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename))); } ?>
Comments
Can I recommend looking at
Can I recommend looking at the examples module. If you look at the last tutorial on forms, it shows how to upload a file, validate, and save.
form_example_tutorial_10
Hi I tried the code in
Hi I tried the code in form_example_tutorial_10
the file was uploaded in the "files" folder inside default.But this error is showing up:
Notice: Undefined property: stdClass::$uri in file_save() (line 575 of includes\file.inc).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1334752869 ) in drupal_write_record() (line 6776 of includes\common.inc).
Can you please advice?
Can you show your code?
Can you show your code?
From the error message it seems you are putting the uri into file_save(), but you should be using the file object instead.
Here it is:under hook_menu:
Here it is:
under hook_menu:
$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
under hook_validation:
$file = file_save_upload('file', array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('png gif jpg jpeg'),
));
if ($file) {
if ($file = file_move($file, 'public://')) {
$form_state['values']['file'] = $file;
}
else {
form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
}
}
else {
form_set_error('file', t('No file was uploaded.'));
}
}
under hook_submit:
$file=$form_state['values']['file'];
unset($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
Works fine for
Works fine for me.
<?php
function test_menu() {
$items = array();
$items['test'] = array(
'title' => 'test',
'page callback' => 'drupal_get_form',
'page arguments' => array('testform'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function testform($form, &$form_state) {
$form = array();
$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function testform_validate($form, &$form_state) {
$file = file_save_upload('file', array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('png gif jpg jpeg'),
));
if ($file) {
if ($file = file_move($file, 'public://')) {
$form_state['values']['file'] = $file;
}
else {
form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
}
}
else {
form_set_error('file', t('No file was uploaded.'));
}
}
function testform_submit($form, &$form_state) {
$file=$form_state['values']['file'];
unset($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
}
?>
Thanks It worked !
Thanks It worked !
Multiple Files to Multiple Nodes
<?php
function ajf_multiple_portfolio_images_form($form, &$form_state) {
$form = array();
$form['photos'] = array(
'#type' => 'file',
'#name' => 'files[]',
'#title' => t('Upload some photos'),
'#description' => t('JPG\'s, GIF\'s, and PNG\'s only, 10MB Max Size'),
'#attributes' => array('multiple' => 'multiple'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
);
return $form;
}
function ajf_multiple_portfolio_images_form_validate($form, &$form_state) {
$num_files = count($_FILES['files']['name']);
for ($i = 0; $i < $num_files; $i++) {
$file = file_save_upload($i, array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('png gif jpg jpeg'),
));
if ($file) {
if ($file = file_move($file, 'public://')) {
$form_state['values']['photos'][$i] = $file;
}
else {
form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
}
}
else {
form_set_error('file', t('No file was uploaded.'));
}
}
}
function ajf_multiple_portfolio_images_form_submit($form, &$form_state) {
$count = count($form_state['values']['photos']);
if (is_array($form_state['values']['photos'])) {
foreach ($form_state['values']['photos'] as $file) {
global $user;
$node = new stdClass;
$file_info = image_get_info($file->uri);
$node->type = 'photo';
$node->title = $file->filename;
$node->field_image[LANGUAGE_NONE][0]['fid'] = $file->fid;
$node->field_image[LANGUAGE_NONE][0]['alt'] = $file->filename;
$node->field_image[LANGUAGE_NONE][0]['title'] = $file->filename;
$node->field_image[LANGUAGE_NONE][0]['width'] = $file_info['width'];
$node->field_image[LANGUAGE_NONE][0]['height'] = $file_info['height'];
$node->field_image[LANGUAGE_NONE][0]['uid'] = $file->uid;
$node->field_image[LANGUAGE_NONE][0]['filename'] = $file->filename;
$node->field_image[LANGUAGE_NONE][0]['uri'] = $file->uri;
$node->field_image[LANGUAGE_NONE][0]['filemime'] = $file->filemime;
$node->field_image[LANGUAGE_NONE][0]['filesize'] = $file->filesize;
$node->field_image[LANGUAGE_NONE][0]['status'] = '1';
$node->field_image[LANGUAGE_NONE][0]['timestamp'] = $file->timestamp;
$node->field_image[LANGUAGE_NONE][0]['rdf_mapping'] = array();
$node->uid = $user->uid;
$node->status = 1;
$node->active = 1;
node_save($node);
}
}
drupal_set_message(t("You have uploaded $count photos"));
}
?>