I'm trying to allow my users to upload a profile picture and display it on their profile.
I've created profilepic.php and added a new function in classes/User.php
So far I'm trying to submit a string via text input and associating it with the current user_id that is logged in.
The function inside 'classes/User.php'
public function uploadPhoto($fields = array()) {
$photo = $this->_db->insert('userPhotos', array('user_id' => $this->data()->id));
if(!$photo) {
throw new Exception('There was a problem creating your account.');
}
}
ProfilePic.php
<?php
require_once 'core/init.php';
include('includes/header.php');
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'url' => array(
'required' => false
)
));
if(!$validation->passed()) {
try {
/* $user->uploadPhoto(array(
'url' => Input::get('url'),
)); */
//This is the directory where images will be saved
$target = 'var/www/app/img';
$target = $target . basename($_FILES['url']['name']);
$pic= ($_FILES['url']['name']);
//Writes the information to the database
//$this->data()->query('INSERT INTO userPhotos (photo) VALUES (‘$pic’)');
$user->uploadPhoto(array(
'url' => Input::get($pic)
));
//Writes the photo to the server
if(move_uploaded_file($_FILES['url']['tmp_name'], $target))
{
echo 'Ok' . basename( $_FILES['uploadedfile']['name']);
}
else {
//Gives and error if its not
echo 'Sorry, there was a problem uploading your file.';
}
Session::flash('home', 'Your profile photo has been uploaded.');
Redirect::to('index.php');
} catch(Exception $e) {
die($e->getMessage());
}
} else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
}
?>
<div class="container">
<div class="row row-offcanvas row-offcanvas-right">
<div class="col-xs-12 col-sm-9">
<p class="pull-right visible-xs">
<button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas">Toggle nav</button>
</p>
<div class="jumbotron">
<h1>Edit Profile</h1>
<p>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="url">URL</label>
<input type="file" id="url" name="url" value="">
<p class="help-block">Example block-level help text here.</p>
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
</p>
</div>
<div class="row">
</div><!--/row-->
</div><!--/span-->
<?php include 'includes/sidebar.php'; ?>
</div><!--/row-->
<hr>
<?php include('includes/footer.php'); ?>
userPhotos table has 4 fields: id, user_id (relationship with id in users table), url, date users table has 6 fields: id, username, password, salt, name, group
So far if I navigate to profilepic.php select a picture and click upload, it will display a success message that my photo has been uploaded. I look into the database, only the row id (auto_increment) and my id (logged_in user id) are being submitted while the "url" field which is supposed to hold the name of the image is not registering.