Drupal 7 以编程方式保存用户图片

2023-06-23php开发问题
1

本文介绍了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);
}
?>

在sites/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(临时)

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

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

我猜 file_managed 表应该存储最终文件(在站点/默认/图片中)而不是原始文件信息,并且用户表也应该链接到那个.

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/ ?

推荐答案

尝试将以下内容添加到您的代码中:

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 ------------------

如果您想将文件的副本保存在新位置,可以将上面的第三行替换为

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 以编程方式保存用户图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17