Saya sedang menulis modul khusus, dan saya membutuhkannya untuk mengunggah gambar. Saya kesulitan menemukan dokumentasi yang bagus tentang ini, tapi saya rasa saya sudah dekat.
Apa yang saya lewatkan? $ file mengembalikan false dalam pengiriman formulir.
function mymodule_custom_content_block_form($form_state){
$form = array();
$form['custom_content_block_text'] = array(
'#type' => 'textarea',
'#title' => t('Block text'),
'#default_value' => variable_get('mymodule_custom_content_block_text'),
'#required' => true,
);
$form['custom_content_block_image'] = array(
'#type' => 'file',
'#name' => 'custom_content_block_image',
'#title' => t('Block image'),
'#size' => 40,
'#description' => t("Image should be less than 400 pixels wide and in JPG format."),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
function mymodule_custom_content_block_form_submit($form, &$form_state){
if(isset($form_state['values']['custom_content_block_image'])){
$validators = array('file_validate_extensions' => array('jpg jpeg'));
$file = file_save_upload('custom_content_block_image', $validators, 'public://');
if($file == false){
drupal_set_message(t("Error saving image."), $type = "error", $repeat = false);
}
else{
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
}
}
variable_set('mymodule_custom_content_block_text', $form_state['values']['custom_content_block_text']);
drupal_set_message(t('Custom Content Block has been updated.'));
}