I'm trying to implement a front-end image upload form for user profile pictures, to a field called 'profile_picture'. The code is based on the various examples found around these forums but isn't yet 100% right.
The form successfully submits the image to the server and updates the field in the dashboard. When you submit the firm, the page reloads and uploads the image but the template still loads the old image path (now broken path as the old image has been removed).
I can only get the new image to show if I hit enter in the address bar forcing the page to reload.
Any ideas? Is it a caching issue, or something to do with the order of the script? It makes no difference if I call the image after the upload form.
<?php
//Display current user image
$userImg = $user->profile_picture->first();
echo '<img src="'.$userImg->url.'">';
$upload_path = $config->paths->assets . "files/avatar_uploads/";
$f = new WireUpload('userimage');
$f->setMaxFiles(1);
$f->setMaxFileSize(1*1024*1024);
$f->setOverwrite(true);
$f->setDestinationPath($upload_path);
$f->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));
if($input->post->form_submit) {
if(!is_dir($upload_path)) {
if(!wireMkdir($upload_path)) throw new WireException("No upload path!");
}
$files = $f->execute();
if ($f->getErrors()) {
foreach($files as $filename) @unlink($upload_path . $filename);
foreach($f->getErrors() as $e) echo $e;
} else {
$user->of(false);
$user->profile_picture->removeAll(); // wirearray (line added by @horst: explanation is three posts beneath)
$user->profile_picture = $upload_path . $files[0];
$user->save();
$user->of(true);
@unlink($upload_path . $files[0]);
}
}
?>
<form class="forum-form" accept-charset="utf-8" action="./" method="post" enctype="multipart/form-data" >
<input type="file" id="attach" name="userimage" accept="image/jpg,image/jpeg,image/gif,image/png" />
<input type="submit" name="form_submit" value="Submit"/>
</form>