Upload file to database using codeigniter - codeigniter

I need to upload a file in a mysql database using codeigniter.
My view is:
<?php echo form_open_multipart('home/addpagina2'); ?>
<label for="titolo">Titolo:</label>
<input type="text" size="20" id="titolo" name="titolo"/>
<br/>
<label for="testo">Testo</label><br/>
<textarea name="testo" cols="50" rows="10"></textarea>
<br/>
<label for="file">File:</label>
<input type="file" name="file" />
<br />
<input type="submit" value="Aggiungi"/>
</form>
and controller is:
function addpagina2(){
$this->form_validation->set_rules('titolo', 'Titolo', 'trim');
$this->form_validation->set_rules('testo', 'Testo', 'trim');
if($_FILES["file"]["size"] > 0){
$tmpName = $_FILES["file"]['tmp_name'];
$fp = fopen($tmpName, 'r');
$file = fread($fp, filesize($tmpName));
$file = addslashes($file);
fclose($fp);
}
$pagina = array();
$pagina['titolo'] = $this->input->post('titolo');
$pagina['testo'] = $this->input->post('testo');
$pagina['file'] = $file;
$this->db->insert('pagine', $pagina);
redirect('home/pagine');
}
When I want to display the file I call this view:
<?php
header("Content-type: ".$file['type']);
echo $file;
?>
that is called by this controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
}
function id($id){
$immagine = $this->db->get_where('pagine', array('id' => $id));
$data['file'] = $immagine->row()->file;
$this->load->view('file', $data);
}
}
the problem is that I have an error while i call the view in the browser, as you can see here: http://mattialori.net/amv/index.php/file/id/2
If I upload a file on the database using phpmyadmin it works.
How can I do?

Why don't you just save the file name into the db?
Then you can upload the img in a specific folder and in your view yuo can set the full path from your folder and append the file name at the end.
EDIT:
you can use the File Upload class in this way:
//Check if the file exists in the form
if($_FILES['file']['name']!=""){
//load library
$this->load->library('upload');
//Set the config
$config['upload_path'] = './uploads/'; //Use relative or absolute path
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = FALSE; //If the file exists it will be saved with a progressive number appended
//Initialize
$this->upload->initialize($config);
//Upload file
if( ! $this->upload->do_upload("file")){
//echo the errors
echo $this->upload->display_errors();
}
//If the upload success
$file_name = $this->upload->file_name;
//Save the file name into the db
}

Related

Upload Image Using Path Codeigniter

I am facing problem in my uploading file using codeigniter.When I create a leave request and upload the image, the image did not appear in the folder path and also did not have a path at the database. Actually, I am doing Leave Request Management I already insert coding upload in my controller. This is my controller leaves.php
Controller (leaves.php)
public function upload ()
{
$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$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('create', $error);
}
else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
$this->load->view('view', $data);
}
This is my view. Actually I has 2 view that called create.php and view.php.
Create.php is request leave and also for uploading file and for view.php is when user want to view their leave request and also the uploading image. This is my code for both view.
View(create.php)
<? echo form_open_multipart('create/upload');?>
<input type="file" name="image" class=" inputfile" value="<?php echo set_value ('image'); ?>" />
View(view.php)
<label for="image"><?php echo lang ('leaves_view_field_image'); ?>Uploaded File</label>
<img src="?php echo $leave['image']"><?php echo $leave['image']; ?>
When I upload the image with the leave request, the image uploading only fetch name file not the whole file in the database. Can someone help me ?
public function upload ()
{
$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('create', $error);
}
else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
$this->load->view('view', $data);
}
}
Note: make sure folder read/write permission give in linux system
Here is the error if ( ! $this->upload->do_upload()) change it to if ( ! $this->upload->do_upload('image')) where "image" is the same thing as "$_FILES['image']" that is the val in your html e.g <input type="file" name="image">.
I hope this will help you to upload you file.

I can not upload a file in codeigniter

I cannot upload a file in codeigniter. Code I'm using:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->library('upload');
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('User_m');
}
public function form()
{
if($_POST){
//print_r($_POST);exit;
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
//Load upload library and initialize configuration
//print_r($config);exit;
$this->load->library('upload',$config);
//$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$uploadData = $this->upload->data();
$userfile = $uploadData['file_name'];
//echo $userfile;exit;
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
$this->User_m->form_insert($data);
}
$this->load->view('form');
}
}
This is my controller function code for processing multipart form data in codeigniter. But file is not uploading.
as you have given code i have modified code to trace it.Here below i have attached the view file as well as controller file :
1) Below is a view file code.In this i have submitted form by giving method name in action attribute.If you are submitting the form through ajax then you can pass the method name over there.
<div id="container">
<h1>Please Fill up the Registration Form.!</h1>
<div id="body">
<!-- <?php echo $error;?> -->
<form method="post" action="<?php echo site_url() . '/Main/form' ?>" enctype="multipart/form-data"> Full name:<br> <input type="text" name="name"><br> Email:<br> <input type="text" name="email"><br> Phone:<br> <input type="text" name="phone"><br><br> Image:<br> <input type="file" name="userfile" /> <br /><br /> <input type="submit"><br> </form>
</div>
</div>
2) Below is a controller code.
<?php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','html'));
}
public function index()
{
$this->load->view('form');
}
public function form()
{
if($_POST){
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
//Load upload library and initialize configuration
//print_r($config);exit;
$this->load->library('upload',$config);
//$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$uploadData = $this->upload->data();
$userfile = $uploadData['file_name'];
//echo $userfile;exit;
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
// Below $data getting all the form data
print_r($data);
}
$this->load->view('form');
}
}
Hope this will help you!
You can try this
public function form()
{
if($_POST){
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
$this->upload->initialize($config);
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$userfile = $_FILES['userfile']['name'];
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
$this->User_m->form_insert($data);
}

Codeigniter Modular HMVC extension - cant upload file

I have a problem with uploading files by form. Everytime I try, I get the error "File type not allowed". I am using HMVC modular extension and my controllers extends MX_Controller.
I already tried to $var_dump($_FILES) from my form-view and the result is fine, but when I try to use method do_upload() in controller it doesnt work.
Does anyone have any idea? Here is the controller:
Class Store_data extends MX_Controller
{
function __construct()
{
parent::__construct();
}
function upload_pic()
{
//$data['item_id'] = $item_id;
$template = "bootstrap_theme";
//$current_url = current_url();
//$data['form_location'] = $current_url;
$data['view_file'] = "upload_pic";
$this->load->module('template');
$this->template->$template($data);
}
function do_upload()
{
//var_dump($_FILES);
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '4024';
$config['max_height'] = '4768';
$this->load->library('upload', $config);
$this->upload->overwrite = true;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$template = "bootstrap_theme";
$data['view_file'] = "upload_pic";
$this->load->module('template');
$this->template->$template($data);
}
else
{
$data['upload_data'] = $this->upload->data();
$template = "bootstrap_theme";
$data['view_file'] = "upload_success";
$this->load->module('template');
$this->template->$template($data);
}
}
And here is the upload form view:
<?php echo $error;?>
<?php echo form_open_multipart('store_data/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
I solved the problem.
I had to uncomment extension=php_fileinfo.dll in php.ini and comment ;extension=php_mime_magic.dll
Thanks anyway

codeigniter cant upload image

i have a problem with codeigniter upload form
View.php I found this code in Codeigniter documentation and i dont know where is the problem..
<?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>
upload.php Thats upload.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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);
}
}
}
?>
When i upload some image ci give me
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Upload::$upload
Filename: controllers/Upload.php
Line Number: 52
Backtrace:
Fatal error: Call to a member function do_upload() on a non-object in
Where is the problem ?Thanks
EDIT:
From the link you posted I followed the guide on uploading the file and it worked perfectly fine.
Make sure you have CI setup properly:
The folders application and system and the file index.php must be uploaded to your working webserver, you must then create a folder called uploads, make sure this folder is writable by the proper user, for my system it is www-data. I'm using apache2 with linux so my webroot is var/www.
So you should see in the primary folder:
application
system
uploads
index.php
After you do that you will go to:
localhost/index.php/upload/ or your proper path for index.php/upload/ and test this again.

picture upload in codeigniter

Everything is fine, but when I open the upload folder there is not any image.
here is the controller "admin_news". I try to upload the image file in localhost/site/asset/upload. But when i click submit only the information(title, news and category) goes into datebase, but the file is not uploaded.
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'html', 'file'));
$config['upload_path'] = base_url('asset/upload/');
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
}
public function add_news(){
$this->load->helper('form');
$this->load->model('AddNews');
$this->load->view('templates/header', $data);
$this->load->view('admin/add_news');
$this->load->view('templates/footer');
if($this->input->post('submit')){
if ($this->upload->do_upload('image'))
{
$this->upload->initialize($config);
$this->upload->data();
}
$this->AddNews->entry_insert();
redirect("admin_news/index");
}
}
The view has only:
<?php echo form_open_multipart(''); ?>
<input type="text" name="title" value="Title..." onfocus="this.value=''" /><br />
<input type="file" name="image" /><br />
....
This should not be a URL:
$config['upload_path'] = base_url('asset/upload/');
It should be a path to somewhere on your server, either a full absolute path or a relative one (all paths in Codeigniter are relative from index.php).
Use either one of these:
// Full path
$config['upload_path'] = FCPATH.'asset/upload/';
// Relative
$config['upload_path'] = './asset/upload/';
One more thing:
if ($this->upload->do_upload('image'))
{
// You don't need this, and besides $config is undefined
// $this->upload->initialize($config);
// You don't seem to be doing anything with this?
$this->upload->data();
// Move this here in case upload fails
$this->AddNews->entry_insert();
redirect("admin_news/index");
}
// Make sure to show errors
else
{
echo $this->upload->display_errors();
}
I think in your code, problem is you have declared configuration after do_upload method
if ($this->upload->do_upload('image'))
{
$this->upload->initialize($config);
$this->upload->data();
}
configuration should be initialized before use the method. I think that was the problem. so you have to do configuration before the do_upload method

Resources