Join part of line in csv with php - php

I have one csv file like this:
"Name","surname","note"
"Jhon","Jhon","fdskngkfngkfnkglnlksanjlbgsjangkjlvnkajsdf"
"Jhon2","Jhon2","fdssfsfsfkngkfngkfnkglnlksanjlbgsjangkjlvnkajsdf"
"Jhon3","Jhon3","fdssfsfsfkngkfngkfnk
glnlksanjlbgsjangkjlvnkajsdf"
"Jhon4","Jhon4","fdssfsfsf
kngkfngkfnkglnlks
anjlbgsjangkjlvnkajsdf"
I need to join the line who return, because i need to add this file in mySQL.
I use this code
$csv_file = "output.csv";
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode('","', $csv_data[$i]);
$insert_csv = array();
$insert_csv['Gestore'] = ltrim($csv_array[0], '"');
echo $insert_csv['Gestore'] . "</br>";
$query = "INSERT INTO prova(Gestore) VALUES('".$insert_csv['Gestore']."')";
$n=mysql_query($query, $connect );
$i++;
}
It works but i need to solve precedent problem...

You can simply use the PHP function fgetcsv.
As an alternative to PHP you could use
LOAD DATA INFILE
see MySQL manual, LOAD DATA INFILE, if you've got the FILE privilege.

Related

Automating CSV conversion from iso-8859-2 to utf-8

I have quite a few CSV files that are unfortunately encoded with iso-8859-2 (according to Brackets). I would like to iterate over these files with PHP and convert them.
I found https://csv.thephpleague.com/9.0/converter/charset/ but the way I can use the conversion function is uncertain to me.
Their example code
use League\Csv\CharsetConverter;
$csv = new SplFileObject('/path/to/french.csv', 'r');
$csv->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY);
$encoder = (new CharsetConverter())->inputEncoding('iso-8859-15');
$records = $encoder->convert($csv);
This is my code so far that is part of a form to upload one file and save the contents to the database for testing. It of course saves the text in the incorrect format.
$db = ConnectDB::getConnection('address_dtb');
$sql = " ... ";
$stmt = $db->prepare($sql);
$rowCount = 0;
$temp_name = $_FILES['adresscsv']['tmp_name'];
$file_handle = fopen($temp_name, 'r');
while (($items = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
if($flag) { $flag = false; continue; }
$stmt->execute($items);
$rowCount++;
}
fclose($file_handle);
ConnectDB::closeConnection($db);
What is the correct way to use the PHP CSV library above to iterate over locally saved files in a for loop to automate the process?
I ended up using iconv as hinted.
$files = glob('address/*.csv');
foreach ($files as $csv) {
$file_data = file_get_contents($csv);
$utf8_file_data = iconv('Windows-1250', 'UTF-8', $file_data);
file_put_contents($csv, $utf8_file_data);
}
You do not have to use a library. There is a function in PHP that can do that iconv

how can I sort a text file in php

english, lang1, lang2
rat, rat_lang1, rat_lang2
ball, ball_lang1, ball_lang2
air, air_lang1, air_lang2
If I have this text file I read in php, how can I sort it starting the second line, the first line being the heading of the file. So that the file will print out like..
english....
air....
ball...
rat....
I read the file using fopen, put it in $content using fread, used explode with new line. I can see the array of lines but cannot figure out how to sort it. Any suggestions would be appreciated.
Much of this solution was answered within the comments in response to your question. All put together you're looking for something like:
<?php
$f = file("text.txt"); //read the text file into an array
$newf = fopen("newtext.txt", "w+"); //open another file
$header = $f[0]; //store the first element of the array as $header
echo $header."<br>"; //and echo the header
fwrite($newf, $header); //write the header to the new file
array_shift($f); //then remove the first element of the array i.e. the header
sort($f); //sort the array (no flag param = alphabetical sort)
foreach($f as $line){ //loop through the sorted array
echo $line."<br>"; //print each element as it's own line
fwrite($newf, trim($line)."\n"); //write other elements to new file on own line
}
fclose($newf); //close the file
?>
Try this:
$data = trim(file_get_contents('sort_test.txt'));
$data = explode("\n", $data);
$array_order = array();
$fileLocation = getenv("DOCUMENT_ROOT") . "/myfile.txt";
$file = fopen($fileLocation, "w");
for ($i = 0; $i < count($data); $i++) {
($i > 0) ? $array_order[$i] = $data[$i] : fwrite($file, $data[0]);
}
sort($array_order);
$data = implode("\n", $array_order);
fwrite($file, $data);
fclose($file);
echo 'file created - location::' . $fileLocation;
Output myfile.txt
english, lang1, lang2
air, air_lang1, air_lang2
ball, ball_lang1, ball_lang2
rat, rat_lang1, rat_lang2
Maybe the new file (myfile.txt) will needs write permission to the directory you are writing to , in my case the file has been stored into C:/.../htdocs/myfile.txt

fclose() expects parameter 1 to be resource

I've a problem with a PHP script that creates some csv file.
The PHP script is the following:
<?php
$inputFile = "/var/www/vhosts/pecso.it/httpdocs/test/export30gg.txt";
$csvData = file_get_contents($inputFile);
$rows = explode(PHP_EOL, $csvData);
$rowsArray = array();
foreach ($rows as $row) {
$rowsArray[] = str_getcsv($row);
}
$csvFileName = "/var/www/vhosts/pecso.it/httpdocs/graphs/export30gg.csv";
if (file_exists($csvFileName)){
unlink($csvFileName);
}
$csvFile = fopen($csvFileName, "w");
$csvFileForGraph = fopen($csvFileNameForGraph, "w");
for ($i = 0; $i < count($rowsArray); $i++) {
$dateTime = DateTime::createFromFormat('d/m/Y', $rowsArray[$i][0]);
$d = $dateTime->format('Y-m-d');
$rowsArray[$i][0] = $d;
$rowForGraph = $rowsArray[$i];
unset($rowForGraph[1]);
$row = implode(',',$rowsArray[$i]);
$rowForGraph = implode(',',$rowForGraph);
file_put_contents($csvFileName, $row.PHP_EOL , FILE_APPEND);
}
fclose($csvFileName);
?>
This script works correctly and csv file export30gg.csv is correctly created but, every time I run this script, I've the following error:
fclose() expects parameter 1 to be resource
Can you help me, please?
flose() accepts parameter as file pointer resource which will be found while code execution for example in your case it is $csvFile = fopen($csvFileName, "w");
So, it should be
fclose($csvFile);
and not the fclose($csvFileName);
It should be like
fclose($csvFile);
because you've stored recourse link in $csvFile, not in $csvFileName

Avoiding \r\n\ on mysql import

I have the following code to import a CSV file in to a MYSQL database but am having a few issues on import.
$csv_file = CSV_PATH . "resultsimport.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['ID'] = $csv_array[0];
$insert_csv['SerialNo'] = $csv_array[1];
$insert_csv['ChannelNo'] = $csv_array[2];
$insert_csv['IL850'] = $csv_array[3];
$insert_csv['IL1300'] = $csv_array[4];
$insert_csv['IL1310'] = $csv_array[5];
$insert_csv['IL1550'] = $csv_array[6];
$insert_csv['RL850'] = $csv_array[7];
$insert_csv['RL1300'] = $csv_array[8];
$insert_csv['RL1310'] = $csv_array[9];
$insert_csv['RL1550'] = $csv_array[10];
$query = "INSERT INTO results(ID,SerialNo,ChannelNo,IL850,IL1300,IL1310,IL1550,RL850,RL1300,RL1310,RL1550)
VALUES('".$insert_csv['ID']."','".$insert_csv['SerialNo']."','".$insert_csv['ChannelNo']."','".$insert_csv['IL850']."','".$insert_csv['IL1300']."','".$insert_csv['IL1310']."','".$insert_csv['IL1550']."','".$insert_csv['RL850']."','".$insert_csv['RL1300']."','".$insert_csv['RL1310']."','".$insert_csv['RL1550']."')";
$n=mysql_query($query, $connect );
$i++;
}
fclose($csvfile);
echo "File data successfully imported to database";
mysql_close($connect);
Here is an example of the first two lines from the CSV file:
ID,SerialNo,ChannelNo,IL850,IL1300,IL1310,IL1550,RL850,RL1300,RL1310,RL1550
1405230001-A-MTP-1-01,1405230001,A-MTP-1-1,0.320,0.150,,,,,,
As you can see there is only data in two of the columns, but on some imports there will be data to import in the final column.
The problem happens when there is no data in that final column, my import process adds in \r\n\ to the database.
How can I avoid that?
I also end up with a blank line on import at the end.
PHP 5 has a built in function to parse CSV for you, fgetcsv. It is better than relying on explode (which is naive about text enclosed with quotes that could contain commas. An empty line will result in an empty $data array, so test for that and skip them
while (($data = fgetcsv($csvfile, 1000, ",")) !== FALSE) {
if (empty($data) continue;
// rest of data manipulation continues
}
Sidenote: you should not be using the mysql_* family of functions. That extension has long been deprecated. You should be using mysqli or PDO to connect to your database and using prepared queries to guard against SQL injection attacks.

php import csv into sql database

whats wrong with this, when i echo out a row from the csv file and concat anything to the end of the row, it doesnt show up, instead all the rows are echo'ed and the concated string only shows up once at the very end, is this some kind of buffering thing that wont let me concat strings with stuff from my csv file, its running on my local wamp server, and i have tryed different line delimiter in my expload function, im sure the file only uses \n at the end of a line
im trying to parse a csv file row by row so i can check the content of it before i use it to construct an sql statement and insert it into my database.
$file = fopen($filename, "r")
$filesize = filesize($filename);
$filecontent = fread($file, $filesize);
fclose($file);
$rows = explode("\n", trim($filecontent));
foreach ($rows as $row)
{
echo $row . '<br />';
}
You are splitting the string by the string \n. Unless the actual string "\n" appears anywhere in the file, this will probably do nothing. You probably meant "\n" (double quotes), which makes this an actual line break.
Your overall process is terribly inefficient though. You should use fgetcsv and process the file line by line, instead of reading it into memory all at once.
$handle = fopen('test.csv', 'r');
while (($row = fgetcsv($handle)) !== false) {
foreach ($row as $field) {
echo $field . '<br />';
}
}
fclose($handle);
Use fgetcsv() function to convert a CSV file to an array:
$csvFile = "test.csv";
$csvSeparator = ",";
$csvFileLength = filesize($csvFile);
$handle = fopen($csvFile, "r");
$csvData = fgetcsv($handle, $csvFileLength, $csvSeparator);
fclose($handle);
Dump the data to show the structure:
var_dump($csvData);
Now you can convert the data to use in database.

Categories