/user-upload

PHP code to that reads .csv files and works with Postgresql database tables.

Primary LanguagePHP

What is user-upload.php and what does it do?

user-upload.php is a PHP program that can parse a csv file and insert data the from the csv file into a Postgresql database table called "users".

As it reads the file it will display the fields from the csv file and give and error message if a field is invalid. After the database table is populated, it will read from the database table, output the contents then drop the table.

The csv files

The csv file has a header followed by the records. The fields are in the following order: name, surname then email.

Requirements

The program will only insert a record if the fields fufil the following requirements:

  • name only uses letters a to z
  • surname only uses letters a to z
  • email is an email address in a valid format
  • email is a unique email address

Assumptions

I assume the program will work on Ubuntu 16.04 and any other Linux distros derived from it. I don't expect it to work on Windows or Mac.

How to use

"The following directives can used with user-upload.php

--file [csv file name] – this is the name of the CSV to be parsed --create_table – this will cause the PostgreSQL users table to be built (and no further action will be taken) --dry_run – this will be used with the --file directive in the instance that we want to run the script but not insert into the DB. All other functions will be executed, but the database won't be altered. -u – PostgreSQL username -p – PostgreSQL password -h – PostgreSQL host --help – which will output the above list of directives with details.

for example, typing in:

  php user-upload.php --file user2.csv

will parse user2.csv, create and populate a table, use the default username, password & host, and drop the table.

Why I wrote user-upload.php and who is it for

I wrote this code as part of a coding challenge when I applied for a job at Catalyst IT in Melbourne Australia around early October 2018.

Resources I used to do this PHP coding challenge: