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.
Related
I am trying to download multiple pdf using dompdf in laravel, but below solution just concatenate into one pdf file
$weeks = ['test','test2'];
$html = '';
foreach($weeks as $hours)
{
$view = view('contract');
$html .= $view->render();
}
$pdf = PDF::loadHTML($html);
$sheet = $pdf->setPaper('a4', 'landscape');
return $sheet->download('download.pdf');
the server can only return a response, if you want several pdf files you should modify your method so that it accepts some parameter and for each parameter generate a pdf
It is not possible to download more than one file in a single HTTP request.
What you could do is pack the files into o zip and download that. You can try https://github.com/zanysoft/laravel-zip or any other available solution.
Another option would be to save the files and return only paths for the saved files back to the client. Then make a new request and download the file for each of the returned filepaths.
Also if you want to save to separate PDF files you need to move PDF creation into your foreach loop (create a PDF for each parameter).
$weeks = ['test','test2'];
$files = [];
foreach($weeks as $hours)
{
$view = view('contract');
$html = $view->render();
$pdf = PDF::loadHTML($html);
$pdf->setPaper('a4', 'landscape');
$pdf->save('documents/'.$hours.'.pdf');
$files[] = 'documents/'.$hours.'.pdf';
}
return response()->json(['files' => $files]);
In this case the foreach doesn't really do anything other than produce two identical PDF files. If you want to actually apply some kind of changes to your view based on values in $weeks you need to pass that to your view.
$view = view('contract', ['data' => $hours]);
That makes $data available in your view and you can change the resulting PDF in that way (show your contract.blade.php if you need more help regarding that).
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.
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'),
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.
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')