drhzn3911 2017-01-17 11:42
浏览 22

以编程方式更改Drupal个人资料图片

I want to programmatically change my profile picture on a Drupal website, but it change the picture only in one place, so it have the good image but we continue to see the old one each time we display it. What do I need to change please?

Here is my code :

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
chdir($path."/");
define('DRUPAL_ROOT', getcwd()); //the most important line
require_once './includes/bootstrap.inc';
$base_url = 'http://' . $_SERVER['HTTP_HOST'];
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

global $user;
$actual_user = user_load($user->uid);
$uri = $actual_user->picture->uri;

$valid_file = true;

if($_FILES['photo']['name'])
{
    //if no errors...
    if(!$_FILES['photo']['error'])
    {
        //now is the time to modify the future file name and validate the file
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
        {
            $valid_file = false;
            $message = 'Oops!  Your file\'s size is to large.';
        }

        //if the file has passed the test
        if($valid_file)
        {
            //move it to where we want it to be
            move_uploaded_file($_FILES['photo']['tmp_name'], $uri);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }else{
    //if there is an error...
        //set that to be the returned message
        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
    }
}

$file = (object) array(
    'uid' => 1,
    'uri' => $uri,
    'filemime' => file_get_mimetype($uri),
    'status' => 1,
  );

$file = file_copy($file, 'public://pictures');

$edit['field_profile_picture'] = $file;
user_save($actual_user, $edit);

echo $message;

?>

it get the picture from this form :

<form id="edit-profile-image" method="post" enctype="multipart/form-data">
    Your Photo: <input type="file" name="photo" size="25" />
    <input class="edit-profile-image-submit" type="submit" name="submit" value="Submit" />
</form>

With this ajax :

$("body").on('click','.edit-profile-image-submit',function(){

    var formData = new FormData($('#edit-profile-image')[0]);
    $.ajax({
        url: '/sites/all/themes/CEX/functions/edit-profile-image.php',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            alert(data)
        },
    });

    return false;
});

I hope I forgot nothing, if someone can help me I'm stuck on it... thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程