I have set up my php view to select the user's uploaded avatar in a table named 'user', the row is 'avatar':
<img src="<?=$user->avatar; ?>" class='profile'>
I am trying to set up a link to a default image if there is no image uploaded:
<? if(empty($avatar)) $avatar = "/uploads/default.gif"; ?>
I can't seem to get this to work - any help would be appreciated. Many thanks.
I've updated to the below but it seems to just default to the default.gif
<?php if(isset($avatar)): ?>
<img src="<?=$user->avatar; ?>" class='profile'>
<?php else: ?>
<img src='/uploads/default.gif'>
<div id="prompt">Upload your own avatar image</div>
<?php endif; ?>
Process Image Upload - in users controller:
public function picture() {
# Sanitize Data Entry
$_POST = DB::instance(DB_NAME)->sanitize($_POST);
# Upload Image
if ($_FILES['avatar']['error'] == 0) {
$avatar = Upload::upload($_FILES, "/uploads/avatars/", array('jpg', 'jpeg', 'gif', 'png', 'JPG', 'JPEG', 'GIF', 'PNG'), $this->user->user_id);
if($avatar == 'Invalid file type.') {
# Error
Router::redirect('/users/profile/error');
}
else {
# Upload Image
$data = Array('avatar' => $avatar);
DB::instance(DB_NAME)->update('users', $data, 'WHERE user_id = '.$this->user->user_id);
# Resize and Save Image
$imageObj = new Image($_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/'.$avatar);
$imageObj->resize(150,150,'crop');
$imageObj->save_image($_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/'.$avatar);
}
}
else {
# Error
Router::redirect("/users/profile/error");
}
# Send to Profile Page
Router::redirect('/users/profile');
}