Drupal 7以编程方式保存用户图片 [英] Drupal 7 save user picture programmatically

查看:158
本文介绍了Drupal 7以编程方式保存用户图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个脚本 http://d.danylevskyi.com/node/7 我以前用作下列代码的起始点。

I have found this script http://d.danylevskyi.com/node/7 which I have used as a starter for the below code.

目标是能够保存用户图片:

The goal is to be able to save a user picture:

<?php
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$uid = 99;
$account = user_load($uid);

// get image information
$image_path = 'public://avatars/upload/b8f1e69e83aa12cdd3d2babfbcd1fe27_4.gif';
$image_info = image_get_info($image_path);

// create file object
$file = new StdClass();
$file->uid = $uid;
$file->uri = $image_path;
$file->filemime = $image_info['mime_type'];
$file->status = 0; // Yes! Set status to 0 in order to save temporary file.
$file->filesize = $image_info['file_size'];

// standard Drupal validators for user pictures
$validators = array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
    'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);

// here all the magic :) 
$errors = file_validate($file, $validators);
if (empty($errors)) {

    file_save($file);
    $edit['picture'] = $file;
    user_save($account, $edit);
}
?>

在site / default / files / pictures /中创建了一张图片,名称为picture-99-1362753611 .gif

A picture is created in sites/default/files/pictures/ with the name picture-99-1362753611.gif

file_managed表中的一切似乎都是正确的,除了:

Everything seems correct in the file_managed table except that:


  1. 文件名字段为空

  2. uri字段显示为public://avatars/upload/b8f1e69e83aa12cdd3d2babfbcd1fe27_4.gif

  3. 状态字段设置为0(临时)

用户表中的图片字段使用上述条目的fid进行更新。

The picture field in the users table gets updated with the fid of the above mentioned entry.

我猜想file_managed表应存储最终文件(在sites / default /图片中)而不是原始文件信息,用户表也应链接到该文件。

I would guess that the file_managed table should store the final file (in sites/default/pictures) instead of the original file info and that the users table should link to the one too.

任何想法如何才能实现?我是Drupal API的新手。谢谢。

Any idea how I can achieve that? I am quite new to the Drupal API. Thank you.

编辑:

我明白,文件到file_save和user_save函数。但是哪一个实际上在sites / default / pictures /?中创建文件?

I understand that I am giving the original file to the file_save and user_save functions. But which one actually creates the file in sites/default/pictures/ ?

推荐答案

尝试将以下代码添加到您的代码中: p>

Try adding the following to your code:

$file->filename = drupal_basename($image_path);
$file->status   = FILE_STATUS_PERMANENT;
$file = file_save($file);  // Use this instead of your current file_save

有帮助吗?

------------------ EDIT ------------------

------------------ EDIT ------------------

如果要将文件的副本保存在新位置,您可以用以下代码替换上述第三行:

If you want to save a copy of the file in a new location, you can replace the third line above with something like

// Save the file to the root of the files directory.
  $file = file_copy($file, 'public://');

这篇关于Drupal 7以编程方式保存用户图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆