2011-11-23 3 views
3

맞춤 모듈에 이미지를 업로드하고 싶습니다. 필요한 경우 이미지를 영구히 저장하고 이름을 변경해야합니다. 나중에 내 양식이나 원하는 곳 어디에서나 이미지를 표시 할 수 있기를 원합니다.드루팔 (Drupal 7 - 이미지를 프로그래밍 방식으로 업로드

내 현재 코드 :

function my_module_name_new_form() 
{ 
.... 

$form['icon'] = array 
(
    '#type' => 'file', 
    '#title' => t('Icon'), 
    '#description' => t('Click "Chose File" to select an image to upload.') 
); 

.... 
} 

제출 후크에서 내가 가진 :

// Icon 
$filename = $form_state['values']['icon']; 
$dest = file_build_uri($filename); 
file_save_data('My data', $dest, FILE_EXISTS_RENAME); 

아무런 변화가 없습니다 ... 나는 그 어떻게 든 만들 중 어떤 드루팔 7.x의 호환 모듈을 찾을 수 없습니다 인생은 여기에서 더 쉽다.

솔루션 :

//---------------------------- 
// Icon 
//---------------------------- 
$dest_dir = file_default_scheme() . '://';// Note: file_directory_path() was removed in Drupal 7.x. // $dest_dir contains the destination directory for the file. 

$validators = array('file_validate_extensions' => array('jpg png gif')); 

//Save file 
if ($file = file_save_upload('icon', $validators, $dest_dir)) 
{ 
    $filename = $file->filename;   
    $file_content = file_get_contents($dest_dir.$filename); // Fatal error: Cannot access empty property in C:\xampp\htdocs\drupal\modules\custom\achievements\achievements.module on line 446 
} 
else 
{ 
    form_set_error('icon', 'Could not upload file.'); 
} 
// To display the image: // die('<IMG SRC="'.file_create_url($dest_dir.$filename).'"/>'); 
//---------------------------- 

답변

2

시도 :

$validators = array(
    'file_validate_extensions' => array('jpg png gif'), 
); 
    //Save file 
    if ($file = file_save_upload('icon', $validators, $your_file_destination)) { 
    $file_content = file_get_contents($file->filepath); 
    } 
    else{ 
    form_set_error('icon', 'Could not upload file.'); 
    } 
+0

나는 작동하지 않습니다. 첫 번째 게시물에서 편집 한 예를 참조하십시오. 나는 어떻게 든 입력 필드에서 파일을 가져와야하지만 그 파일은 이미 잘못되었다고 생각됩니다. – Napoleon

+0

file_save_data 호출을 제거하면 $ dest는 파일이 저장 될 디렉토리 여야합니다 (예 : 기본 파일 디렉토리를 설정하는 file_directory_path(), 보통 sites/default/files) – vgardner

+0

file_directory_path()가 제거되었습니다. D7에서하지만 대체품을 찾았습니다. 편집 : 그것은 지금 작동하는 것 같습니다. – Napoleon

5

나는 최근에이 작업을 수행했다. 여기 내가 결국에 끝난 것이다. Drupal 폼을 사용하지 않고 이미지 필드에 이미지를 저장하는 것입니다.

if(isset($_FILES['image'])){ 

    $node = node_load($nid); 

    //Get the uploaded file from the temp directory.    
    $image = file_get_contents($_FILES['image']['tmp_name']); 

    //Create the directory if it does not already exist, otherwise check the permissions 
    $directory = 'public://my-image-folder'; 
    file_prepare_directory($directory, FILE_CREATE_DIRECTORY); 

    //Saves a file to the specified destination and creates a database entry. 
    $file = file_save_data($image, 'public://my-image-folder/'.$_FILES['image']['name'], FILE_EXISTS_RENAME); 

    //Set the file status to permanent so it is not deleted in next cron run 
    $file->status = FILE_STATUS_PERMANENT; 
    file_save($file); 

    //Now we have a saved file and an object for it, just need to add it to the node's image field. 
    $node->field_my_image_field[$node->language][0] = (array)$file; 

    node_save($node); 
}