Home>

I want to read csv file information from a PHP file installed in EC2.
If i hit the global IP on the browser, there will be no error message in the PDO connection, so I think you can connect to the DB.
If the code for reading csv file is added, it cannot be read due to an error (HTTP ERROR 500).
Since I heard that reading is fast, I use the SplFileObject function.
I would appreciate any advice.

<? php
try {
    $file = new SplFileObject ('/ home/ec2-user/winneyuser.csv', r);
    $file->setFlags (SplFileObject :: READ_CSV);
} catch (RuntimeException $e) {
    throw $e;
}
try {
    $pdo = new PDO (
        'mysql: dbname = DBname;host = database-1.cuiejdfzi0uf.ap-northeast-1.rds.amazonaws.com;charset = utf8mb4',
        'username',
        'pass',
    [
        PDO :: ATTR_ERRMODE =>PDO :: ERRMODE_EXCEPTION,
        PDO :: ATTR_DEFAULT_FETCH_MODE =>PDO :: FETCH_ASSOC,
    ]
);
} catch (PDOException $e) {
    header ('Content-Type: text/plain;charset = UTF-8', true, 500);
    exit ($e->getMessage ());
}
header ('Content-Type: text/html;charset = utf-8');
Code
  • Answer # 1

    new SplFileObject ('/ home/ec2-user/winneyuser.csv', r);
    Code


    It was solved by deleting the above -r!

  • Answer # 2

    In the meantime, DB operations are not related to CSV reading, so how about executing with minimal code?

    If you are using a framework, it is easier to isolate the problem if you consider running it with the minimum amount of code outside the framework and then incorporating it into the framework.

    First, delete all other sources

    setFlags (SplFileObject :: READ_CSV);
    } catch (RuntimeException $e) {
        throw $e;
    }

    PHP manual
    If you add error display and CSV reading code, and read the error, you will be able to make the minimum code.

    setFlags (SplFileObject :: READ_CSV);
    // Add code for CSV reading/displaying here
    } catch (Exception $e) {
        // display exceptions on the fly
        echo $e->getMessage ();
        echo PHP_EOL;
        echo $e->getTraceAsString ();
        exit ();
    }
    How to check the minimum code without affecting the existing environment

    The source code is/home/ec2-user/read_csv.php,
    After logging in with ssh, if you dophp /home/ec2-user/read_csv.php, you can execute the minimum code regardless of the framework and Apache settings, so start from there Debugging is easy.