Ifile not getting uploaded in the database - php

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');
);

Related

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);
}
}
}

Image uploading doesn't work in codeigniter

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');

Photo uploading not working in CodeIgniter

I am trying to upload photo using codeigniter but that is not working. I don't get file name it display only error.The helper files also available in my system folder but I do not upload photo.
Controller: upload.php
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','file'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
view: upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
I always specify the input field name when calling do_upload() method. This is a fragment from working example, follow the pattern and it should work.
$field_name = 'userfile';
if (isset($_FILES[$field_name]) && is_uploaded_file($_FILES[$field_name]['tmp_name'])) {
$this->load->library('upload', $config);
if (!$this->upload->do_upload($field_name)) {
//upload failed
} else {
//upload passed
}
}
Also, make sure your upload directory is writable.

Url redirection problems uploading a file in Codeigniter 2.0.2

I am trying to upload an image using Codeigniter, following the CI user guide - but I'm getting an error due to the url redirection.
This is my view upload_form.php:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
View 2 upload_success.php:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Controller upload.php:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
I added the folder "uploads" under the "project_name" folder.
I am using this url for the upload form:
localhost/project_name/index.php/upload/
After submitting I get a "page not found" error and the url becomes:
http://localhost/project_name/index.php/upload/localhost/project_name/index.php/upload/do_upload
Based on comments under question:
$config['base_url'] = 'http://localhost/codeigniter/';
$config['base_url'] = 'http://localhost/set root folder name here/';
and use this variable <?php echo
Change the variable name of upload object anything other than $config.
eg:
$this->**config2** = array(
'upload_path' => "/root/user_name/public_html/*/files",
'upload_url' => "/root/user_name/public_html/*/files",
'allowed_types' => "pdf|doc",
'overwrite' => TRUE
);
$this->load->library('upload', $this->config2);
if(!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}

Categories