How to read csv from S3 bucket and insert into database

| By Webner

While working on a project, we wanted to read csv from s3 bucket, store this data in another local file and insert it into database. We had S3 bucket url where csv was kept.

Samp S3 URL -‘’;

Below is the sample code which reads the csv from S3 bucket and inserts the data into local file.

$file=file_put_contents(‘localFile.csv’, file_get_contents($url));

Csv file was tab separated so need to separate with /t

Below is the sample code:

$csv_file = "localFile.csv";
//put the code data in a local CSV from AWS S3 url
$file=file_put_contents('localFile.csv', file_get_contents($url));
//now read local csv record by record and update database
if (($handle = fopen($csv_file, "r")) !== FALSE) 
	while (($data = fgetcsv($handle,10000,"\t")) !== FALSE) 
            //code here to save data in database

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at

Leave a Reply

Your email address will not be published. Required fields are marked *