codeigniter issue in uploading csv file in cpanel - php

I'm getting following Error:
The filetype you are attempting to upload is not allowed.
i want to upload csv file.
i'm using codeigniter file upload method do_upload
and i also supply allowed_types as csv
public function csvRateList(){
$redirectData=['error'=>'','success'=>''];
$type=$this->input->post('type');
date_default_timezone_set('Asia/Kolkata');
$config['upload_path'] ='./csv/';
$config['allowed_types'] = 'csv'; //type of file
$config['max_size'] = '100';
$this->load->library('upload',$config);
$query = $this->db->get_where('csv_rate_list', array('type' => $type));
if($query->num_rows()==0){
$query = $this->db->get_where('rate_list', array('type' => $type));
if($query->num_rows()==0){
if($this->upload->do_upload()){
$fdata=$this->upload->data();
$newName=$fdata['file_name'];
$origName=$fdata['orig_name'];
$data = array(
'type' => $type ,
'new_name' => $newName ,
'orig_name' => $origName,
'timestamp' =>time()
);
$this->db->insert('csv_rate_list', $data);
}else{
$redirectData['error']=$this->upload->display_errors();
redirect(base_url().'add_rate');
}
$redirectData['success']='Successfully inserted!';
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}else{
$redirectData['error']='Service type already exists. in old table';
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}
}else{
$record=$query->row_array();
$id=$record['id'];
$old_name=$record['new_name'];
if($this->upload->do_upload()){
$fdata=$this->upload->data();
$newName=$fdata['file_name'];
$origName=$fdata['orig_name'];
$data = array(
'type' => $type ,
'new_name' => $newName ,
'orig_name' => $origName,
'timestamp' =>time()
);
$this->db->where('id', $id);
$this->db->update('csv_rate_list', $data);
unlink('./csv/'.$old_name);
$redirectData['success']='Successfully updated!';
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}else{
$redirectData['error']=$this->upload->display_errors();
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}
}
}

You have to modify few line in (/system/libraries/Upload.php)
from:
$this->file_type = #mime_content_type($file['tmp_name']);
return;
to this:
$this->file_type = #mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return;

Check the config/mimes.php have the correct value for "csv"
In $mimes array,find the 'csv' and check the following.
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
If not, add this into it.

Related

Codeigniter Image Upload PathInfo Extension 'The filetype you are attempting to upload is not allowed'

I am uploading multiple images and I need to get their different file format.
But the problem is i only got the first file image format and there's an error when I do resize images because of the incorrect file format.
I am having an error The filetype you are attempting to upload is not allowed. which occur when I put the PATHINFO_EXTENSION when getting file.
Here's my code
for($i = 0; $i < $count_upload; $i++) {
$_FILES['userfile']['name'] = pathinfo($_FILES['product_image']['name'][$i], PATHINFO_EXTENSION);
$_FILES['userfile']['type'] = $_FILES['product_image']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['product_image']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['product_image']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['product_image']['size'][$i];
$file_name = $file_name;
$config = array(
'file_name' => $file_name,
'allowed_types' => 'jpg|jpeg|png',
'max_size' => 3000,
'overwrite' => TRUE,
'encrypt_name' => FALSE,
'remove_spaces' => TRUE,
'upload_path'
=> $directory
// => './assets/img/products'
);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$response = array(
'status' => false,
'environment' => ENVIRONMENT,
'message' => $error['error'],
'csrf_name' => $this->security->get_csrf_token_name(),
'csrf_hash' => $this->security->get_csrf_hash()
);
echo json_encode($response);
die();
}else {
$file_name = $this->upload->data()['file_name'];
echo $file_name;
echo '<br>';

uploading docx file in codeigniter

mimes.php
'doc' => array('application/msword', 'application/vnd.ms-office'),
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
'dot' => array('application/msword', 'application/vnd.ms-office'),
'dotx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip', 'application/msword'),
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel', 'application/msword', 'application/x-zip'),
'word' => array('application/msword', 'application/octet-stream'),
var_dump is giving following response
string(24)"application/octet-stream"
This is my some controller code
if(!empty($_FILES['file_name']))
{
// Profile Pic upload
$config = array();
$config['upload_path'] = 'uploads/file/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|csv|txt|doc|docx|rar|zip|svg|xml|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx|CSV|TXT|SVG';
$config['max_size'] =10000;
$config['max_width'] = 1024;
$config['max_height'] = 1024;
$config['file_ext_tolower'] = true;
$this->load->library('upload', $config, 'profilepic');
$this->profilepic->initialize($config);
if(!$profile_pic = $this->profilepic->do_upload('file_name'))
{
$this->session->set_flashdata('message', $this->upload->display_errors());
}
else
{
$imgname = $this->profilepic->data();
$imgpath = 'uploads/file/'.$imgname['file_name'];
$data['file_name'] = $imgpath;
}
}else{
$this->session->set_flashdata('message',
'Please
I tried all the possible solution available but not able to succeed.Please suggest.
I missed docx in controller and adding 'application/octet-stream' in docx mimes is solving problem.Thanks for your effort and time.

Can not insert the data with image upload

I made insert data along upload images into the database , when the run was successful , but when the insert data and upload images contained errors.
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
how to cope if I insert the data without uploading images is not error ? and the image database went into default image ?
This my controllers
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => $url.$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
}
else
{
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
}
This my model
function savealat($nama_foto) {
$data = array(
'id' => $this->input->post('id'),
'namaalat' => $this->input->post('namaalat'),
'dayalistrik' => $this->input->post('dayalistrik'),
'merk' => $this->input->post('merk'),
'namasupplier' => $this->input->post('namasupplier'),
'nokatalog' => $this->input->post('nokatalog'),
'noseri' => $this->input->post('noseri'),
'category' => $this->input->post('category'),
'lokasi' => $this->input->post('lokasi'),
'pengguna' => $this->input->post('pengguna'),
'status' => $this->input->post('status'),
'jadwalkal' => $this->input->post('jadwalkal'),
'manual' => $this->input->post('manual'),
'dateinput' => $this->input->post('date'),
'foto' => $nama_foto
//'created' => $tanggal
);
$this->db->insert('tbdetail', $data);
}
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => base_url().$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
else
{
//Moved your code up there
}
}
If I'm right the problem is that you put the upload in else. Try to move the code and tell me if it works
MaY be this answer can help you out. There are few more suggestion in comment part which can figure some way out for you. Your server does not support the GD function required to process this type of image.Ci

Cant Upload doc format files in codeigniter

$config['allowed_types'] = 'doc|Doc';
I want to upload Doc format file only but cant upload doc format file just written The filetype you are attempting to upload is not allowed.
Why?
How to solve issue,
I add mimes file,
'doc' => 'application/msword',
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
you can use this function
public function do_upload(){
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}

Inserting path of a image into database using codeigniter

Here is my controller insertion code
This code inserts image into image path folder but the path is not saving in database.
function add_hotel() {
//validate form input
$this->form_validation->set_rules('hotelname', 'Hotel Name', 'required|xss_clean');
$this->form_validation->set_rules('hotellocation', 'Hotel Location', 'required|xss_clean');
$this->form_validation->set_rules('hotelphone', 'Hotel Phone', 'required|xss_clean');
$this->form_validation->set_rules('hotelimg', 'Hotel Image ', 'callback__image_upload');
$this->form_validation->set_rules('hotelabout', 'Hotel About', 'required|xss_clean');
if ($this->form_validation->run() == true)
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$field_name = "hotelimg";
if ( ! $this->upload->do_upload($field_name))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/add_hotel', $error);
}
else {
$data = array(
'hotelname' => $this->input->post('hotelname'),
'hotellocation' => $this->input->post('hotellocation'),
'hotelphone' => $this->input->post('hotelphone'),
'hotelimg' => $this->upload->data('hotelimg'),
'hotelabout' => $this->input->post('hotelabout')
);
print_r($data);
$this->db->insert('hotel_content', $data);
$this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");
redirect(base_url().'index.php/admin/hotel_controller/index');
}
Error shown is:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552
and
A Database Error Occurred:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `hotel_content` (`hotelname`, `hotellocation`, `hotelphone`, `hotelimg`, `hotelabout`) VALUES ('hotel5', 'hyd', '0402365477', Array, 'welcome')
Filename: G:\wamp\www\CodeIgniter\system\database\DB_driver.php
Line Number: 330
I need path to be inserted in database. Can anyone help me?
replace else part with
else {
$data = array(
'hotelname' => $this->input->post('hotelname'),
'hotellocation' => $this->input->post('hotellocation'),
'hotelphone' => $this->input->post('hotelphone'),
'hotelimg' => $this->upload->data('hotelimg'),
'hotelabout' => $this->input->post('hotelabout')
);
print_r($data);
$this->db->insert('hotel_content', $data);
$this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");
redirect(base_url().'index.php/admin/hotel_controller/index');
}
with this
else {
$image_path = $this->upload->data();
$data = array(
'hotelname' => $this->input->post('hotelname'),
'hotellocation' => $this->input->post('hotellocation'),
'hotelphone' => $this->input->post('hotelphone'),
'hotelimg' => $image_path[full_path],
'hotelabout' => $this->input->post('hotelabout')
);
print_r($data);
$this->db->insert('hotel_content', $data);
$this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");
redirect(base_url().'index.php/admin/hotel_controller/index');
}
the line "$this->upload->data('hotelimg')" in else part returns an array.
You just need uploaded path from it which can be extracted as:
$temp = $this->upload->data('hotelimg');
$uploadedPath = $temp[full_path];

Categories