How to multiple upload files in codeigniter 3 - php

i'm newbie on codeigniter and php, i just don't know how to upload multiple files..
I am able to upload & insert a image ine the database. But I am unable to upload multiple images, what should I change in the below code , in order to upload multiple images
help me, here is my code
Controller
public function index()
{
$data['title'] = 'Pengaduan';
$masyarakat = $this->db->get_where('masyarakat',['username' => $this->session->userdata('username')])->row_array();
$data['data_pengaduan'] = $this->Pengaduan_m->data_pengaduan_masyarakat_nik($masyarakat['nik'])->result_array();
$this->form_validation->set_rules('isi_laporan','Isi Laporan Pengaduan','trim|required');
$this->form_validation->set_rules('foto','Foto Pengaduan','trim');
if ($this->form_validation->run() == FALSE) :
$this->load->view('_part/backend_head', $data);
$this->load->view('_part/backend_sidebar_v');
$this->load->view('_part/backend_topbar_v');
$this->load->view('masyarakat/pengaduan');
$this->load->view('_part/backend_footer_v');
$this->load->view('_part/backend_foot');
else :
$upload_foto = $this->upload_foto('foto'); // parameter nama foto
if ($upload_foto == FALSE) :
$this->session->set_flashdata('msg','<div class="alert alert-danger" role="alert">
Upload foto pengaduan gagal, hanya png,jpg dan jpeg yang dapat di upload!
</div>');
redirect('Masyarakat/PengaduanController');
else :
$params = [
'tgl_pengaduan' => date('Y-m-d'),
'nik' => $masyarakat['nik'],
'isi_laporan' => htmlspecialchars($this->input->post('isi_laporan',true)),
'foto' => $upload_foto,
'status' => '0',
];
$resp = $this->Pengaduan_m->create($params);
if ($resp) :
$this->session->set_flashdata('msg','<div class="alert alert-primary" role="alert">
Laporan berhasil dibuat
</div>');
redirect('Masyarakat/PengaduanController');
else :
$this->session->set_flashdata('msg','<div class="alert alert-danger" role="alert">
Laporan gagal dibuat!
</div>');
redirect('Masyarakat/PengaduanController');
endif;
endif;
endif;
}
private function upload_foto($foto)
{
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'jpeg|jpg|png';
$config['max_size'] = 2048;
$config['remove_spaces'] = TRUE;
$config['detect_mime'] = TRUE;
$config['mod_mime_fix'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($foto)) :
return FALSE;
else :
return $this->upload->data('file_name');
endif;
}
i tried using [] but it's show error, Is there a way to use this code so i can upload multiple images?

Related

How can I store validation errors related to image upload as flash massages in this Codeigniter 3 application?

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
The posts, of course, have main images. There is a default image if no image is uploaded by the user.
There are restrictions on image files a user can upload:
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
I have managed to display the upload error messages that correspond to the configuration above (for your curiosity and/or use, the code is here).
For the update() method however, since there are redirects involved, all form validation messages are stored as "flash messages".
public function update() {
// Form data validation rules
$this->form_validation->set_rules('title', 'Title', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('desc', 'Short description', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('body', 'Body', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
$id = $this->input->post('id');
// Update slug (from title)
if ($this->form_validation->run()) {
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, $id);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
} else {
$slug = $this->input->post('slug');
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
// Use name field in do_upload method
if (!$this->upload->do_upload('userfile')) {
$errors = array('error' => $this->upload->display_errors());
// Dysplay upload validation errors
// only if a file is uploaded and there are errors
if (empty($_FILES['userfile']['name'])) {
$errors = [];
}
if (!empty($errors)) {
$data['upload_errors'] = $errors;
}
} else {
$data = $this->upload->data();
$post_image = $data[ 'raw_name'].$data[ 'file_ext'];
}
}
else {
$post_image = $this->input->post('postimage');
}
if ($this->form_validation->run() && empty($errors)) {
$this->Posts_model->update_post($id, $post_image, $slug);
$this->session->set_flashdata('post_updated', 'Your post has been updated');
redirect('/' . $slug);
} else {
$this->form_validation->run();
$this->session->set_flashdata('errors', validation_errors());
// this line was added later
// but the bug persists
$this->session->set_flashdata('upload_errors', $errors);
redirect('/dashboard/posts/edit/' . $slug);
}
}
In the edit-post.php view I have:
<input type="hidden" name="postimage" id="postimage" value="<?php echo $post->post_image; ?>">
<label for="postimage">Upload an image</label>
<div class="form-group">
<input type="file" name="userfile" id="postimage" size="20">
<?php if ($this->session->flashdata('upload_errors')) { ?>
<div class="error-messages">
<?php if(isset($upload_errors)){
foreach ($upload_errors as $upload_error) {
echo $upload_error;
}
}?>
</div>
<?php } ?>
</div>
I have not been able to add the image upload errors messages as "flash messages".
How can I do that?
In edit-post.php you have to change:
<?php if ($this->session->flashdata('upload_errors')) { ?>
With:
<?php if ($upload_errors = $this->session->flashdata('upload_errors')) { ?>
Try this:
$this->session->set_flashdata('errors', validation_errors());
$this->session->set_flashdata('upload_errors', $errors);
Or you can create an $errorArray from validation_errors() and $errors.
I do the exact same thing (upload errors go into a flashdata element). Here's how it works for me:
(I'll skip the upload config... but for this example, it's stored in an array conveniently named $config)
In my controller:
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$this->session->set_flashdata('message', "An error ocurred: ".$this->upload->display_errors());
$this->session->set_flashdata('alert_class', "alert-danger");
}
else
{
$this->session->set_flashdata('message', "Upload successful");
$this->session->set_flashdata('alert_class', "alert-success");
}
In my view (all my views carry this code)
<?php
if (null !== $this->session->flashdata('message'))
{ ?>
<div class="alert <?php echo $this->session->flashdata('alert_class'); ?> text-center mb-2"><i class="fas fa-exclamation-triangle fa-fw"></i> <?php echo $this->session->flashdata('message'); ?></div>
<?php
}
?>
In this way, if I don't set a flashdata element called message the view won't even display the DIV where alert is displayed. If, on the other hand, I do set the message element, a col-12 alert is displayed with the message I defined and styled with the alert class I want (success, warning, danger, info, etc)
This will only work for upload validation errors. In your multipart form you may also have regular validation errors which you should check for separately. You can do the same with a flash element:
$this->session->set_flashdata('message', validation_errors());
I use the same element name (message) because I'd stop upon the first error, So there would be no upload error and form validation error in the same display.
This should work.
public function update()
{
// Form data validation rules
$this->form_validation->set_rules('title', 'Title', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('desc', 'Short description', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('body', 'Body', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
$id = $this->input->post('id');
$form_is_valid = $this->form_validation->run()
$errors = [];
// Update slug (from title)
if ($form_is_valid) {
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, $id);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
} else {
$slug = $this->input->post('slug');
$errors[] = validation_errors();
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
// Use name field in do_upload method
if (!$this->upload->do_upload('userfile')) {
$upload_errors = array('error' => $this->upload->display_errors());
// Dysplay upload validation errors
// only if a file is uploaded and there are errors
if (empty($_FILES['userfile']['name'])) {
$upload_errors = [];
}
if (!empty($upload_errors)) {
$data['upload_errors'] = $upload_errors;
$errors[] = $upload_errors;
}
} else {
$data = $this->upload->data();
$post_image = $data[ 'raw_name'].$data[ 'file_ext'];
}
}
else {
$post_image = $this->input->post('postimage');
}
if (empty($errors)) {
$this->Posts_model->update_post($id, $post_image, $slug);
$this->session->set_flashdata('post_updated', 'Your post has been updated');
redirect('/' . $slug);
} else {
$this->session->set_flashdata('errors', $errors);
redirect('/dashboard/posts/edit/' . $slug);
}
}
$this->form_vlaidation->run() is called only once.
Errors are saved both (form validation & upload) in the same array.
Post is updated only if $errors is empty.
You should think how you can improve the code further.
Try this one .Add this in constructor
$this->session->keep_flashdata('errors');
and remove session data after use it in view
$this->session->unset_userdata('errors');

Image upload still submitting with callback validation in CI

I am trying to do a image upload validation on codeigniter 3. I have tried many tut's but still face this issue. I would like if the user was to leave the upload a photo area blank ( eventually if they upload a unsupported mime and size) for it to throw a error before it allows or tries to submit the data.
Right now it throws a error but showing the database query along with "Image in sql cannot be blank". I would like it to show like a regular form validation would without it showing the sql query and error.
It still seems to submit the form without it throwing the error first, because it says $post_image undefined.
I have already tried entering a callback with isset function to try to only allow the form to submit data if a image was uploaded, if not I was trying to display the errors by passing the error variable. None of this seems to work correctly as I explained above.
Controller:
public function create(){
//Check in login session
if(!$this->session->userdata('logged_in')){
$this->session->set_flashdata('log_post','Please login or create a free account to post a ad.');
redirect('users/register');
}
$data['title'] = 'Create a Post';
$data['categories'] = $this->post_model->get_categories();
$data['states'] = $this->post_model->get_city();
$this->form_validation->set_error_delimiters('<div class="error"> <h7> Error: </h7>', '</div>');
$this->form_validation->set_rules('title','Title',array('required', 'min_length[3]'));
//$this->form_validation->set_rules('file','Image Upload','required');
$this->form_validation->set_rules('Description','About You',array('required', 'min_length[5]'));
$this->form_validation->set_rules('Number','Phone Number',array('required', 'min_length[7]'));
$this->form_validation->set_rules('Area','Location/Area',array('required', 'min_length[2]'));
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// $this->load->helper('file');
//$this->form_validation->set_rules('file','','callback_file_check');
if($this->form_validation->run()==TRUE){
$config['upload_path'] = 'assets/images/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['encrypt_name'] = TRUE; //TURN ON
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$errors = array('error'=>$this->upload->display_errors());
$this->load->view('templates/header');
$this->load->view('posts/create', $errors);
$this->load->view('templates/footer');
}else {
$data = $this->upload->data();
$post_image = $data['file_name'];
}
}
$this->post_model->create_post($post_image);
$this->session->set_flashdata('post_created','Your Post has been submitted');
redirect('posts');
}
}
}// end of class
Model:
public function create_post($post_image){
$slug = md5(uniqid().mt_rand());
//url_title($this->input->post('title'), 'dash', TRUE). // this is the orginal way for slug SEO friendly
$site = $this->input->post('site');
//adds HTTP too website links
if (!preg_match("~^(?:f|ht)tps?://~i", $site)) {
$site = "http://" . $site;
}
$data = array(
'title'=>$this->input->post('title'),
'body'=> $this->input->post('Description'),
'post_image' => $post_image
);
return $this->db->insert('posts',$data);
}
View:
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="textarea">Photo</label>
<div class="col-lg-8">
<div class="mb10">
<?php echo form_error('file') ?>
<input name="file" type="file" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
</div>
<?php if (isset($error)) { echo $error; } ?>
</div>

i am not understanding how to upload image in codeigniter

Below i have code of my form data with image but it was not working. i tried to insert data without image the data inserted into db. but when i tried to insert with image the form error raised and redirecting to targeted page. Below i have mvc code please while giving edited answer also explain me why my code didn't worked and how your code worked instead of copy paste i want to learn concept behind it.please.
//my form view code
<?php echo form_open_multipart('admin/uauthor');?>
<h5><label>Username Here:</label></h5>
<?php echo form_input(['name'=>'aname','class'=>'form-control','placeholder'=>'Enter your name here']);?>
<h5><label>Mail Here:</label></h5>
<?php echo form_input(['name'=>'amail','class'=>'form-control','placeholder'=>'Enter your Mail here']);?>
<h5><label>Password Here:</label></h5>
<?php echo form_input(['name'=>'apwd','type'=>'password','class'=>'form_control','placeholder'=>'Enter your password here']);?>
<h5><label>Phone Here:</label></h5>
<?php echo form_input(['name'=>'aphone','class'=>'form-control','placeholder'=>'Enter your phone here']);?>
<h5><label>Profile Pic Here:</label></h5>
<?php echo form_upload(['name'=>'apic','class'=>'form-control']);?>
<select name="alevel" class="form-control">
<option value="admin">Admin</option>
<option value="author">Author</option>
</select><br>
<button type="submit" class="btn btn-success">Add Author</button>
<?php form_close();?>
----------
//my Controller code
public function uauthor()
{
$this->form_validation->set_rules('aname','Author Name','required');
$this->form_validation->set_rules('amail','Author Mail','required|is_unique[admin.amail]',array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));
$this->form_validation->set_rules('aphone','Author Phone','required|is_unique[admin.aphone]',array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));
$this->form_validation->set_rules('apwd','Author Password','required');
if ($this->form_validation->run()){
$config = [
'upload_path' => './uploads/authors',
'allowed_types' => 'jpg|gif|png|jpeg',
];
$this->load->library('upload', $config);
$data= $this->input->post();
if(! $this->upload->do_upload()){
$this->session->set_flashdata('pmsg',"Author Upload Failed Please Try Again");
return redirect('admin/aauthor');
}else{
$info = $this->upload->data();
$image_path = $info['raw_name'] . $info['file_ext'];
$data['apic'] = $image_path;
}
$this->adata->uauthorQ($data);
$this->session->set_flashdata('amsg', 'Author Added Successfully');
return redirect('admin/authors');
}else{
$this->session->set_flashdata('pmsg', validation_errors());
return redirect('admin/aauthor');
}
}
----------
//My Model code
public function uauthorQ($data)
{
return $this->db->insert('admin',$data);
}
Change this :
$this->upload->do_upload()
to this :
$this->upload->do_upload('apic')
so it will using 'apic' instead of the default 'userfile' input field name
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('welcome_message', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
html
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
do some changes on your controller.
inserting data must be as array of fields. so i have changed some $data as array.
$data['aname'] = $this->input->post('aname');
$data['amail'] = $this->input->post('amail');
$data['apwd'] = md5($this->input->post('apwd'));
$data['aphone'] = $this->input->post('aphone');
//my Controller code
public function uauthor()
{
$this->form_validation->set_rules('aname','Author Name','required');
$this->form_validation->set_rules('amail','Author Mail','required|is_unique[admin.amail]',array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));
$this->form_validation->set_rules('aphone','Author Phone','required|is_unique[admin.aphone]',array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));
$this->form_validation->set_rules('apwd','Author Password','required');
if ($this->form_validation->run()){
$config = [
'upload_path' => './uploads/authors',
'allowed_types' => 'jpg|gif|png|jpeg',
];
$this->load->library('upload', $config);
if(! $this->upload->do_upload('apic')){
$this->session->set_flashdata('pmsg',"Author Upload Failed Please Try Again");
return redirect('admin/aauthor');
}else{
$info = $this->upload->data();
$image_path = $info['raw_name'] . $info['file_ext'];
$data['apic'] = $image_path;
$data['aname'] = $this->input->post('aname');
$data['amail'] = $this->input->post('amail');
$data['apwd'] = md5($this->input->post('apwd'));
$data['aphone'] = $this->input->post('aphone');
}
$this->adata->uauthorQ($data);
$this->session->set_flashdata('amsg', 'Author Added Successfully');
return redirect('admin/authors');
}else{
$this->session->set_flashdata('pmsg', validation_errors());
return redirect('admin/aauthor');
}
}

codeigniter alert if image size exceeds

Hi I have this code for uploading an image. I can perfectly add the image but I want to add restrictions and there will an alert that will pop up if image size exceeds but I don't know how can add I alert if image size exceeds. please help me
Here's my code:
CONTROLLERS:
public function upload_file()
{
$filename = 'r_image';
$status = "";
$msg = "";
$config['upload_path'] = 'img/recipes/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 8 ;
$config['encrypt_name'] = true;
$this->load->library('upload',$config);
if (!$this->upload->do_upload($filename))
{
$stats = 'error';
$msg = $this->upload->display_errors('', '');
}else{
$uploadData = $this->upload->data('file_name');
if($uploadData['file_name'])
{
$thumnail = 'img/recipes/'.$uploadData['file_name'];
}else{
$thumnail = 'No thumnbail uploaded!';
}
$updateThumbData = array('recipe_id' => $recipe_id,
'r_image' => $thumnail
);
$this->products_model->updataRecipeThumnail($updateThumbData);
redirect('dashboard/view_product');
} /* upload_file() */
}
for uploading files you must use POST method. read this w3schools file upload.
if($_FILES['Your_POST_name']['size']>1024 * 8 )// to check what is your post name print_r($_FILES);
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Image is too big!</div>');
redirect('dashboard/view_product');
}
and in your view_product's view file, add following code to display error
<?php echo $this->session->flashdata('msg'); ?>
Important things to remember while dealing with file uploads in php.
Make sure that the form uses method="post"
The form also needs the following attribute:
enctype="multipart/form-data". It specifies which content-type to use
when submitting the form
Without the requirements above, the file upload will not work.

Upload two files in codeIgniter showing error

my View form multipart
<h3>Upload Submission File(doc,docx,pdf,cda,rtf,txt)</h3>
<?php echo form_error('fileToUpload'); ?>
<p>
<input type="file" name="fileToUpload" id="fileToUpload" title="Upload Submission File"
accept="application/pdf, .doc, .docx, .txt, application/msword, .rtf, .cda"/>
</p>
<h3>Upload Additional File (gif,jpeg,png,tiff,pdf)</h3>
<?php echo form_error('additionalUpload'); ?>
<p>
<input type="file" name="additionalUpload" id="additionalUpload" title="Upload Additional File"
accept="image/gif, image/jpeg, image/png, image/x-tiff, application/pdf" />
</p>
My Contoller
function submit_article() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');
$my_rules = array(
array(
'field' => 'fileToUpload',
'label' => 'Submission File',
'rules' => 'callback_is_proper_file'
),
array(
'field' => 'additionalUpload',
'label' => 'Additional File',
'rules' => 'callback_is_image'
)
);
$this->form_validation->set_rules($my_rules);
if ($this->form_validation->run() == FALSE) {
//ERROR
$data['title'] = ucfirst('submit Article');
$this->load->view('templates/header', $data);
$this->load->view('submit_article', $data);
$this->load->view('templates/footer', $data);
} else {
//SUCCESS
$data['title'] = ucfirst('article Submitted');
$this->load->view('templates/header', $data);
$this->load->view('forms_view/submit_article_success', $data);
$this->load->view('templates/footer', $data);
}
}
function is_image() {
if ($_FILES['additionalUpload']['tmp_name'] != '') {
$config1['upload_path'] = './public/uploads/';
$config1['allowed_types'] = 'gif|jpg|jpeg|png|pdf|tiff';
$config1['max_size'] = '2048';
$config1['max_width'] = '0';
$config1['max_height'] = '0';
$config1['remove_spaces'] = true;
$config1['overwrite'] = true;
$this->load->library('upload', $config1);
if (!$this->upload->do_upload('additionalUpload')) {
$this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
return FALSE;
} else {
$this->upload->data();
return TRUE;
}
}
}
function is_proper_file() {
if ($_FILES['fileToUpload']['tmp_name'] != '') {
$config['upload_path'] = './public/uploads/';
$config['allowed_types'] = 'doc|docx|pdf|cda|rtf|txt';
$config['max_size'] = '1024*10';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('fileToUpload')) {
$this->form_validation->set_message('is_proper_file', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
return FALSE;
} else {
$this->upload->data();
return TRUE;
}
} else {
$this->form_validation->set_message('is_proper_file', '<p style="color:red">Please select a file to upload.<br/></p>');
return FALSE;
}
}
The additionalUpload field is not required field and filetoupload is a required field.
when i submit the form by filling all the details except additionalUpload my code works well for me, but when i submit the form with additionalUpload along with aal other details it shows me error "The filetype you are attempting to upload is not allowed." for additionalUpload field..
in other words when i submit the form with one file that is filetoupload which is required its works for me.. and when i submit the form with both the files that is filetoUpload and additionalUpload then its shows me error for the additional upload fields "The filetype you are attempting to upload is not allowed.",, and when i simply submit the form with only addtionalupload file its do the upload in my upload folder but when i submit with all the fields it shows me error "The filetype you are attempting to upload is not allowed." for the additional Upload filed..
THANKS IN ADVANCE PLEASE HELP ANYONE
$this->load->library('upload');
$this->upload->initialize($config1);
I need to call it like above where as I have called it in this way:
$this->load->library('upload',$config);

Categories