Image Drupal 7-以编程方式上载图像

Image Drupal 7-以编程方式上载图像,image,file,drupal,upload,Image,File,Drupal,Upload,我想上传一个自定义模块中的图像。如果需要,必须永久保存并重命名图像。稍后,我希望能够在表单上的某个位置或任何我想要的地方显示图像 我当前的代码: function my_module_name_new_form() { .... $form['icon'] = array ( '#type' => 'file', '#title' => t('Icon'), '#description' => t('Click "Chose File" to sel

我想上传一个自定义模块中的图像。如果需要,必须永久保存并重命名图像。稍后,我希望能够在表单上的某个位置或任何我想要的地方显示图像

我当前的代码:

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).'"/>');
//----------------------------  
//----------------------------
//图标
//----------------------------  
$dest_dir=file_default_scheme()。:/';//注意:Drupal 7.x中的文件_directory_path()已被删除$dest_dir包含文件的目标目录。
$validators=array('file\u validate\u extensions'=>array('jpg png gif');
//保存文件
如果($file=file\u save\u upload($icon',$validators,$dest\u dir))
{
$filename=$file->filename;
$file_content=file_get_contents($dest_dir.$filename);//致命错误:无法访问第446行C:\xampp\htdocs\drupal\modules\custom\acgregations\acgregations.module中的空属性
}
其他的
{
表单设置错误('图标','无法上载文件');
}
//显示图像://die(“”);
//----------------------------  
试试:


我最近不得不这么做。这就是我的结局。这是在不使用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);
}

我不能让它工作。请参见我在第一篇文章中编辑的示例。我必须以某种方式从输入字段中获取文件,但该字段似乎已经出错。请删除file\u save\u data调用,$dest应该是文件将保存到的目录(例如,file\u directory\u path(),您可以在其中设置默认文件目录,通常是sites/default/files)file\u directory\u path()在D7中被移除,但我找到了一个替代品。编辑:现在似乎可以工作了。验证不应该通过FAPI渲染数组中的回调函数进行吗?
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);
}