我刚开始学习 Drupal 7,想使用自定义表单上传文件。但是当我上传文件时,它会产生以下错误。
这是我的代码。
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.'));
}
}
我不知道我在哪里犯错了!!!