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.
Related
I have problem for upload video file with codeigniter 3. We use Linux Server Ubuntu 15.04. This is script controller for upload:
$config['upload_path'] = './uploads/videos/';
$config['allowed_types'] = 'mp4';
$config['max_size'] = 0;
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
and configuration php.ini:
post_max_size = 20000M
upload_max_filesize = 20000M
but we still error message:
The uploaded file exceeds the maximum allowed size in your PHP configuration file
Try this one
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'),
Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.
I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.
My form:
<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
My Controller function:
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$image_data = $this->upload->data();
echo '<pre>'; print_r($image_data); echo '</pre>';
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I am using
echo print_r($image_data); echo;
To display the associated image data but this is all that is returned:
Array
(
[file_name] =>
[file_type] =>
[file_path] => c:/wamp/www/honest/images/
[full_path] => c:/wamp/www/honest/images/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?
Many thanks,
DP.
You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
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.
$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;
}
}
What I am trying to do is after uploading the file parse the cvs file to the screen with print_r so I can do some things with it. I cannot seem to figure out how you access the file name with the $data. I have tried several things like $file_name, $data['file_name], and $this->data->$file_name. The relevant part is on line 37.
Not sure what I am doing wrong. I have looked at the code igniter documentation for the uploader library but it doesn't be sufficient to answer my question. Thanks for any help!
<?php
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'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('getcsv');
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);
echo 'The file name is: '.$file_name;
}
}
}
?>
The filename can be anything you want. This is clearly in the documentation, you might've been reading the wrong page or something:
http://ellislab.com/codeigniter%20/user-guide/libraries/file_uploading.html
file_name: If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type.
OR:
You can get it from $this->upload->data(), which is an array.
$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
Use the following code :
if($this->upload->do_upload($file)){
//Get uploaded file data here
$allData=$this->upload->data();
$myFile = $allData['file_name'];
}