How to read Multiple CSV file data in php - php

I am trying to import 6 files which are in zip files. First I extract those files after that I want to get all the data in these files. But currently I am getting only the first file data. The script had not read the second file. I don't understand how to get rid from this problem.
Here is my code.
<?php
if ($_FILES) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array(
'application/zip',
'application/x-zip-compressed',
'multipart/x-zip',
'application/x-compressed'
);
foreach ($accepted_types as $mime_type) {
if ($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if (!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = "zip/" . $filename;
if (move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
$col = array();
if ($x === true) {
for ($x = 0; $x < $zip->numFiles; $x++) {
$csv = $zip->getNameIndex($x);
$zip->extractTo("zip/");
$csv_path = "zip/" . $csv;
if (($handle = fopen($csv_path, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
echo "<pre>";
print_r($col);
}
fclose($handle);
}
}
$zip->close();
unlink($target_path);
exit;
}
$message = "Your .zip file was uploaded and unpacked.";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
?>
Any help would be Highly appreciated.

Look at this part of your code...
<?php
// ...code...
$zip->extractTo("zip/");
$csv_path = "zip/" . $csv;
if (($handle = fopen($csv_path, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// ...code...
?>
extractTo() extracts the files. There are six files, you said. Then you do fopen(), and you do it once. You want to do that fopen() on each of the files.
What you'll want is...
<?php
// ...code... (files are extracted at this point)
$files = files('zip/');
for($i = 0; i < count($files); $i++) {
$file = $files[$i];
// ...do csv stuff here for $file
}
// ...code...
?>

Related

How to concatenate two columns in a csv file in php?

I have a csv file like this:
I would like to concatenate the values of the style_color columns in this csv file. To have for example SCJEG4_1014.
I wrote a script, it creates this last column with the header 'Pictures Names' but in each cell I just have "_".
How can I solve my problem?
<?php
//uploaded xlsx file recovery
$xlsx="C:/wamp64/www/Extract_pictures_Excel/xlsx_files/".date('Y_m_d H-i-s')."_file.xlsx";
move_uploaded_file($_FILES["mon_fichier"]["tmp_name"],$xlsx);
// Excel in CSV
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load($xlsx);
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$nomcsv = "C:/wamp64/www/Extract_pictures_Excel/csv/".date('Ymd_His').".csv";
$writer->save($nomcsv);
$delimiter = ";";
$csv_data = array();
$row = 1;
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$names_pictures = $data[7].'_'.$data[4];
$csv_data[] = $data;
$row++;
}
fclose($handle);
}
$extra_columns = array('Pictures Names' => $names_pictures);
foreach ($csv_data as $i => $data) {
if ($i == 0) {
$csv_data[$i] = array_merge($data, array_keys($extra_columns));
} else {
$csv_data[$i] = $data = array_merge($data, $extra_columns);
}
}
if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
foreach ($csv_data as $data) {
fputcsv($handle, $data, $delimiter);
}
fclose($handle);
}
?>
It looks like you only add in the details from the last row ( as you only use the value of $names_pictures once). It would be better (IMHO) to add this value into the data at the point at which you generate the $csv_data array...
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$data['Pictures Names'] = $data[7] . '_' . $data[4];
$csv_data[] = $data;
$row++;
}
You could then remove the foreach ($csv_data as $i => $data) { loop
If you had a different file for the output you could open the output file before the above loop and write the data directly to the output file rather than using $csv_data...
if (($handle = fopen($nomcsv, 'r')) !== FALSE
&& ($ohandle = fopen($nomcsvOutput, 'w')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$data['Pictures Names'] = $data[7] . '_' . $data[4];
fputcsv($ohandle, $data, $delimiter);
}
fclose($handle);
fclose($ohandle);
}

Need to run twice in-order for it to fully work

$find = '.5010.';
$directory_with_files = './'.date('m-d-Y');
$dh = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
if(in_array($filename, array('.', '..')) || is_dir($filename))
continue;
$files[] = $filename;}
foreach($files as $file){
//find only 5010 files
if(stripos($file, $find) !== false){
// open the 5010 file
$handle = fopen(date('m-d-Y').'/'.$file, "r");
$file_content = file_get_contents(date('m-d-Y').'/'.$file);
$handle2 = fopen(date('m-d-Y').'/'.$file, "r");
$file_content2 = file_get_contents(date('m-d-Y').'/'.$file);
if ($handle) {
$header = '';
$name = '';
$footer = '';
$payor_blocks = array();
// determine if file has more than one payor
$payor_count = substr_count($file_content, 'N1*PR*');
//if the file has more than one payor
if($payor_count > 1) {
//read the file line by line
$header_end = false;
$block_start = false;
$count = 1;
if($handle2){
$line_number = 0;
$line_stop= array();
while (($line1 = fgets($handle2)) !== false) {
$line_number++;
if(strpos($line1, 'CAS') !==false){
$line_stop[] = $line_number;}}
$footer_line = count($line_stop)-2;
$footer_line = $line_stop[$footer_line];
$line_number = 0; }
//look for occurances of CAS and what line each on is on
while (($line = fgets($handle)) !== false) {
$line_number++;
//look for the first payor block
if(strpos($line, 'N1*PR*') !== false || $block_start) {
$header_end = true; $block_start = true;
if(strpos($line, 'N1*PR*') !== false) {
$count++;
}
//see if the block finished
if($line_number == $footer_line) {
$block_start = false;
$payor_blocks[$count] .= $line;
$count++; }
$payor_blocks[$count] .= $line;}
else {
if($header_end) {
$footer .= $line."\n"; }
else {
$header .= $line."\n";}}
$refid = 'REF*2U*';
if(stripos($line, $refid) !== false)
{
$refnumber = str_replace(array($refid, '~'), array('', ''), $line);
$refnumber = trim($refnumber);
if($refnumber != '')
{
$refnumber = '_'.$refnumber.'_';
$filerenamed = str_replace($find, $refnumber,$file);
copy('./'.date('m-d-Y').'/'.$file, './'.date('m-d-Y').'/'. $filerenamed);
}
echo $refnumber . "\n";
}
}
//get payor blocks and create a file foreach payor
$new_files = array();
foreach($payor_blocks as $block) {
$filename = date('m-d-Y').'/'.$file . "_" . $count;
$count++;
$new_files[] = array(
'name' => $filename,
'content' => $header."\n".$block."\n".$footer
);
}
foreach($new_files as $new_file) {
$myfile = fopen($new_file['name'], "w");
fwrite($myfile, $new_file['content']);
fclose($myfile);
}
}
else{
while (($line = fgets($handle)) !== false)
{
$refid = 'REF*2U*';
if(stripos($line, $refid) !== false)
{
$refnumber = str_replace(array($refid, '~'), array('', ''), $line);
$refnumber = trim($refnumber);
if($refnumber != '')
{
$refnumber = '_'.$refnumber.'_';
$filerenamed = str_replace($find, $refnumber,$file);
copy('./'.date('m-d-Y').'/'.$file, './'.date('m-d-Y').'/'. $filerenamed);
}
echo $refnumber . "\n";
}
}
}
}
}
// DONE - close the file
fclose($handle);
}
foreach($files as $fiftyfile){
if(stripos($fiftyfile, $find) !== false){
$handle3 = fopen(date('m-d-Y').'/'.$fiftyfile, "r");
$file_content3 = file_get_contents(date('m-d-Y').'/'.$fiftyfile);
if ($handle3) {
if(unlink('./'.date('m-d-Y').'/'.$fiftyfile))
{
echo "file named $fiftyfile has been deleted successfully";
}
else
{
echo "file is not deleted";
}
}
}
}
I have a few files in my directory with filenames that contain "3256.5010.548674.23a" In this code it opens the file and searches if there is more than one "N1*PR*" and if there is to split them into separate files. Lastly to change ".5010." to the REF number which is something like "8743" . Then it deletes all the files with ".5010." And combines the rest in one document. It works fine however, when I first run it it splits and renames, but only deletes the first files not all the ".5010." (not the ones that were split), which then when I run it again after that, it deletes everything but renames the old ones, since it goes through the "else statement" that also does the renaming. How could I solve the issue with the delete?

Show CSV rows in reverse order with PHP

Trying to get this code to echo the rows it gets from the CSV file in reverse order. Any idea how to do this?
<?php
$row = 1;
$FILE = "file.csv";
if (($handle = fopen($FILE, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($c == 0) $first = $data[$c];
if ($c == 1) $second = $data[$c];
}
}
fclose($handle);
}
?>
Create a specific function for this task and call the function. It is always easier to split your program into smaller pieces. You need to read the data into an array and the reverse the order.
<?php
function loadCSV($file) {
$rows = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
array_push($rows, $data);
}
fclose($handle);
}
return array_reverse($rows);
}
?>
Here is how to use this function:
<?php
$data = loadCSV('data.csv');
print_r($data);
?>

How to skip the first line of CSV in PHP

I have a CSV upload that I am struggling to get to skip the first line of the CSV document. I am uploading a single CSV document and the first line contains a cell that contains one bit of text which is throwing out the array. I am not sure which count to edit?
$fields_firstrow = true;
$i = 0;
$a = 0;
$fields = array();
$content = array();
$allowedExts = array("csv");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < 2000000)&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists($_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
}
}
}
else
{
echo "Invalid file";
}
$file = $_FILES["file"]["name"];
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if($fields_firstrow == true && $i<1) {
foreach($data as $d) {
$fields[] = strtolower(str_replace(" ", "_", $d));
}
$i++;
continue;
}
$c = 0;
foreach($data as $d) {
if($fields_firstrow == true) {
$content[$a][$fields[$c]] = $d;
} else {
$content[$a][$c] = $d;
}
$c++;
}
$a++;
}
} else {
echo "Could not open file";
die();
}
Any help would be greatly appreciated.
Just add an extra line of code before the line from where the while loop starts as shown below :
....
.....
fgetcsv($handle);//Adding this line will skip the reading of th first line from the csv file and the reading process will begin from the second line onwards
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
.......
.......
It is just as simple........ !!!
$i=0;
if($fields_firstrow == true) {
foreach($data as $d) {
if ($i == 0){continue;}
$i++;
$fields[] = strtolower(str_replace(" ", "_", $d));
}
}
You are not changing the value for variable $fields_firstrow. For all loop iteration it will still be true.
In my opinion and per my understand of your code, you should change it to false before the first continue.
...
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if($fields_firstrow == true && $i<1) {
foreach($data as $d) {
$fields[] = strtolower(str_replace(" ", "_", $d));
}
$i++;
$fields_firstrow = false;
continue;
}
$c = 0;
foreach($data as $d) {
if($fields_firstrow == true) {
$content[$a][$fields[$c]] = $d;
} else {
...
Maybe you do not need the $i variable after that.
Here is an example from http://php.net/fgets modified a bit:
<?php
$handle = #fopen("/tmp/inputfile.txt", "r");
$firstLine = true;
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
if(firstLine) {
$firstLine = false;
continue;
}
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
I assume you see the point, and can modify your script accordingly.

PHP not reading the entire CSV file

I've written a simple bit of code to test two parameters and forward the user to a link.
error_reporting(E_ALL|E_STRICT);
ini_set("display_errors", "On");
$secure_box=$_GET['query'];
$fh = fopen('db.csv', 'r');
$now = date("d.m.Y");
$data=fgetcsv($fh);
$first_name=$data[0];
$date=$data[1];
$url=$data[2];
{
if($secure_box == $first_name AND $date>=$now)
{
header("Location: $url");
exit();
}
else
{
header("Location: http://localhost/x/delivery_method.html");
}
exit;
?>
My problem here is that the entire CSV file is not being read. What can I do ?
The entire file isn't being read because you only called fgetcsv once. fgetcsv only reads one line at a time, you need to put the that code in a loop.
Here is an example of how to use fgetcsv in a loop, from the php docs at http://www.php.net/fgetcsv:
<?php
// Example #1 Read and print the entire contents of a CSV file
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
This is what I've been able to come up with..Hope it's right
<?php
error_reporting(E_ALL|E_STRICT);
ini_set("display_errors", "On");
$name_value = $_GET['query'];
$fh = fopen('db.csv', 'r');
$now = date("d.m.Y");
$line = 1;
if (($handle = fopen("db.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
$line++;
for ($c = 0; $c < $num; $c++)
{
if ($name_value == $data[0] AND $data[1] >= $now)
{
header("Location: $data[2]");
exit();
}
else
{
header("Location: http://localhost/x/client_unauthorized.html");
}
}
}
fclose($handle);
}
?>
You can loop to read the whole file line by line until EOF, and check each row in turn.

Categories