Error in codeigniter while file uploading - codeigniter

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.

Related

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

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

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.

Laravel 4 get image from url

OK so when I want to upload an image. I usually do something like:
$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
if( $uploadSuccess ) {
// save the url
}
This works fine when the user uploads the image. But how do I save an image from an URL???
If I try something like:
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
and then:
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
I get the following error:
Call to a member function move() on a non-object
So, how do I upload an image from a URL with laravel 4??
Amy help greatly appreciated.
I don't know if this will help you a lot but you might want to look at the Intervention Library. It's originally intended to be used as an image manipulation library but it provides saving image from url:
$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');
$url = "http://example.com/123.jpg";
$url_arr = explode ('/', $url);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$destinationPath = public_path().'/img/'.$name;
file_put_contents($destinationPath, file_get_contents($url));
this will save the image to your /public/img, filename will be the original file name which is 123.jpg for the above case.
the get image name referred from here
Laravel's Input::file method is only used when you upload files by POST request I think. The error you get is because file_get_contents doesn't return you laravel's class. And you don't have to use move() method or it's analog, because the file you get from url isn't uploaded to your tmp folder.
Instead, I think you should use PHP upload an image file through url what is described here.
Like:
// Your file
$file = 'http://....';
// Open the file to get existing content
$data = file_get_contents($file);
// New file
$new = '/var/www/uploads/';
// Write the contents back to a new file
file_put_contents($new, $data);
I can't check it right now but it seems like not a bad solution. Just get data from url and then save it whereever you want

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

Resources