Is it possible to write uploading image config code in config file/folder? - codeigniter-2

I want to put this code in config file /folder.
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpeg|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';

Why not?
$config['upload']['upload_path'] = './upload/';
$config['upload']['allowed_types'] = 'jpeg|jpg|png';
$config['upload']['max_size'] = '1000';
$config['upload']['max_width'] = '1024';
$config['upload']['max_height'] = '768';
And use it somewhere You need it like this
$this->load->library('upload', $this->config->item('upload'));

George's answer will work, but there is an official way to do it:
Simply create a new file called the upload.php, add the $config array in that file. Then save the file in: config/upload.php and it will be used automatically. You will NOT need to use the $this->upload->initialize function if you save your preferences in a config file.
With the way mentioned above you won't need to load upload library with any config it will load them automatically.
check Codeigniter user guide for further information.

Related

Error in codeigniter while file uploading

I have a problem while uploading the file this is the whole controller code:
Controller screenshot
and this is the file upload code:
$config['upload_path'] = './_uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('Upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('image')) {
echo "failed";
} else {
echo "sucess";
}
And when I run this it gives me error:
An Error Was Encountered
Resource 'upload' already exists and is not a CI_Upload instance
Are you using this code for multiple image upload? If you are using for multiple image then please load upload library $this->load->library('upload') outside loop.
Please provide full code with method name if not multiple images.
You are loading 'Upload' instead of 'upload' (upload should be in small case). and I also would like to make little more changes
$config['upload_path'] = './_uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = 'filename.png'; //extension should be same as uploaded file
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image')) {
echo "failed";
} else {
echo "sucess";
}
Hope it may help you.
The upload class must have been autoloaded already on your config /autoload.php so reinitializing the upload class will throw an error.

unable to upload an excel file in codeigniter

I have a Codeigniter project that can insert data into database through uploading an excel file and reading it with PHPExcel. It is working on my localhost however when I uploaded it in a LAMP server, It gives me an error The filetype you are attempting to upload is not allowed. How am I going to solve this?
EDIT
In my project, I also have a file image uploading..
public function uploadConfig(){
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$config['upload_path'] = './csv_uploads/';
$config['allowed_types'] = 'xlsx|csv|xls';
$config['max_size'] = '10000';
$config['overwrite'] = true;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!is_dir('csv_uploads'))
{
mkdir('./csv_uploads', 0777, true);
}
// gif|jpg|jpeg|png
}
This works on my localhost in XAMPP. However, when I uploaded it on my LAMP server, it didn't work.
For The file type you are attempting to upload is not allowed this issue occur because of your file type is wrong or not fulfill.
Please apply below step to get your file type.
You can looking at system/libraries/Upload.php line 199:
$this->_file_mime_type($_FILES[$field]);
Update that line to:
$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();
Now you can see your actual file type Like xlsx or csv or xls or ods
Now you can add your file type below code
$config['allowed_types'] = 'xlsx|csv|xls';
also add in config/mimes.php
Codeigniter 3
add to your config/mimes.php types 'application/octet-stream'
'xlsx' => array('application/octet-stream','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel', 'application/msword', 'application/x-zip')
then it will work
Go to config/mimes.php -> lines no 32 (xls) -> add 'application/vnd.ms-office' into existing array.
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel', **'application/vnd.ms-office'**)
This work only for xls for any other type get the mime type and add into this array.
In your code
$config['allowed_types'] = 'xlsx|csv|xls';
But in Ubuntu it save file types is ods format. So it will never let allow to upload. add that
$config['allowed_types'] = 'xlsx|csv|xls|ods';
And in config/mimes.php
add this
'ods' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
Provide Permeation to write in the folder
sudo chown apache:apache -R /var/www/html
and in php.ini
Check file_upload is on
and check max upload limit
as well as check max execution time
If you are using liberOffice or Open Office, You will face this problem. add "application/octet-stream" type in 'xlsx' to your application/config/mimes.php
'xlsx' => array('application/octet-stream','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel', 'application/msword', 'application/x-zip')
In Your Code Add Below Line
$config['allowed_types'] = '*';
$config['detect_mime'] = false;
this is configuration setting.

how to upload video file in database using codeigniter

I want to upload video file in mysql database
my controller coding
function add_videochk()
{
$this->load->helper('url');
$this->load->library('session');
$this->load->model("add_db");
$map=$_FILES['map']['name'];
$filename=$this->input->post('map');
if(is_uploaded_file($_FILES['map']['tmp_name']))
{
$filename = $_FILES['map']['name'];
//$config['upload_path'] = './assets/video/';
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'mp4|3gp|gif|jpg|png|jpeg|pdf';
$config['max_size']='';
$config['max_width']='200000000';
$config['max_height']='1000000000000';
// $config['image_library']='gd2';
$this->load->library('upload', $config);
$img = $this->upload->do_upload('map');
}
$data["result"] = $this->add_db->addnewvideo($map);
redirect('user/add_video','refresh');
}
when i upload image file it upload success in database and uplaods folder
but when i upload video file its upload only database not in folder
so i want to upload video file in database and its folder
please help...
Check for the post and upload file sizes first
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M`
Then inside the config/mimes.php add the missing file type you are going to uplaod, something like bellow.
'mpeg' => array('video/mpeg', 'video/quicktime'),
'mpg' => array('video/mpeg', 'video/quicktime'),
'mp4' => array('video/mp4', 'video/quicktime'),
'mpe' => array('video/mpeg', 'video/quicktime'),
'qt' => 'video/quicktime',
'mov' => array('video/quicktime', 'video/mp4'),
'avi' => array('video/avi', 'video/x-msvideo'),

CodeIgniter upload file error: you did not select a file to upload

This question has been asked before, but not with this exact issue.
I have an upload system that works great (I'm uploading CSV and parsing them).
However, if a file size is over 5Mb it gives me the error:
you did not select a file to upload
If it is under, the file uploads and parses just fine.
I've set my php.ini setting first to 128M and just now to 99999999999999999999
I've also set my CI config maz_size to 9999999999
(I was using more realistic numbers, but I wanted to be sure)
I've restarted apache with each ini change but still ths problem remains.
I've seen that the error:
you did not select a file to upload
is shown when there's a file size issue but I don't know where else to check this.
Finally, if I echo phpinfo(), I can confirm that max upload is 999999999999999
Pulling my hair out...
What else could it be?
/// update: code addition
I've got two methods of parsing the csv, either line by line or dumping the whole file direct into the DB and sort it out later. both of these work, and are selected by a config setting. that's what the line: $this->config->item('import_type') is for. Any ideas are welcome!
public function upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'txt|csv|xls|xlsx';
$config['max_size'] = '999999999999999999999';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('feed_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
if ($this->config->item('import_type') == 'db') {
$result = $this->model_feed->capture_file($this->input->post('feed_name'), $data['upload_data']['full_path']);
} else {
$result = $this->capture_feeds($this->input->post('feed_name'), $data['upload_data']['full_path']);
}
if ($result) {
$this->load->view('feed_upload_success', $data);
} else {
$this->load->view('feed_form', array('error' => 'The file you supplied does not match the data structure we were expecting.' ));
}
}
}
You have to increase post_max_size on php.ini too
http://www.php.net/manual/en/ini.core.php#ini.post-max-size
i had a same problem and what i did is on $this->upload->do_upload() function i have passed the name of the input file i mean from the form input type file name. Suppose i gave the name as avatar so i used the function as $this->upload->do_upload('avatar')

CodeIgniter and autoloading the database library

I've got an issue connecting to the database with CodeIgniter.
In the model, if I do
$this->load->database();
then a query such as
$query = $this->db->get('articles', 10);
works and returns data. However, when I remove the load->database line and try autoloading the database library, using
$autoload['libraries'] = array('database');
in application/config/autoload.php, the above query fails. As it works when I explicitly load the library, it's not a problem with my database connection. I'm using CodeIgniter 2.0.2 (the current latest release).
As most of my pages use the database I'd like to autoload it to avoid having to load it anually in each model. Have I misunderstood the documentation regarding loading the database library or am I doing something wrong?
I know I am probably just answering a dead question but i had the same problem.
if you are using eclipse pdt or similar IDE and modified the system>core>model.php by adding some variables just before the constructor to get code completion then the autoload doesnt work
i dont know why but the fact is it doesnt work. I restored the core>model.php to original and autoload works fine. I dont get autoload now and its really a bad experience for new coder to CI.
If you have a workaround please comment so.
This is just an alternative if you still can't resolve the issue. Just extend the CI_Model and auto load the database library in the constructor. Then you can just make all other models inherit from that base class.
Example:
<?php
class My_Model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
}
And the all others will start like :
<?php
class Some_Model extends MY_Model{}
This method has the other benefit that you don't have to include the loading code line in every model you create. Also you can easily push shared functionalities among models into the parent MY_Model class.
Hope it helps!
This is my database.php from my application/config/ directory
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'mydbhost';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydatabase';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Check yours looks like this and rerun through the user guide at http://codeigniter.com/user_guide/database/configuration.html and reread the comments in the autoload.php file in the config directory.
The relevant line in mine reads:
$autoload['libraries'] = array('database', 'form_validation', 'session');
You should be loading the db library like this:
$this->load->library('database');
The only thing I can think of is in your application/config/database.php file, the following line is set to false, instead of true.
//['autoinit'] Whether or not to automatically initialize the database.
$db['default']['autoinit'] = TRUE;
Everything else looks ok.
Shababhsiduque,
Just you we're right, at least in my case!
The workaround to getting the ide to work is creating the reference variables in ANOTHER CI project.
That is leave the system model as it is in the current project but modify it in another project.
Then change the php buildpath (project->properties->phpbuildpath) to the project with the variables.
Note this are settings for Aptana Studio 3.
Worked for me.
You could try PHP Autoload Class Magic function in this case.
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'libraries/'. $class . '.php' );
}
}
This code must copied inside the config.php (after the last line) within Application folder.
This code will automatically load libraries for you when its needed.
It might work. Happy coding.
In my case the problem was because there was another occurrence of $autoload['libraries'] inside the same file and it reset the array.

Resources