0

I'm making a script that reads a .txt file and put the data of each line in this file into a database. But the problem is that the file size is 7.5mb. and php won't allow this file size.

I also wrote that "ini_set('memory_limit','-1');" isn't recommended, so i'm wondering how to fix this.

My script:

$file = fopen("./ftp_imports/ingram/ingram_fees.txt", "r");
$first_row = fgets($file);
$column = explode(",", $first_row);
$columns = count($column);

while ($row = fgets($file)) {
    $value = explode(",", $row);
    $i = 0;
    try {
        // Add a SQL-query.
        $i2 = 0;
        $sql = "INSERT INTO ingram_fees (";
        while ($i2 <= ($columns - 1)) {
            if ($i2 < ($columns - 1)) {
                $sql .= $column[$i2].",";
            } else {
                $sql .= $column[$i2];
            }
            $i++;
        }
        $sql .= ") VALUES (";
        $i2 = 0;
        while ($i2 <= ($columns - 1)) {
            if ($i2 < ($columns - 1)) {
                $sql .= ":".$column[$i2].",";
            } else {
                $sql .= ":".$column[$i2];
            }
        }
        $sql .= ")";
        $stmt = $db->prepare($sql);
        $sql_array = array();
        $i2 = 0;
        while ($i2 <= ($columns - 1)) {
            $key = ":".$column[$i2];
            $sql_array[$key] = $value[$i];
        }
        $stmt->execute($sql_values);
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

Maybe my code is a bit messy, thats because im a beginner in php :p

Can someone give me an example to fix this problem?

Thanks

Perra
  • 44
  • 2
  • 7
  • Try to change in php.ini file `max_execution_time=1800` and `memory_limit=512M` . Save it and restart your apache server – Nidhi May 30 '17 at 10:34
  • How big is your file `ingram_fees.txt` ? Maybe your code needs to be optimized for better performance. As I see you just import CSV data into a MySQL table. There are already many solutions you can find on SO: https://stackoverflow.com/questions/11448307/importing-csv-data-using-php-mysql – CodeBrauer May 30 '17 at 10:40
  • i tried what Nidhi said but after loading for 5 minutes it gave me the same error. – Perra May 30 '17 at 10:50
  • @CodeBrauer the file contains 152.000 rows of text. – Perra May 30 '17 at 10:51

0 Answers0