Uploading CSV file in CodeIgniter 3 - php

I'm having issues uploading a CSV file in codeigniter.
I am getting the error:
array(1) { ["error"] => string(64) "The filetype you are attempting to upload is not allowed." }
Ok!!
but I am setting allowed_types like this:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
and i have this in mimes.php:
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'),
So any ideas why I am still getting this error?
$_FILES dump:
Array
(
[name] => default questions.csv
[type] => application/force-download
[tmp_name] => /tmp/phpbkXxUd
[error] => 0
[size] => 5899
)

Try this
$config['allowed_types'] = '*';
This should work
.Also check your upload.php or mimes.php. Use the latest one.

Related

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.

how to get length of audio file in codeigniter while upload file

I have an issue related file upload,
I want to get the audio file's duration length in minutes.
While I am uploading that file, I am able to get all the details like size, type, max-length, file name but not the duration of that audio file.
$config['upload_path'] = './uploads/songs/';
$config['allowed_types'] = 'gif|jpg|png|mov|mp3|aiff|mpeg|zip';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['file_ext_tolower'] = 'TRUE';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$uploadSong = $this->upload->data();
print_r($uploadSong);
after print_r am getting this output of uploaded file
Array (
[file_name] => song_one1.mp3
[file_type] => application/octet-stream
[file_path] => C:/xampp/htdocs/rockherogame/uploads/songs/
[full_path] => C:/xampp/htdocs/rockherogame/uploads/songs/song_one1.mp3
[raw_name] => song_one1
[orig_name] => song_one.mp3
[client_name] => song_one.mp3
[file_ext] => .mp3
[file_size] => 4059.28
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] => )
please help me understand how to get duration of file?
Thanks in advance
This should work for you, notice the getduration function: http://www.zedwood.com/article/127/php-calculate-duration-of-mp3
Duration of a audio file needs to be calculated.
here is a good example showing how to do the same in php
http://www.zedwood.com/article/127/php-calculate-duration-of-mp3
you can also Install and use getid3.
code snippet here

Codeigniter error: The filetype you are attempting to upload is not allowed

If I upload a csv file, there is no problem on localhost and everything works fine, but when I upload my app on live server and upload a csv file then this error thrown: The filetype you are attempting to upload is not allowed. I am confused as to why this happens. Please help me to solve this.
For my localhost environment, I am using XAMPP and CodeIgniter.
I only want to allow csv file uploads.
check 2 things:
First:
in your upload controller: make sure to set the correct allowed types
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
Second:
update the array $mimes in your config/mimes.php:
'csv' => array('application/vnd.ms-excel',
'text/anytext',
'text/plain',
'text/x-comma-separated-values',
'text/comma-separated-values',
'application/octet-stream',
'application/vnd.ms-excel',
'application/x-csv',
'text/x-csv',
'text/csv',
'application/csv',
'application/excel',
'application/vnd.msexcel')
UPDATE:
you could use print_r($_FILES) in your upload controller to check for the mime-type missing. this would output something like:
[userfile] => Array
(
[name] => teste1.csv
[type] => application/vnd.ms-excel
[tmp_name] => C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\binaries\tmp\php8BFD.tmp
[error] => 0
[size] => 7880
)
Add ‘text/plain’ to the CSV array in config/mimes.php to $mimes arrays, that is
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'),

issue with uploading mp4 video file even after changing the mime type to 'mp4' => video/mp4 and giving file type as mp4

I am trying to upload a mp4 video file using codeigniter file upload class as follows,
function do_upload_video()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'mp4';
$config['mimes'] = 'mp4';
$config['max_size'] = '1000000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] ='admin/elements/add_video';
$this->load->view('includes/template', $data);
}
else
{
$chapter_id=$this->session->userdata('chapter_id');
redirect("/admin/elements/".$chapter_id);
}
}
mime type given in mimes.php
'mp4' => 'video/mp4'
further, I have also increased the file size in php.ini post_upload_max size and upload_max_filesize to sufficient level.
However when i am trying to upload a sample video file of size 8 MB with .mp4 extension it is showing error as "The filetype you are attempting to upload is not allowed".
Array ( [video] => Array ( [name] => daddyshome_paLrcx9H.mp4 [type]=>video/mp4 [tmp_name] => /tmp/php3xrVFp [error] => 0 [size] => 7350845 ) )
getting this on print_r($_FILES)
Set MIME type for MP4 in mime.php
'mp4' => array('video/mp4', 'application/octet-stream'),
I removed
$config['mimes'] = 'mp4';
from my controller and it is working properly.

Trying to get file_name from codeigniter after uploading

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'];
}

Categories