image uploading is not working in CodeIgniter - php

when I click button then it will return error "You did not select a file to upload." and My image is not uploaded!
now please help me what can I do?
my view is
<form action="insertform" enctype="multipart" name="form" method="post" id="contact-form" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="name">Image Upload:</label>
<div class="controls">
<input type="file" class="input-large" name="image" id="image">
</div>
<form>
my controller is:
public function upload()
{
$this->load->helper(array('form', 'url'));
$config['upload_path'] = FCPATH."upload";
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('xyz', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('xyz', $data);
}
}

I think the file input name should be "userfile", not "image".

CI file upload file control default name is userfile.
If you want to change the it's name to customized one, then you have to pass the new name to upload function like :
if ( ! $this->upload->do_upload("image")) // image is your new name for file control

Firstly,
either change the do_upload function call to:
if ( ! $this->upload->do_upload('image'))
OR
change the input name to this:
<input type="file" class="input-large" name="userfile" id="image">
Secondly,
change your form enctype to:
enctype="multipart/form-data"

Related

How to upload 2 file input in one form Codeigniter 3

I have a problem with how to upload 2 file input in 1 form CI 3 with different file name. My view code: There is have 2 file input, 1. file_sertifikat and 2. file_rekomendasi
<div class="form-group">
<label for="file_sertifikat">File Sertifikat</label>
<input type="file" name="file_sertifikat" class="form-control" id="file_sertifikat">
<small class="text-danger"><?= form_error('file_sertifikat') ?></small>
</div>
<div class="form-group">
<label for="file_rekomendasi">File Rekomendasi</label>
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
<small class="text-danger"><?= form_error('file_rekomendasi') ?></small>
</div>
My Controller action to save filename to database:
$file_sertifikat = $this->upload_file_sertifikat($this->input->post('file_sertifikat'));
$file_rekomendasi = $this->upload_file_rekomendasi($this->input->post('file_rekomendasi'));
$data = array(
'file_sertifikat' => $file_sertifikat,
'file_rekomendasi' => $file_rekomendasi,
);
$this->perizinan_model->insert_data($data,'perizinan');
redirect('admin/perizinan');
}
and my upload file function:
public function upload_file_sertifikat(){
$config['upload_path'] = './assets/uploads';
$config['file_name'] = 'file_sertifikat_'.rand(1, 99999);
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['remove_space'] = TRUE;
$config['max_size'] = 10048;
$this->load->library('upload', $config);
if($this->upload->do_upload('file_sertifikat')){
return $this->upload->data("file_name");
}else{
return "defaultimg.png";
}
}
public function upload_file_rekomendasi(){
$config['upload_path'] = './assets/uploads';
$config['file_name'] = 'file_rekomendasi_'.rand(1, 99999);
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['remove_space'] = TRUE;
$config['max_size'] = 10048;
$this->load->library('upload', $config);
if($this->upload->do_upload('file_rekomendasi')){
return $this->upload->data("file_name");
}else{
return "defaultimg.png";
}
}
I want to upload that 2 different filename one by one, but my code overwrite the file. Thank before.
In html change
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
to
<input type="file" name="file_rekomendasi[]" class="form-control" id="file_rekomendasi" multiple >
In the controller, you will get $file_rekomendasi as an array. Handel that as like array. Run a loop.

Codeigniter file upload error: You did not select a file to upload

This is really odd, I tried to check the top suggestions in google that is similar to my issue here but I believe those are not the answers to this. I'm using the default 'userfile'. Can anyone tell me what I'm missing here?
The structure of my form:
<div class="panel-body">
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal'); ?>
<div class="form-group">
<label for="userfile" class="col-lg-3 col-md-3 col-sm-3 control-label">Profile Picture</label>
<div class="col-lg-9 col-md-9 col-sm-9">
<input type="file" name="userfile" size="20" id="userfile">
</div>
</div>
<button type="submit" class="btn btn-default btn-login-style btn-sm pull-right">Submit</button>
<?php echo form_close(); ?>
</div>
The structure of my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 2048;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
if(!$this->upload->do_upload()){
$error = array('error'=>$this->upload->display_errors());
echo $error['error'];
}else{
$uploaded_pic = $this->upload->data();
}
}
}
?>
NOTE: 'Upload' library is loaded automatically in autoload.php.
try removing $this->upload->do_upload(); before the if else statement?
don't need to specify twice.
You have called $this->upload->do_upload(); twice. Pls remove the first one.
And use $this->upload->do_upload('userfile'); in place of $this->upload->do_upload();
ok i got it. I am missing a quotation " in my class:
the wrong code:
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal'); ?>
the correction here:
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal"'); ?>
The Second Line in Your View,Please Correct it
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal" '); ?>

codeigniter $this->upload->do_upload() = false

I'm trying to upload a file. I select a file and then I submit it but result of $this->upload->do_upload() is always false.
Here is my form;
<?php echo form_open_multipart(base_url('files/fileUpload')); ?>
<input type="file" name="userfile" class="btn btn-default" size="20"/>
<input type="submit" value="upload" />
</form>
and here is my controller;
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function fileUpload(){
$this->load->library('upload');
$data = array('upload_data' => $this->upload->data());
var_dump($this->upload->do_upload());die;
}
Do I miss something ? What do I need to do ?
Thank you..
There is problem with code in controller fileUpload function
Try with below code
public function fileUpload(){
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload',$config);
$data = array('upload_data' => $this->upload->data());
var_dump($this->upload->do_upload());die;
}
Here you need to define file upload path and file allow types.
Hope this will help you.

Uploading file with php

I have the following set up in a Code Igniter application, however the file is not being passed through to the file uploader class. This is the second form on the page, just for reference and I have checked that they are both closed correctly.
The output I am getting is You did not select a file to upload. when I have been selecting an valid image.
View
<form action="../albums/upload" class="form-horizontal" method="post">
<div class="form-group">
<input type="file" name="userfile" required="" class="form-control">
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-4">
<input type="submit" id="upload" name="upload" class="btn btn-success" value="Upload">
</div>
</div>
</form>
Controller
public function upload() {
if($this->input->post('userfile')){
$this->model_photo->do_upload();
}
}
Model
function do_upload() {
$path = base_url().'res/image/';
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png|tif|tiff',
'upload_path' => $path
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
echo $error['error']; //echo debugging purposes
} else {
$data = array('upload_data' => $this->upload->data());
echo $data['upload_data']; //echo debugging purposes
}
}
in your <form> add enctype="multipart/form-data"
<form action="../albums/upload" class="form-horizontal" method="post" enctype="multipart/form-data">

How to clear form data after submission?

I have form with a file input in my CodeIgniter website. When I fill in the form and click the submit button it's working fine. However, when I refresh the browser the same form data is uploaded again. How can I prevent this ?
input form
<form name="image_upload" action="<?= base_url() ?>admin/image_move_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<ul>
<li class="ui-field"><label for="filename">File Name :</label></li>
<li class="ui-input"><input type="text" name="filename" placeholder=""></li>
<li class="ui-field"><label for="file">Image( *only .jpg) :</label></li>
<li class="ui-input"><input type="file" name="imagefile" value="" placeholder="" required=""></li>
<li><input type="submit" name="Upload" class="ui-submit"></li>
</ul>
</form>
controller section
function image_move_upload() {
$data['errors'] = NULL;
$data['success'] = NULL;
if (isset( $_POST['Upload'] )) {
$config_arr = array(
'upload_path' => './uploads/slider/',
'allowed_types' => 'jpg',
'max_size' => '2048',
'max_width' => '1024',
'max_height' => '768',
'encrypt_name' => true,
'file_name' => 'sanji.jpg',
);
$this->load->library('upload', $config_arr);
if (!$this->upload->do_upload('imagefile')) {
$data['errors'] = $this->upload->display_errors(); // this isn't working
} else {
$data['qs'] = $this->upload->data();
$data['success'] = "File Uploaded Successfully";
}
}
$data['main_content'] = 'admin/upload';
$this->load->view('admin_layout', $data);
}
Use the Post/Redirect/Get pattern. http://en.wikipedia.org/wiki/Post/Redirect/Get
redirect to same controller with refresh. Use following code.
redirect('admin/image_move_upload', 'refresh');
Use Redirect Function rather than calling a function of same class.
So, Use redirect('admin/image_move_upload', 'refresh');

Categories