我正在尝试允许我的用户上传个人资料照片并将其显示在他们的个人资料中。
我创建了profilepic.php并在classes / User.php中添加了一个新函数
到目前为止,我正在尝试通过文本输入提交字符串,并将其与登录的当前user_id相关联。
'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表有4个字段:id,user_id(与users表中的id的关系),url,date users表有6个字段:id,username,password,salt,name,group
到目前为止,如果我导航到profilepic.php选择图片并单击上传,它将显示我的照片已上传的成功消息。我查看数据库,只有行id(auto_increment)和我的id(logged_in用户id)正在提交,而应该保存图像名称的“url”字段没有注册。