<tfoot id='zvSCi'></tfoot>
<legend id='zvSCi'><style id='zvSCi'><dir id='zvSCi'><q id='zvSCi'></q></dir></style></legend>

<small id='zvSCi'></small><noframes id='zvSCi'>

      <bdo id='zvSCi'></bdo><ul id='zvSCi'></ul>

      <i id='zvSCi'><tr id='zvSCi'><dt id='zvSCi'><q id='zvSCi'><span id='zvSCi'><b id='zvSCi'><form id='zvSCi'><ins id='zvSCi'></ins><ul id='zvSCi'></ul><sub id='zvSCi'></sub></form><legend id='zvSCi'></legend><bdo id='zvSCi'><pre id='zvSCi'><center id='zvSCi'></center></pre></bdo></b><th id='zvSCi'></th></span></q></dt></tr></i><div id='zvSCi'><tfoot id='zvSCi'></tfoot><dl id='zvSCi'><fieldset id='zvSCi'></fieldset></dl></div>
      1. Drupal 7 以编程方式保存用户图片

        时间:2023-06-23
        • <bdo id='ONFx4'></bdo><ul id='ONFx4'></ul>

              <legend id='ONFx4'><style id='ONFx4'><dir id='ONFx4'><q id='ONFx4'></q></dir></style></legend>
                <tbody id='ONFx4'></tbody>
              <i id='ONFx4'><tr id='ONFx4'><dt id='ONFx4'><q id='ONFx4'><span id='ONFx4'><b id='ONFx4'><form id='ONFx4'><ins id='ONFx4'></ins><ul id='ONFx4'></ul><sub id='ONFx4'></sub></form><legend id='ONFx4'></legend><bdo id='ONFx4'><pre id='ONFx4'><center id='ONFx4'></center></pre></bdo></b><th id='ONFx4'></th></span></q></dt></tr></i><div id='ONFx4'><tfoot id='ONFx4'></tfoot><dl id='ONFx4'><fieldset id='ONFx4'></fieldset></dl></div>

                <small id='ONFx4'></small><noframes id='ONFx4'>

                  <tfoot id='ONFx4'></tfoot>

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

                  上一篇:Drupal 安装 PDOException 下一篇:Drupal 7 架构中的日期时间类型

                  相关文章

                  <tfoot id='Ipfl5'></tfoot>
                • <i id='Ipfl5'><tr id='Ipfl5'><dt id='Ipfl5'><q id='Ipfl5'><span id='Ipfl5'><b id='Ipfl5'><form id='Ipfl5'><ins id='Ipfl5'></ins><ul id='Ipfl5'></ul><sub id='Ipfl5'></sub></form><legend id='Ipfl5'></legend><bdo id='Ipfl5'><pre id='Ipfl5'><center id='Ipfl5'></center></pre></bdo></b><th id='Ipfl5'></th></span></q></dt></tr></i><div id='Ipfl5'><tfoot id='Ipfl5'></tfoot><dl id='Ipfl5'><fieldset id='Ipfl5'></fieldset></dl></div>

                  1. <small id='Ipfl5'></small><noframes id='Ipfl5'>

                      <bdo id='Ipfl5'></bdo><ul id='Ipfl5'></ul>
                      <legend id='Ipfl5'><style id='Ipfl5'><dir id='Ipfl5'><q id='Ipfl5'></q></dir></style></legend>