Reputation: 979
I have just started learning Drupal 7, and want to upload a file using a custom form. But when I upload file it generates below error.
Here is my code.
function custom_form_form($form,&$form_state) {
$form = array();
$form['photos'] = array(
'#title' => t('Image'),
'#type' => 'file',
'#name' => 'files[photos]',
);
$form['submit'] = array(
'#value' => 'Submit',
'#type' => 'submit',
'#name' => 'submit',
);
$form['#submit'][] = 'custom_submit_function';
return $form;
}
function custom_submit_function($form, &$form_state){
$validators = array(
'file_validate_extensions' => array('jpg png gif'),
);
//Save file
$file_destination = "public://Photos/";
$file = file_save_upload('photos', $validators, $file_destination,FILE_EXISTS_RENAME);
if(isset($file->uri)){ //if you need this file to be not temporary
$file->status = 1;
file_save($file);
}
if ($file) {
$file_content = file_get_contents($file->filepath);
echo $file_content;
}
else{
print_r(form_set_error('photos', 'Could not upload file.'));
}
}
I don't know where I am making a mistake !!!
Upvotes: 0
Views: 517
Reputation: 212
If you look at the definition of file_save_upload()
in the Drupal 7 API, it seems that the 'file' object returned by that function have no 'filepath' member. You may want to try something like $file_content = file_get_contents(file_create_url($file->uri));
instead.
Upvotes: 1