I have a from with two file uploads: one for image upload and another for video uploading. Now the problem is that, if I upload invalid file types for either video or image, it is showing the same error
The filetype you are attempting to upload is not allowed.
What I need is to customize the error so that for video it will be like
The video you are attempting to upload is not allowed.
How can I make this possible? I am sharing the section of code for video and image.
View Page Code:
<tr>
<td>
<label for="video_type">Video Genre <span class="required">*</span></label>
</td>
<td>
<?php
$array['']="Select";
foreach($genre as $row)
{
$array[$row->id] = $row->genre;
}
echo form_dropdown('video_type',$array, set_value('video_type'));
?>
</td>
<td>
<?php echo form_error('video_type'); ?>
</td>
</tr>
<tr>
<td>
<label for="uploader_id">Upload Video <span class="required">*</span></label>
</td>
<td>
<input type="file" name="video" class="input" value="<?php echo set_value('video'); ?>" />
</td>
<td>
<?php echo form_error('video'); ?>
</td>
</tr>
Controller:
function index()
{
$data['dropdown'] = lang_dropdown(); // multilanguage dropdown list
$data['language']=$this->session->userdata('site_language');
$userid=$this->tank_auth->get_user_id();
if (empty($_FILES['thumb_image']['name']))
{
$this->form_validation->set_rules('thumb_image', 'Thumb Image', 'required|max_length[255]');
}
if (empty($_FILES['video']['name']))
{
$this->form_validation->set_rules('video', 'Video', 'required');
}
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
$data['page']='video/upload';
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('layout/template',$data);
}
else // passed validation proceed to post success logic
{
$file=$_FILES['video']['name'];
$thumbfile=$_FILES['thumb_image']['name'];
$form_data = array(
'videolink' => time().$file,
'videothumbnail' => time().$thumbfile,
'uploaderid' => $userid,
'uploaderid' => set_value('1'),
);
$config['upload_path'] = './secure/';
$config['allowed_types'] = 'mpeg|mp4|mpg|mpe|qt|mov|avi|movie|wmv|flv|3gp|mkv|dv|m4u|m4v|mxu|rv';
$extn = end(explode(".", $_FILES['video']['name']));
$config['file_name'] = time().$_FILES['video']['name'].'.'.$extn;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$data['upload_data'] = '';
if (!$this->upload->do_upload('video'))
{
$data['msg'] = $this->upload->display_errors();
$this->load->view('layout/template',$data);
}
else
{
$data['upload_data'] = $this->upload->data();
if($_FILES['thumb_image']['name']!="")
{
$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'].'.'.$ext;
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
if (!$this->upload->do_upload('thumb_image'))
{
$data['msg'] = $this->upload->display_errors();
$this->load->view('layout/template',$data);
}
}
if ($this->Video_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
$data['successmsg']="Successfully uploaded the video";
}
else
{
echo 'An error occurred saving your information. Please try again later';
}
$this->form_validation->unset_field_data();
$this->load->view('layout/template',$data);
}
}
}
try this
if (empty($_FILES['video']['name']))
{
$this->form_validation->set_message('required', 'video name incorrect'); //put this line above this statement
$this->form_validation->set_rules('video', 'Video', 'required');
}
Related
I have an issue updating images for my posts in CodeIgniter.
I have no probleme creating posts with images, but when I try to update a post, post changed info are updated bu not the image.
I have tried several posibilities from different tutorials without success
Thanks in advance
Here is my code:
controller
public function create(){
if(!$this->session->userdata('logged_in'))
{
redirect('users/login');
}
$data['title'] = 'Create Post';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
else
{
$slug = url_title($this->input->post('title'));
$post_image = $this->upload_image();
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'post_image' => $post_image
);
//$this->post_model->create_post($post_image);
$this->post_model->create_post($data);
// Set message
$this->session->set_flashdata('post_created', 'Your post has been created successfully!');
redirect('posts');
}
}
public function edit($slug)
{
$data['post'] = $this->post_model->get_posts($slug);
// Check if logged user has created this post
if($this->session->userdata('user_id') != $this->post_model->get_posts($slug)['user_id'])
{
redirect('posts');
}
$data['categories'] = $this->category_model->get_categories();
if(empty($data['post']))
{
show_404();
}
$data['title'] = 'Edit Post';
$this->load->view('templates/header');
$this->load->view('posts/edit', $data);
$this->load->view('templates/footer');
}
public function update()
{
// Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$id=$this->input->post("id");
$slug = url_title($this->input->post('title'));
if( $_FILES['userfile']['name']!="" )
{
$post_image = $this->upload_image();
}
else
{
$post_image=$this->input->post('old');
}
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'post_image' => $post_image
);
//$this->post_model->update_post($post_image);
$this->post_model->update_post($data,$id);
// Set message
$this->session->set_flashdata('post_updated', 'Your post has been updated successfully!');
redirect('posts');
}
public function upload_image()
{
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}
else
{
$upload_data=$this->upload->data();
$post_image=$upload_data['file_name'];
}
return $post_image;
}
model:
public function create_post($data)
{
return $this->db->insert('posts', $data);
}
public function update_post($data, $id)
{
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
view (edit view):
<?php echo form_open('posts/update'); ?>
<input type="hidden" name="id" value="<?php echo $post['id']; ?>">
<input type="hidden" id="old" name="old" value="<?php echo $post['post_image'] ?>">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title" placeholder="Add Title" value="<?php echo $post['title']; ?>">
</div>
<div class="form-group">
<label>Body</label>
<textarea id="editor1" class="form-control" name="body" placeholder="Add Body"><?php echo $post['body']; ?></textarea>
</div>
<div class="form-group">
<label>Change Image</label>
<input class="form-control" type="file" name="userfile" size="20">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I'm not sure how anything is uploading as do_upload() doesn't declare the the name of the file field. In this case I suppose it is userfile e.g. do_upload('userfile').
Consider doing something with you file uploads error variable as this should have uncovered your error rather quickly.
EDIT:
My bad, apparently do_upload() default file name parameter for upload is userfile. My guess now as to why it isn't working is that your "update" form, doesn't use form_open_multipart!
Please note: you can also move your redirect function back to login to the controllers constructor. This will eliminate some lines of duplicate code.
Also if you are looking for an easy way to get the errors of the upload function you can do the following:
private function upload_image()
{
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
throw new Exception($this->upload->display_errors());
//$errors = array('error' => $this->upload->display_errors());
//$post_image = 'noimage.jpg';
}
else
{
$upload_data=$this->upload->data();
$post_image=$upload_data['file_name'];
}
return $post_image;
}
Usage:
try {
$this->upload_image();
} catch (Exception $e) {
show_error($e->getMessage());
}
How to upload images and videos in two different folders using CodeIgniter 3?
I have done only images. Please guide me through videos. I want to upload image and videos in two different folders.
My Controller, Model and View
class Blog extends CI_Contrller{
public function create_blog(){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('blogs/login');
}
$data['title'] = 'Create Blog';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('blog/create_blog', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$path = './assets/uploads/blogs/images';
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 15000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('blog/create_blog');
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->M_blog->create_blog($post_image);
redirect('blog/view');
}
}
}
Model
// Model Begins here
public function create_blog($post_image){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'content' => $this->input->post('content'),
'image' => $post_image
);
return $this->db->insert('events', $data);
}
View
// View begins here
<div class="container"><br>
<h2><?= $title ?></h2>
<?php echo form_open_multipart('blog/create_blog'); ?>
<?php echo validation_errors(); ?>
<?php echo $this->session->flashdata('file_error'); ?>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title"
placeholder="Add Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea class="form-control" id="editor1" name="content"
placeholder="Add Body"></textarea>
</div>
<div class="form-group">
<label>Upload Image</label>
<input type="file" name="userfile" id="userfile" size="20" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
Try this
class Blog extends CI_Contrller{
function create_blog() {
// Check Login
if (!$this->session->userdata('logged_in')) {
redirect('blogs/login');
}
$data['title'] = 'Create Blog';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('blog/create_blog', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
$ext = strtolower($ext);
$image_ext_arr = array('gif', 'jpg', 'png', 'jpeg');
if (in_array($ext, $image_ext_arr)) {
$path = FCPATH.'assets/uploads/blogs/images';
$allowed_types = 'gif|jpg|png';
} else {
$path = FCPATH.'assets/uploads/blogs/video';
$allowed_types = 'mp4|mp3';
}
$config['upload_path'] = $path;
$config['allowed_types'] = $allowed_types;
$config['max_size'] = 15000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('blog/create_blog');
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->M_blog->create_blog($post_image);
redirect('blog/view');
}
}
}
Added few line :
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION); //to find whether is image or video
$ext = strtolower($ext);// some time extension in upper later so converted into small letters
$image_ext_arr = array('gif', 'jpg', 'png', 'jpeg'); //give here all extension of image that you want to upload
if (in_array($ext, $image_ext_arr)) { //if matches means is image
$path = FCPATH.'assets/uploads/blogs/images';
$allowed_types = 'gif|jpg|png';
} else { //else video
$path = FCPATH.'assets/uploads/blogs/video';
$allowed_types = 'mp4|mp3';
}
Note : Better to give absolute path instead of relative path to upload so
$path = FCPATH.'assets/uploads/blogs/video';
I have a code which works fine if I want to upload one image into my MySQL and server.
PHP code:
if(isset($_POST['btnsave']))
{ foreach($_FILES['user_image']['tmp_name'] as $key => $tmp_name ){
$username = $_POST['user_name'];
$userjob = $_POST['user_job'];
$imgFile = $key.$_FILES['user_image']['name'][$key];
$tmp_dir = $_FILES['user_image']['tmp_name'][$key];
$imgSize = $_FILES['user_image']['size'][$key];
}
if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute())
{
$successMSG = "new record successfully inserted ...";
header("refresh:5;index.php");
}
else
{
$errMSG = "error while inserting....";
}
}
}
Html code:
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Username.</label></td>
<td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profession(Job).</label></td>
<td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_image[]" accept="image/*" multiple /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> save
</button>
</td>
</tr>
</table>
</form>
How can I make it code to use for multiple upload? For all images which I would upload I would make the same values except 'userPic'- the same name for a file at my server. Can you help me out please?
I found one more problem! Maybe you guys can help me?
If I'm trying to upload for example 6 images, one of theme is bigger than maxsize its popups the error but every other files which was uploaded just before it went to upload folder. How to delete this files if i get an error?
second questions is how to resize images? any code?
Put everything inside the foreach block.
if(isset($_POST['btnsave']))
{ foreach($_FILES['user_image']['tmp_name'] as $key => $tmp_name ){
$username = $_POST['user_name'];
$userjob = $_POST['user_job'];
$imgFile = $key.$_FILES['user_image']['name'][$key];
$tmp_dir = $_FILES['user_image']['tmp_name'][$key];
$imgSize = $_FILES['user_image']['size'][$key];
if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute())
{
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php");
}
else
{
$errMSG = "error while inserting....";
}
}
}}
i have form to upload images in php, when i save the path and name, the name returns empty field in table database here is my code and the value in my database always empty field, please help me
Registration controller
<?php
/**
*
*/
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
//$this->load->library('upload');
$this->load->helper('form');
}
function index() {
$this->load->view('/registration/daftar');
}
function add_user() {
$this->load->model('/daftar/Daftar_Model');
$image_path = APPPATH. 'user_images/';
$config['upload_path'] = $image_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_height'] = '450';
$config['max_width'] = '450';
//$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$path = $this->upload->data();
$image_name1 = $path['file_name'];
$image_path1 = $path['file_path'];
//$this->upload->do_upload('foto');
//$photo_data = $this->upload->data();
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'),
'no_telepon' => $this->input->post('no_telepon'),
'alamat' => $this->input->post('alamat'),
'image_path' => $image_path1,
'image_name' => $image_name1
);
$this->db->select('username');
$this->db->where('username', $data['username']);
$result = $this->db->get('user');
if($result->num_rows() > 0) {
echo "Udah ada";
$this->output->set_header('refresh:2; url='.site_url("/registration"));
} else {
if(! $this->upload->do_upload('foto')) {
var_dump($this->upload->display_errors());
//$this->load->view('/registration/daftar', $error);
} else {
//$data1 = array('upload_data' => $this->upload->data());
$this->Daftar_Model->insert($data);
echo "Registrasi Berhasil";
$this->output->set_header('refresh:2; url='.site_url("/login"));
//$this->load->view('/registration/daftar', $data1);
}
}
}
}
this is my form
<html>
<head>
<title>Daftar</title>
<body>
<?php
?>
<h1><center>Daftar</center></h1>
<?php echo form_open_multipart('registration/registration/add_user'); ?>
<center>
Username <br>
<input type="text" name="username"> <br>
Password <br>
<input type="password" name="password"> <br>
Email <br>
<input type="text" name="email"> <br>
No Telepon <br>
<input type="text" name="no_telepon"> <br>
Alamat <br>
<textarea name="alamat" rows="6" cols="23"></textarea> <br>
</form>
Foto
<input type="file" name="foto"><br><br><br>
<input type="submit" name="submit" value="Daftar">
</center>
</form>
</body>
</head>
</html>
Are you check your function add_user()?
I suggested you to check upload status before you can add to database. If your upload code error, they will show you error and then you will know where to fix it. It should be:
if(!$this->upload->do_upload('foto'))
{
// Print the upload error
var_dump($this->upload->display_errors());
}else{
$return = $this->upload->data();
$image_name = $return['file_name'];
// your code add to database
}
Hope this help.
I'm new to codeigniter, and I'm trying to make a form that updates a profile. It has worked before but now it doesn't anymore.
I changed something in the view and it stopped working. I changed it back to where it was but it doesn't work anymore. I spend half a day trying to make it work again but I failed.
Maybe I don't see what you guys can see. At least I hope so.
this is my view:
<?php echo form_open_multipart('Gids/do_upload');?>
<img width="200px" src="<?php echo base_url()."uploads/".$profile[0]['image']; ?>" alt=""/>
<label for="">Uploade new picture:</label><input type="file" name="userfile" size="20" value="128.jpg" >
<input style="display: none" id="image" name="image" type="text" value="<?php echo "profile_picture".$_SESSION['id'].".jpg"; ?>"/>
<label for="naam">Naam:</label><input id="naam" name="naam" type="text" value="<?php echo $profile[0]['naam']; ?>"/>
<label for="voornaam">Voornaam:</label><input id="voornaam" name="voornaam" type="text" value="<?php echo $profile[0]['voornaam']; ?>"/>
<label for="email">Gebruikersnaam:</label><input id="gebruikersnaam" name="gebruikersnaam" type="text" value="<?php echo $profile[0]['gebruikersnaam']; ?>"/>
<label for="email">Email:</label><input id="email" name="email" type="text" value="<?php echo $profile[0]['email']; ?>"/>
<label for="opleiding">Opleiding:</label><input id="opleiding" name="opleiding" type="text" value="<?php echo $profile[0]['opleiding']; ?>"/>
<label for="school">School:</label><input id="school" name="school" type="text" value="<?php echo $profile[0]['school']; ?>"/>
<label for="wachtwoord">Wachtwoord:</label><input id="wachtwoord" name="wachtwoord" type="text" />
<label for="typeAgain">Type Opnieuw:</label><input id="typeAgain" type="text" />
<label for="over">Over mezelf:</label><textarea name="over" id="over" cols="30" rows="10"><?php echo $profile[0]['over']?></textarea>
<input type='text' style="display: none" name='student_id' value="<?php echo $profile[0]['student_id']?>"/>
<button class="btn btn-default" id="changeprofile" type="submit">Wijzigingen opslaan</button>
</form>
As you can see there is also a image upload
this is my controller:
function do_upload()
{
$this->load->model("Gids_model",'',true);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['file_name'] = 'profile_picture'.$_SESSION['id'].'.jpg';
$config['overwrite'] = 'TRUE';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error_upload = array('error' => $this->upload->display_errors());
redirect('Gids/datum', $error_upload);
}
else
{
$this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
//$e = $this->input->post('student_id');
$data_upload = array('upload_data' => $this->upload->data());
redirect('Gids/datum', $data_upload);
}
}
And this is my model:
public function update_profile($id, $voornaam, $naam, $email, $wachtwoord, $opleiding, $school, $over, $image, $gebruikersnaam){
$data = array(
'student_id' => $id,
'voornaam' => $voornaam,
'naam' => $naam,
'email' => $email,
'wachtwoord' => $wachtwoord,
'opleiding' => $opleiding,
'school' => $school,
'over' => $over,
'image' => $image,
'gebruikersnaam' => $gebruikersnaam
);
$this->db->where('student_id', $id);
$this->db->update('tbl_student', $data);
}
Could you please help me out.
Also I don't know what the form_open_multipart('Gids/do_upload') is for, I got it from a tutorial to upload images with codeigniter.
Here is what i tried
public function do_upload() {
$this->load->model("Gids_model",'',true);
// load library or you can do it with autoloading
// feature of CI
$this->load->library('upload');
// make sure that the folder uploads is created
// at the root directory of the project
$config = array(
'upload_path' => './uploads',
'allowed_types' => 'jpg|jpeg|JPG|JPEG|png',
'max_size' => '1000',
'file_name' => 'profile_picture.jpg',
'overwrite' => true
);
// use initialize instead
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error_upload = array('error' => $this->upload->display_errors());
// you can check the errors here by using var_dump();
// just uncomment the the line below
// var_dump($this->upload->display_errors());die();
redirect('Gids/datum', $error_upload);
}
else
{
$this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
//$e = $this->input->post('student_id');
$data_upload = array('upload_data' => $this->upload->data());
redirect('Gids/datum', $data_upload);
}
}
I tried this and hope it helps:
Controller :
public function upload_profile() {
$input = $this->input->post();
$config['upload_path'] = './uploads/profile_pics/'; //path were I save the uploaded profile pics
$config['allowed_types'] = 'gif|jpg|png'; // allowed types that is mention
//size of the picture by default
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = true;
$this->load->library('upload', $config);
// display error if the picture is not on the config (sample bmp)
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors(); // display the errors
$data['upload_error'] = $error;
if($this->session->userdata('account_id') != null) { // if there is an account
$this->load->model('profile'); //model
$this->load->model('account'); //model
$data['user'] = $this->profile->get_profile($this->session->userdata('account_id')); //get_profile is a function in model
$data['account'] = $this->account->get_account($this->session->userdata('account_id')); //get_account is a function in model
$data['view'] = 'users/settings';
$data['title'] = "User's Settings";
$data['error'] = $error;
$this->load->view('masterpage', $data);
} else {
redirect(base_url('index.php/qablog/login'));
}
}
else
{
//if no error
$data = $this->upload->data();
$updateProfile = array(
'profile_pic' => $data['file_name']
);
$this->load->model('profile');
$this->profile->update_profile($this->session->userdata('account_id'), $updateProfile); // update the profile of the user
redirect(base_url('index.php/users/profile'));
}
}
Model get_profile():
public function get_profile($profile_id)
{
$this->db->select()->from('profile_tbl')->where('profile_id', $profile_id);
$query = $this->db->get();
return $query->first_row('array');
}
Model update_profile():
public function update_profile($profile_id, $data)
{
$this->db->where('profile_id', $profile_id);
$this->db->update('profile_tbl', $data);
return $this->db->affected_rows();
}
Model get_account():
public function get_account($account_id)
{
$this->db->select()->from('account_tbl');
$this->db->where('account_id', $account_id);
$query = $this->db->get();
return $query->result_array();
}
View :
//if there is an error
<?php
if ($error == 3) {?>
<div class="alert alert-success">
×
<strong>Success!</strong> Account or Profile Changed Successfully.
</div>
<?php } else if ($error == 1) { ?>
<div class="alert alert-warning">
×
<strong>Warning!</strong>Password Entered is Incorrect!.
</div>
<?php }else if ($error == 2) { ?>
<div class="alert alert-warning">
×
<strong>Warning!</strong>New Password and Confirm Password!.
</div>
<?php }?>
// default.png if haven't uploaded profile picture
<?php $profilePic = "default.png"; ?>
// if already uploaded profile picture it will display
<?php if($user['profile_pic'] != null) { ?>
<?php $profilePic = $user['profile_pic']; ?>
<?php } ?>
// if there is an error in uploading
<?php if(isset($upload_error)) { ?>
<div class="col-lg-12">
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Uploading profile image could not be completed. <?php print_r($upload_error); ?>
</div>
</div>
<?php } ?>
<div class="col-lg-12">
//display the profile picture
<img src="<?php echo base_url('uploads/profile_pics/'.$profilePic); ?>" width="100" />
// call the controller upload_profile
<?php echo form_open_multipart(base_url('index.php/users/upload_profile'));?>
<input type="file" name="userfile" id="userfile" size="20" style="display:none;" />
<label for="userfile" class="btn btn-info btn-sm">Choose Image</label>
<input type="submit" class="btn btn-xs" value="edit profile" />
</form>
<h3 class="text-info"><?php echo $user['fname'].' '.$user['lname']; ?></h3>
</div>
Just follow this, and if you have problem just tell me. :)