파일 업로드, file
2515 단어 File
function form_example_tutorial_10($form_state) {
// If you are familiar with how browsers handle files, you know that
// enctype="multipart/form-data" is required. Drupal takes care of that, so
// you don't need to include it yourself.
$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
// '#default_value' =>
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Validate handler for form_example_tutorial_10().
*/
function form_example_tutorial_10_validate($form, &$form_state) {
$file = file_save_upload('file', array(
'file_validate_is_image' => array(), // Validates file is really an image.
'file_validate_extensions' => array('png gif jpg jpeg'), // Validate extensions.
));
// If the file passed validation:
if ($file) {
// Move the file, into the Drupal file system
if ($file = file_move($file, 'public://')) {
// Save the file for use in the submit handler.
$form_state['storage']['file'] = $file;
}
else {
form_set_error('file', t('Failed to write the uploaded file to the site\'s file folder.'));
}
}
else {
form_set_error('file', t('No file was uploaded.'));
}
}
/**
* Submit handler for form_example_tutorial_10().
*/
function form_example_tutorial_10_submit($form, &$form_state) {
$file = $form_state['storage']['file'];
// We are done with the file, remove it from storage.
unset($form_state['storage']['file']);
// Make the storage of the file permanent
$file->status = FILE_STATUS_PERMANENT;
// Save file status.
file_save($file);
// Set a response to the user.
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java에서 InputStream, String, File 간의 상호 전환 비교InputStream, String, File 상호 전환 1. String --> InputStream 2. InputStream --> String 오늘 인터넷에서 또 다른 방법을 보았는데, 특별히 가지고 와서 공...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.