0

I have a script that uploads and display a CSV file into HTML table. My problem is that I have an error when I try to open big files. My file is about 4.5 Mo with 80000 lines. It works fine with small files but I get

Warning: fopen(upload/test.csv): failed to open stream: No such file or directory in /var/www/cvs/PHP charts/sb-admin-v2/test.php on line 45

with big files Here is my code

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" />
</form>
<?php
//upload
// for set memory limit & execution time
ini_set('memory_limit', '512M');
ini_set('max_execution_time', '180');
if ( isset($_POST["submit"]) ) {

   if ( isset($_FILES["file"])) {

            //if there was an error uploading the file
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

        }
        else {

                 //if file already exists
             if (file_exists("upload/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
             }
             else {
                    //Store file in directory "upload" with the name of "uploaded_file.txt"
            $storagename = "test.csv";
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $storagename);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />";
            }
        }
     } else {
             echo "No file selected <br />";
     }
}


//display
$row = 1;
if (($handle = fopen("upload/test.csv", "r")) !== FALSE) {

    echo '<table border="1">';

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        if ($row == 1) {
            echo '<thead><tr>';
        }else{
            echo '<tr>';
        }

        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />\n";
            if(empty($data[$c])) {
               $value = "&nbsp;";
            }else{
               $value = $data[$c];
            }
            if ($row == 1) {
                echo '<th>'.$value.'</th>';
            }else{
                echo '<td>'.$value.'</td>';
            }
        }

        if ($row == 1) {
            echo '</tr></thead><tbody>';
        }else{
            echo '</tr>';
        }
        $row++;
    }

    echo '</tbody></table>';
    fclose($handle);
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • have you checked if the file already exists there? – baig772 Sep 12 '14 at 22:14
  • *"My file is about 4.5 Mo"* - I'd say try and increase your memory size and execution time. – Funk Forty Niner Sep 12 '14 at 22:16
  • I did it, I delete the file from the upload/ dir, same problem. –  Sep 12 '14 at 22:17
  • @Fred-ii- , I did it . I added ini_set('memory_limit', '512M'); ini_set('max_execution_time', '180'); same problem –  Sep 12 '14 at 22:18
  • 1
    Look at the manual http://php.net/manual/en/function.fgetcsv.php - It states **length** *"Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.1.0 and later) the maximum line length is not limited, which is slightly slower."* – Funk Forty Niner Sep 12 '14 at 22:20
  • @Fred-ii-, you didnt get my probem, When I try with a file of some lines, it works, when I try with bigger file, it doesnt. all lines hav same lenght –  Sep 12 '14 at 22:22
  • Check to see if your temp folder isn't full. Also try increasing the values you already set for memory limit and execution time. Something is failing somewhere. Check your logs and add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Sep 12 '14 at 22:24
  • You may be exceeding PHP's max upload file size. See http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size – Barmar Sep 12 '14 at 22:27
  • @Fred-ii- Why would a memory or execution time limit cause a `File not found` error? – Barmar Sep 12 '14 at 22:28
  • @Barmar, Thx that's was my problem. I figured it out when I copy manually the file into the upload dir, so the problem was in the upload function. thank you –  Sep 12 '14 at 22:39
  • @Barmar You have a point there. Your comment makes more sense. I knew I was missing something. I thought it might have been a tmp file size also, filling up the temp folder; it's happened to me before. – Funk Forty Niner Sep 12 '14 at 23:23

1 Answers1

0

Well when I ran into this problem I resorted to a client side solution with https://github.com/knrz/CSV.js. Works very well and takes the load off my server. Only thing is I did not need the entire CSV to be uploaded to my server, once parsed only selective portions were sent using ajax.

If you are only trying to display the data in HTML table this can be a very good solution.

nilobarp
  • 3,806
  • 2
  • 29
  • 37