0

Using php 5.4.34 And Laravel 4 with apache 2.2.22 and Ubuntu.

Using the library https://github.com/goodby/csv to parse a csv uploaded.

here is my code :

 $file = Input::file('file');
            //echo $file->getClientOriginalName();
$config = new LexerConfig();
$config
                ->setDelimiter(";")
                ->setToCharset('UTF-8')
;
$lexer = new Lexer($config);
$interpreter = new Interpreter();

$salarie_csv = [];
$errors = [];
$lineNb = 0;
$interpreter->addObserver(function (array $rows) use (&$salarie_csv, &$lineNb, &$errors) {
    //some code
});
$lexer->parse($file, $interpreter);

return Response::json($errors, 200);

When I upload a 1.5Mb size csv with 20.000 rows it works.

When I upload a 2.5Mb size csv with 38.500 rows it give me the error :

 SplFileObject::__construct():Filename cannot be empty in Lexer.php line 50.

i tried with the same file (just removed or add some rows for the test)

Is there a way to fix this ?

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
BastienSander
  • 1,718
  • 4
  • 24
  • 50
  • If it expects a string name, shouldn't it be `Input::file('file')->getRealPath();`, or is your Goodbye/CSV Laravel-aware? – Wrikken Nov 24 '14 at 12:31
  • 3
    Hi @BastienSander, did you check the file size limit set in the php.ini file? The default size is 2mb usually. So just a guess that it might cause a problem. – CuriousMind Nov 24 '14 at 12:33
  • @Wrikken : It needs the file input, not a string. – BastienSander Nov 24 '14 at 12:49
  • @CuriousMind : It does (20MB for upload max filesze & 25MB for post max size) but still get the same error. – BastienSander Nov 24 '14 at 12:49
  • @BastienSander: the _name of a file_ ***is*** a _string_, not an object. Especially if [SplFileObject](http://php.net/manual/en/class.splfileobject.php) explicitly says so. – Wrikken Nov 24 '14 at 14:01

2 Answers2

4

Check your post_max_size and upload_max_filesize in your php.ini config file. PHP probably does not allow too big files to be uploaded, so it cuts it off from post.

var_dump( ini_get('post_max_size') );

Note, that post_max_size overrides upload_max_filesize (as explained in answer here), you should make sure that both of those settings allow sizes that you'll be uploading.

Community
  • 1
  • 1
Kleskowy
  • 2,648
  • 1
  • 16
  • 19
  • It does (20MB for upload max filesze & 25MB for post max size) but still get the same error. – BastienSander Nov 24 '14 at 12:48
  • ini my php.ini I do have the settings I said, but actually when I do ini_get('post_max_size') and ini_get('upload_max_filesize') it is not the same. However after editing php.ini, I did service apache2 restart ? – BastienSander Nov 24 '14 at 12:57
  • 1
    Make sure you restarted apache and the PHP config reads from the file you were editing (sometimes you have multiple, like en example file and an actual configuration file). – Kleskowy Nov 24 '14 at 14:02
  • Solved, I was editing /etc/php5/cli/php.ini instead of /etc/php5/apache2/php.ini Thanks for help ;) – BastienSander Nov 24 '14 at 14:04
0
Try This..........



$type=$_FILES['file']['type'];
    $filename=$_FILES["file"]["tmp_name"];
     $filename_csv = explode(".", $_FILES["file"]["name"]);
    $extension = end($filename_csv);
    if($extension=="csv")
    {
        if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");

        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {

        $sql = mysql_query("insert into upload_data(spent,click,filename,date) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')") or die (mysql_error());
        mysql_query($sql);
        }
        fclose($file);
        echo $error1=ucwords('<div style="margin-left:60px;position: absolute;width: 400px; color: #006400;">CSV File has been successfully Imported</div>');

        }
        }
        else
        {
        echo $error1=ucwords('<div style="margin-left:60px;position: absolute;width: 400px; color: #CC0000;">Invalid File:Please Upload CSV File</div>');
       // echo 'Invalid File:Please Upload CSV File';
        }
Hara Prasad
  • 704
  • 6
  • 15
  • 2
    mysql_* functions are deprecated! go for PDO or mysqli, and anyway, you call the same query twice, I'm not sure if that's working. – Raphael Müller Nov 24 '14 at 12:40