picture upload in codeigniter - image

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

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.

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.

Upload file to database using 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
}

CodeIgniter file upload black hole

I am trying to upload a single file in CodeIgniter 2.0.2 in a normal way.
The do_upload() function returns true but no file appears in the directory.
Directory permissions appear to be fine (after running other diagnostics not shown in code below), so it makes no sense that the file is not appearing in the upload directory.
Here is controller and view code, adapted straight from the CI docs.
Controller:
function do_upload_sql(){
// create directory
if (! is_dir(BACKUP_DIR)) {
mkdir(BACKUP_DIR, 0777);
}
// or if it exists and has restricted file permissions, change them
elseif(fileperms(BACKUP_DIR)!=0777){
chmod(BACKUP_DIR, 0777);
}
$config['upload_path'] = BACKUP_DIR;
$config['allowed_types'] = 'backup'; // an SQL backup file type
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
//$this->upload->initialize($config); //redundant, so commenting out
if ( ! $this->upload->do_upload())
{
$data['action'] = 'c_backup_restore/do_upload_sql';
$tab_error = array('error' => $this->upload->display_errors());
$data['error'] = $tab_error['error'];
$this->load->view('common/header');
$this->load->view('v_upload_sql', $data);
}
else
{
echo "success"; // yes it's getting here, but i get no file!
$data = array('upload_data' => $this->upload->data());
$file_upload = $data["upload_data"];
$this->restore_backup($file_upload); // go do something useful with the file
}
}
View:
<p>Select the backup file</p>
<div class="not_success"><?php echo $error;?></div>
<?php echo form_open_multipart($action);?>
<input type="file" name="userfile" size="30" />
<input type="submit" value="Upload" />
</form>
I found the bug using the xDebug profiler.
It turned out that the native php function chdir(path) further downstream was the problem.
#qwertzman's link to paths in CI may prove to be helpful to determining exactly why the hangup occurs.

Resources