CodeIgniter file upload black hole - codeigniter

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.

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 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

Upload the image path into db and image in the folder name upload

This is the controller:
public function category()
{
if($this->form_validation->run()==FALSE)
{
$this->load->view('admin/home');
}
else{
$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);
$image_data=$_POST['file'];
if ( ! $this->upload->do_upload())
{
// no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/home', $error);
}
else
{
// success
$data = array('upload_data' => $this->upload->data());
$this->your_upload_model->add($title, $description, $data["file_name"]);
$this->load->view('upload_success', $data);
}
}
}
This is the view:
<?php echo validation_errors();?>
<?php echo form_open_multipart('login/category');?>
<?php
if(isset($error))
{
echo $error;
}
?>
<table>
<tr>
<td align=right>Logo:</td>
<td><input type="file" name="file"></td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
I am getting two errors:
Message: Undefined index: file
You did not select a file to upload.
Even though textfield names are same and I used form_open_multipart(). Also, the image is not uploaded and the errors are not helpful.
First of all, $_POST['file'] won't work, but second, you need to supply the HTML field name in do_upload():
if ( ! $this->upload->do_upload('file'))
{
...
}
Also, remove $image_data=$_POST['file']; because it won't work anyway. You are already using $this->upload->data() which is the correct way to get uploaded file data.
The reason $this->upload->do_upload() does not work without the field name is because it looks for the field name="userfile" if the parameter is not supplied. You are using name="file".
Use this code for the first question :
public function category()
{
if (array_key_exists('submit', $_POST)) {
---------You can enter the remaining code here............
}
}
And for the second one:
if ( ! $this->upload->do_upload("file"))
{
-----------code here----------
}

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
}

Resources