Image uploading doesn't work in codeigniter - php

Here is my controller:
<?php
if(!defined('BASEPATH')) exit('no direct access allowed');
class Import_data extends CI_Controller
{
function __construct()
{
parent::__construct();
//$this->load->helper('url');
//$this->load->helper('form');
$this->load->library('upload');
}
function index()
{
}
function image_upload()
{
$config['upload_path'] = './upload';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = 'photo_' . substr(md5(time()), 0, 16);
$image = 'aaa';
$this->upload->initialize($config);
if (!$this->upload->do_upload($image))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
$this->load->view('home_view', $error);
}
else
{
$data = $this->upload->data();
print_r($data);die;
$this->load->view('upload_success', $data);
}
}
}
I tried to print error message which prints:
Array ( [error] => You did not select a file to upload. )
I don't know what is wrong in my code. I am selecting file properly, here is my view code:
<form name="" method="post" action="<?php echo site_url('import_data/image_upload');?>" enctype="multipart/form-data">
<input type="file" name="excel_file" /> <br/><br/>
<input type="submit" name="submit" value="Submit">
<?php //echo $error;?>
<br/>
<?php //echo $upload_data;?>
</form>

Here is right code. $this->upload->do_upload('excel_file')
File input field name must be passed here.

Where you pass file name "excel_file" >> to $this->upload->do_upload('file_name');

Related

Codeigniter enctype="multipart/form-data" not save photo properly

Codeigniter When i use enctype="multipart/form-data" in signup form photo not save in database but photo going to uploads folder. Then When i remove enctype="multipart/form-data" photo save in database but photo not coming uploads folder. If this is problem for the code, please do the full code. Because I'm not a good developer.
View
<form method="post" action="family_join" enctype="multipart/form-data" id="wizard">
<h4 class="text-center mb-4 mt-4">Upload Photo</h4>
<div class="form-group mt-3 mb-4">
<div class="dropzone-wrapper">
<div class="dropzone-desc">
<i class="glyphicon glyphicon-download-alt"></i>
<p>Choose an image file or drag it here.</p>
</div>
<input type="file" name="photo" class="dropzone">
</div>
</div>
<h4 class="text-center mb-4">Please enter the zipcode</h4>
<div class="default-form contact-form">
<div class="form-group">
<input type="text" name="zipcode" placeholder="Enter Zip Code" required>
</div>
</div>
</form>
Controller
< ? php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Family_Join extends CI_Controller {
public
function __construct() {
parent::__construct();
$this->load-> helper(array('form', 'url'));
}
public
function index() {
$this->form_validation-> set_rules('zipcode', 'Zipcode', 'required|min_length[4]');
if ($this->form_validation->run()) {
$zipcode=$this->input->post('zipcode');
$photo=$this->input->post('photo');
$status = 1;
$this->load->model('Family_Join_Model');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload('photo');
$this-> Family_Join_Model->insert($zipcode, $photo, $status);
} else {
$this->load->view('user/family_join', array('error' => ' '));
}
}
}
Model
< ? php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Family_Join_Model extends CI_Model {
public
function insert($zipcode, $photo, $status) {
$data = array(
'zipcode' => $zipcode,
'photo' => $photo,
'isActive' => $status
);
$sql_query=$this->db->insert('tblfm', $data);
if ($sql_query) {
$this->session->set_flashdata('success', 'Registration successfull');
redirect('user/family_join');
} else {
$this->session->set_flashdata('error', 'Somthing went worng. Error!!');
redirect('user/family_join');
}
}
}
That happens because when you set enctype to multipart/form-data your input send to server as a binary file and a binary file can not be inserted in database.
For inserting file name into your database follow steps below.
Keep enctype=multipart/form-data (So your file will be uploaded to server)
replace this line :
$this->Family_Join_Model->insert($zipcode, $photo, $status);
with these:
$upload_data = $this->upload->data();
$this->Family_Join_Model->insert($zipcode, $upload_data['file_name'], $status);
Upload file first and get the file name from uploaded file data.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ($this->upload->do_upload('photo')) {
$file_details = $this->upload->data();
$photo = $file_details['file_name'];
} else {
$file_upload_msg = $this->upload->display_errors('<p>', '</p>');
}
$this->upload->do_upload('photo');
$upload_data = $this->upload->data();
$this->Family_Join_Model->insert($zipcode, $upload_data, $status);
**Model**
public
function insert($zipcode, $upload_data, $status) {
$data = array(
'zipcode' => $zipcode,
'photo' => $upload_data['file_name'],
'isActive' => $status
);
$sql_query=$this->db->insert('tblfm', $data);
if ($sql_query) {
$this->session->set_flashdata('success', 'Registration successfull');
redirect('user/family_join');
} else {
$this->session->set_flashdata('error', 'Somthing went worng. Error!!');
redirect('user/family_join');
}
}

Ifile not getting uploaded in the database

i'm trying to upload a file in databse using codeigniter, the image is getting stored in the folder but i'm having issue in storing the data in databse, i don know where
im going wrong. please can any one guide me what im doing wrong ? im new to codeigniter concept.
upload.php(controller)
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('filename')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data('filename'),$this->input->post());
$this->Upload_model->saverecords($data);
//$this->load->view('upload_success', $data);
}
}
}
?>
Upload_model.php(model)
<?php
class Upload_model extends CI_Model
{
//Insert
function saverecords($data)
{
//saving records
$this->db->insert('latest_news', $data);
}
}
?>
Upload_form.php(view)
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action = "" method = "POST">
<input type = "file" name = "filename" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
</form>
</body>
</html>
You declare <form> twice! Remove <form action = "" method = "POST"> completely and just use the above form_open_multipart:
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="filename" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Also there is no need for spaces like type = "submit"
Then in your code:
if ( ! $this->upload->do_upload('filename')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
//$data = array('upload_data' => $this->upload->data('filename'),$this->input->post());
$data['upload_data'] = $this->upload->data('file_name');
$this->load->model('upload_model');
$this->upload_model->saverecords($data);
//$this->load->view('upload_success', $data);
}
For future:
Do not just do $this->input->post() to assign the key value pairs to those in db. It is not very controlled and can lead to issues. Instead just define it. So if you add other data it would look something like:
$data = array(
'example1_fieldname' => $this->input->post('example1_fieldname');
'upload_data' => $this->upload->data('file_name');
);

Codeigniter uploaded file not move to folder

I want to upload file in folder, following is my code for upload file in folder, the code is successfully working in my local system and also file moved to folder but on server side i got 'The upload path does not appear to be valid'. when i used $config['upload_path'] = './uploads/'; but when i used $config['upload_path'] = './assets/images/store/category'; i got success message but uploaded file is not showing in category folder.
Following is my code.
Controller
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
echo "Tst";
$this->load->view('Upload_form', array('error' => ' ' ));
}
public function Test_do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$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('Upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
echo '<pre>';
print_r($data);
// $this->load->view('upload_success', $data);
}
}
}
?>
View ( form)
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/Test_do_upload');?>
<input type = "file" name = "userfile" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
<?= form_close(); ?>
</body>
</html>
View of success upload
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?phpforeach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?phpendforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Code is working in my local system but on server side file not move to folder. I also given permission.
I can see there are two form tag could fire this problem. So revise it
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<form action ="<?php echo site_url('upload/Test_do_upload');?>" method="post" enctype="multipart/form-data">
<input type = "file" name = "userfile" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
</form>
</body>
</html>
May be try this code and try to create directory if its not created. Else print $error variable to find out what kind of upload error. May be this will be helpful. And remove form tag from your view its repeated.
$dir_path= 'assets/admin/uploads/course_images/';
if (!is_dir($dir_path)) {
mkdir($dir_path, 0777, TRUE);
}
$new_name = time().$this->input->post('Course_code');
//move_uploaded_file($_FILES['file']['tmp_name'], $dir_path);
$config['upload_path'] = $dir_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = FALSE;
$config['max_size'] = '1000';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('file'))
{
// no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
/*Print Error*/
}else{
/*Insert Code*/
}

how to upload image with ajax and codeigniter where my CI is IN var/www/html

In html file upload is in form like this
<form method="post" action="" enctype = "multipart/form-data" ><label> File Input: </label><input type = "file" name = "userfile" id="userfile"><a href = "http://localhost/upload_img/index.php/upload/do_upload" >Submit </a></form>
in my controller:
<?php class Upload extends CI_Controller {
public function index(){
$this->load->view('upload_view');
}
public function do_upload(){
$config = array(
'upload_path' => "http://localhost/upload_img/index.php/uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf"
);
//print_r($config);
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r( $error);
}
}
}
Changes
<form> action filed is set
Submitting data with <input type="submit"
upload_path Modified
Try this
In View
<form method="post" action="<?php echo base_url() ;?>index.php/upload/do_upload" enctype = "multipart/form-data" >
<label> File Input: </label>
<input type="file" name="userfile" id="userfile">
<input type="submit" name="submit" value="Upload" />
</form>
In Controller
class Upload extends CI_Controller {
public function index(){
$this->load->view('upload_view');
}
public function do_upload()
{
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|docx"
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
print_r( $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
}
}
}
If you using Ubuntu OS. Then give 777 permission to upload folder.
Multiple issues with your form code!!
You are not supposed to use anchor tag on Submit, it should either be a <button> tag or <input> tag with attribute type="submit"
Please see this:
http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
First of all correct your form like this:
View:
<form method="post" action="http://localhost/upload_img/index.php/upload/do_upload" enctype = "multipart/form-data" >
<label> File Input: </label>
<input type="file" name="userfile" id="userfile">
<input type="submit" name="submit" value="Submit" />
</form>
Controller:
class Upload extends CI_Controller {
public function index(){
$this->load->view('upload_view');
}
public function do_upload(){
$config = array(
'upload_path' => "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf"
);
//print_r($config);
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r( $error);
}
}
}

Adding data to codeigniter subviews- returns errors

I'm using CodeIgniter file upload class.
I am unable to load the error array into the file upload form view.
(this returns a 'Undefined variable: error' message, or a ' Array to string conversion' error message)
and I am also unable to load the upload_data array to the success view.
(this returns a ' Array to string conversion' error' message.)
This is my controller - upload.php
<?php
class Upload extends Admin_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->helper('directory');
}
function index()
{
$this->data['subview'] = 'admin/upload_form';
$this->load->view('admin/_layout_main', $this->data);
}
function do_upload()
{
$this->load->library('upload');
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->data['error'] = $error;
$this->data['subview'] = 'admin/upload_form';
$this->load->view('admin/_layout_main', $this->data);
}
else
{
$upload_data = array('upload_data' => $this->upload->data());
$this->data['upload_data'] = $upload_data;
$this->data['subview'] = 'admin/upload_success' ;
$this->load->view('admin/_layout_main', $this->data);
}
}
}
?>
This is up upload form view upload_form.php
<?php echo $error;?>
<?php echo form_open_multipart('admin/upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
This is the success page view
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('admin/upload', 'Upload Another File!'); ?></p>
$upload_data = array('upload_data' => $this->upload->data());
$this->data['upload_data'] = $upload_data;
This means that when you print_r $upload_data in a view, that there is an array in there with a key of 'upload_data' and another array as a value. Then when you echo $value it returns an error because it's an array not a string.
I believe what you are looking for is just:
$this->data['upload_data'] = $this->upload->data();

Categories