CodeIgniter Forums
Can't updload csv file with upload->do_upload() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Can't updload csv file with upload->do_upload() (/showthread.php?tid=1068)



Can't updload csv file with upload->do_upload() - bvrignaud - 02-09-2015

Hello everybody,

I try to upload csv but it doesn't work and I don't understand why.
I use the documentation's example.
This is my controller
PHP Code:
class Prof extends CI_Controller {
 
   
    public 
function import() {
 
       $this->load->helper(array('form''url'));
 
       
        $config
['upload_path'] = './tmp/';
 
       $config['allowed_types'] = 'csv'   // NOK
 
       //$config['allowed_types'] = '*';    // OK

 
       $this->load->library('upload'$config);
 
       
        if 
( ! $this->upload->do_upload())
 
       {
 
           $error = array('error' => $this->upload->display_errors());
 
           $this->load->view('prof/upload_form'$error);
 
       }
 
       else
        
{
 
           $data = array('upload_data' => $this->upload->data());
 
           $this->load->view('prof/upload_success'$data);
 
       }
 
   }


If I use  allowed_types = 'csv', it's not ok, but if I use something else like '*' it's Ok

Any Idea please ?


RE: Can't updload csv file with upload->do_upload() - mariek - 02-09-2015

First check in your config file (mimes.php) if CSV is there and what are mimes attached to it.
Than check your file mime type with something like :
Code:
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="fichier" />
<input type="submit" value="ok" />
</form>

<?php
if(isset($_POST)) {
if(isset($_FILES)) {

echo '<pre>';
var_dump($_FILES);
echo '</pre>';

}

}

?>

If the mime is not in the config array, add it. If it is.... well, dunno Wink


RE: Can't updload csv file with upload->do_upload() - Narf - 02-09-2015

(02-09-2015, 08:18 AM)mariek Wrote: First check in your config file (mimes.php) if CSV is there and what are mimes attached to it.
Than check your file mime type with something like :

Code:
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="fichier" />
<input type="submit" value="ok" />
</form>

<?php
if(isset($_POST)) {
if(isset($_FILES)) {

echo '<pre>';
var_dump($_FILES);
echo '</pre>';

}

}

?>

If the mime is not in the config array, add it. If it is.... well, dunno Wink

The MIME-type found in the $_FILES array is ignored ... that's the whole point. What you see in $_FILES is client input and should not be trusted.


RE: Can't updload csv file with upload->do_upload() - bvrignaud - 02-09-2015

My mimes.php :
PHP Code:
$mimes = array( 'hqx' => 'application/mac-binhex40',
 
'cpt' => 'application/mac-compactpro',
 
'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'),
 ...
); 

Mi view :
PHP Code:
<?=form_open('prof/import''class="form-inline"')?>
      <div class="form-group">
        <label for="inputFile">Sélectionnez un fichier :</label>
        <input type="file" class="form-control" id="inputFile">
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
    <?=form_close()?>

And I add my capture.
$this->upload->display_errors() return : Le type de fichier que vous tentez d'envoyer n'est pas autorisé. ( = The type of file you are trying to send is not allowed.).

I have no problem with other file.


RE: Can't updload csv file with upload->do_upload() - bvrignaud - 02-09-2015

I try this way : https://stackoverflow.com/questions/4731071/uploading-a-csv-into-codeigniter
An it's works !

strange ?


RE: Can't updload csv file with upload->do_upload() - bvrignaud - 02-09-2015

I have merged the CI3 mimes.php with CI2.2.1.
And it works !

Thanks for your help !


RE: Can't updload csv file with upload->do_upload() - mariek - 02-09-2015

Yep, strange, maybe CI has changed recently on this point. It works on my recent projects (CI 2.1).

@Narf : I know user input should not be trusted but it is the way CI does it. My point was only to check what mime type is detected by the script, to check if it's in CI config array.


RE: Can't updload csv file with upload->do_upload() - Narf - 02-09-2015

(02-09-2015, 09:40 AM)mariek Wrote: Yep, strange, maybe CI has changed recently on this point. It works on my recent projects (CI 2.1).

@Narf : I know user input should not be trusted but it is the way CI does it. My point was only to check what mime type is detected by the script, to check if it's in CI config array.

It's not how CI does it - that was my point.

$_FILES[$file]['type'] is an absolutely last-resort assumption in CI's MIME type detection. If you're reaching it, it means that you have none of the following:

- ext/fileinfo (finfo_file()), which is bundled and installed with PHP by default since version 5.3)
- exec(), shell_exec(), popen() and /usr/bin/file under a UNIX-based OS (or you're using Windows)
- mime_content_type()

CodeIgniter will attempt to use all of the above (in the order they're listed) before it falls back to the browser-specified value that is in $_FILES, so virtually no application should be ever relying on that one.