我无法检索我的图片以更新用户的个人资料。我的上传图片成功了。它可以在'/ uploads'文件夹和数据库中看到。如何在个人资料页面上显示它?我还有一个默认图片如何替换它?
Memberlogincontroller
//The link to my upload page
public function editphoto($id){
$data = array(
"actionupdate"=>base_url('/index.php/Memberlogincontroller/updatephoto/'.$id),
"data" => $this->db->get_where('member',array('id'=>$id))
);
$this->load->view('member/upload', $data);
}
public function getMember($id)
{
if ( (int)$id < 1)//$id is not an integer
{
redirect('Memberlogincontroller/member_view', 'refresh');//no valid uri segment
}
else
{
$this->load->model('Memberloginmodel');
$memberinfo['memberinfo'] = $this->Memberloginmodel->getMember($id);
if ( !$memberinfo['memberinfo'])//returned FALSE from model
{
redirect('Memberlogincontroller/member_view', 'refresh');//no $id in DB
}
else
{
$this->load->view('member/userprofile',$memberinfo);
}
}
}
//from Dropzone
public function updatephoto($id){
if ( (int)$id < 1)//$id is not an integer
{
redirect('Memberlogincontroller/member_view', 'refresh');
}
else
{
$this->load->library('form_validation');//it's good to autoload it
if (empty($_FILES['file']['name']))
{
$this->form_validation->set_rules('image', 'Image', 'required');
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('member/userprofile');
}
else{
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$image = $_FILES['file']['name'];
$targetPath = getcwd() . '/uploads/';
$targetFile = $targetPath . $image ;
move_uploaded_file($tempFile, $targetFile);
$data = array(
'image' => '/uploads/'.$image,
);
$this->db->update('member',$data,array('id'=>$id));
$this->session->set_userdata($data); //i used this when i get to update other but in image i cant
redirect('index.php/Memberlogincontroller/getMember/'.$id, 'refresh');
}
}
}
}
Memberloginmodel
public function getMember($id)
{
$this->db->where('id', $id);
$query = $this->db->get('member');
if ($query->num_rows() !== 1)
{
return FALSE;
}
return $query->row();//just single member data
}
上传页面
<html>
<head>
<title>Upload Photo</title>
<?php include 'header_member.php'; ?>
<link href="assets/css/font-awesome.css" rel="stylesheet" />
<link href="<?php echo base_url(); ?>assets/css/dropzone.css" type="text/css" rel="stylesheet" />
<script src="<?php echo base_url(); ?>assets/js/dropzone.js"></script>
<script src="<?php echo base_url(); ?>assets/js/dropzone.min.js"></script>
</head>
<script>
Dropzone.options.myDropzone = {
// Prevents Dropzone from uploading dropped files immediately
autoProcessQueue: false,
init: function() {
var submitButton = document.querySelector("#submit-all")
myDropzone = this; // closure
submitButton.addEventListener("click", function() {
myDropzone.processQueue(); // Tell Dropzone to process all queued files.
});
// You might want to show the submit button only when
// files are dropped here:
this.on("addedfile", function() {
// Show submit button here and/or inform user to click it.
});
}
};
</script>
<body>
<div class="container">
<br>
<center><h3 class="description">UPLOAD PROFILE PICTURE</h3></center>
<br>
</div>
<form action="<?php echo $actionupdate;?>" name="image" value="<?php echo $this->session->userdata('image'); ?>" class="dropzone" enctype="multipart/form- data" id="my-dropzone">
<input type="submit" value="update">
</form>
</body>
</html>
UserProfile页面(VIEW)
<a href="#"><img height="200px" width="200px" src="<php echo base_url();?>uploads/no-avatar.jpg"></a>
(How to replace this default avatar to an updated one?)
below wont work
<a href="#"><img height="200px" width="200px"src="<?php echo $this->session->userdata('image'); ?>"> --> done changing to base_url()
上传视图
<html>
<head>
<title>Upload Photo</title>
<?php include 'header_member.php'; ?>
<link href="assets/css/font-awesome.css" rel="stylesheet" />
<link href="<?php echo base_url(); ?>assets/css/dropzone.css" type="text/css" rel="stylesheet" />
<script src="<?php echo base_url(); ?>assets/js/dropzone.js"></script>
<script src="<?php echo base_url(); ?>assets/js/dropzone.min.js"></script>
</head>
<script>
Dropzone.options.myDropzone = {
// Prevents Dropzone from uploading dropped files immediately
autoProcessQueue: false,
acceptedFiles: ".png,.jpg,.gif,.bmp,.jpeg",
maxFilesize: 1,
parallelUploads: 10,
addRemoveLinks: true,
init: function() {
var submitButton = document.querySelector("#submit-all")
myDropzone = this; // closure
submitButton.addEventListener("click", function() {
myDropzone.processQueue();
// autoProcessQueue: true// Tell Dropzone to process all queued files.
});
// You might want to show the submit button only when
// files are dropped here:
this.on("addedfile", function() {
// Show submit button here and/or inform user to click it.
});
}};
</script>
<body>
<div class="container">
<br>
<center><h3 class="description">UPLOAD PROFILE PICTURE</h3></center>
<br>
</div>
<!--<form action="<php echo site_url('/dropzone/upload'); ?>" class="dropzone" >-->
<form action="<?php echo $actionupdate;?>"method="POST" id="my-dropzone" class="dropzone" name="image" enctype="multipart/form-data" >
<input type="hidden" name="hidden" value="<?php echo $this->session->userdata('id'); ?>"/>
<button id="submit-all" type="submit" value="update">Submit</button>
</form>
</body>
</html>