2011-11-23 12 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); 

何も起こりません...私は何とか作るのいずれかの任意のDrupal 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

私はそれを動作させることはできません。最初の記事で編集した例を見てください。私は何とか入力フィールドからファイルを取得する必要がありますが、1つは間違っているようです。 – 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); 
} 
関連する問題