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