Daily import .csv files from sftp to a MySQL database [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to automate a simple and daily task: There're multiple .csv files on a SFTP-server that I want to import in a MySQL-database.
The filenames have a structure like 20160601-1.csv, 20160601-2.csv, 20160601-3.csv, etc. The number of files divers from 8 to 20 per day. The layout of the .csv is always the same (10 columns with some empty fields).
I know this isn't very detailed, but i'm searching for some hours now and I don't know where to begin.
I think php is a good way to do this? Or maybe Python?

You can do this in PHP using fgetcsv() function,
Sample code to read csv file
//read file
$fp = fopen($filename,'r') or die("can't open file");
$csv_line=fgetcsv($fp);
$last=0;
$insert_count=0;
while($csv_line = fgetcsv($fp,1024))
{
//$csv_line is an indexed array for values on each line
//database update code goes here
}

Related

Merge CSV files on GCS using PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have multiple small CSV files in a GCS bucket. I'm trying to merge those into a single large CSV file.
I've seen it can be done using "gsutil compose" command, but I was wondering if that can be done using the PHP Google Cloud Storage API.
You can use the the Client Library that references this other link. And the code should be something like this
$sourceObjects = ['log1.txt', 'log2.txt'];
$singleObject = $bucket->compose($sourceObjects, 'combined-logs.txt');

Efficien memory and cpu usage with progressive read and write [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose i have fetch more data rows in laravel and i have to write in csv file. And suppose my server execution time is out before all data rows are written in csv file. Then how can i proceed in order to be able to write all the data row in csv file ?
Actually i wanted to fetch data rows, clean memory and read and write data in the csv file but have no clue in laravel.
Please guys help me with any reference or ideas.
Create a file and having the file location stored somewhere.
Limit the records fetched once using the query.
e.g.
get the count of your required records and execute a loop according to that say 100 records at a time and write/append those records in your CSV file.
Alternative Way (Simplest Way)
use Maatwebsite/Laravel-Excel library to create CSV, excel etc.

How to send info from two session variable to a text file if a number has been guessed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm stuck and I would be happy to get some guidance.
I need to send information from two session variables in the same session to a text file.
Number of guesses: $_SESSION['antal_gaet']
Username: $_SESSION['username']
How do I write to a textfile so that I can make a highscore list from the array with (number of guesses, username).
Thank you for any help you can provide.
You can create/open a new file then write in it the concatenation of your variables:
$scores_file = 'scores.txt';
// open the file
$handle = fopen($scores_file, 'w') or die('Cannot open file: '.$scores_file);
// create 1rst line and a line feed \n
$data = 'Number of guesses:'.$_SESSION['antal_gaet']."\n";
// concat the 2nd line
$data .= 'Username:'.$_SESSION['username'];
// write to the opened file
fwrite($handle, $data);

How to make specific columns of a csv file read only with php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wrote a script to export some columns(id and name) of a mysql table so that scores of students can be inserted. But I want to make the id and name columns read only. I searched but could not find a solution. Please, how can I do this?
You cannot do this.
A file is just a stream of bytes stored (in HDD/memory). You read these bytes and interpret it as you want. In case of csv file you read one line and the split this based on separator.
The concept of read only comes from OS. Where you mark a file as read only to ensure users without proper permission do not edit the file.
While writing a program you open a file in read only mode using r option this is just a way of telling you do not intend to write anything to file and fwrite should not be allowed (throw exception) on this stream.
What you are asking is to make certain bytes of the files to be read only while others should be editable. OS or file stream have no way differentiating between the two.

How to write a Microsoft Excel file using PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
There are 8 columns that I want to save in the excel file. How do I use PHP to create, access and edit Microsoft Excel files?
I think this is what you want.
https://github.com/PHPOffice/PHPExcel#phpexcel---openxml---read-write-and-create-spreadsheet-documents-in-php---spreadsheet-engine
PHPExcel is a library that can handle Excel files, and some other file formats. If you only have this 8 columns you than just set the data with
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Content');
...
$objPHPExcel->getActiveSheet()->SetCellValue('H1', 'Content');
And have a look at the hello word example, this will guide you how to create a simple xlsx file.
Check out https://phpexcel.codeplex.com/
It provides some very cool features to manipulate Excel files. You can find a couple of examples here: https://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home
PHPExcel is what you want, you can download from
https://phpexcel.codeplex.com/
Be careful when managing huge spreadsheets, because PHPExcel consumes lots of memory.
If you only need output a simple excel file with non formatted data, you can output the data in comma separated values (CSV)

Categories