PHP convert CSV files into arrays and intersect - php

I have six CSV files that I want to convert into individual arrays and then intersect those arrays to see all of the values that are similar in each. I have this so far:
$directory = "/csv/CSS_AUDIT/";
if (! is_dir($directory)) {
exit('Invalid directory path');
}
$files = array();
foreach (scandir($directory) as $file) {
if ('.' === $file) continue;
if ('..' === $file) continue;
$files[] = $file;
$row = 1;
if (($handle = fopen("/csv/CSS_AUDIT/$file", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($r=0; $r < $num; $r++) {
echo $data[$r];
if (!empty($data[$r]))
echo "\n";
}
}
fclose($handle);
}
}
Right now it is looping through the files and giving me everything in one large array and I'm just kinda stuck. Any advice on how to achieve What I am trying to do?

If the files aren't too big, you can load them into an array and either check that such value doesn't exist in the array before adding to it, or running array_unique() when you're done. See this for a reference.
If the files are too big, you'll have to use some other more complicated and time-consuming methods (such as reading the (hopefully sorted) output each time before inserting new line).

Related

Skip empty rows in reading csv excel file in php

I have a code where a csv file is uploaded ,the data is extracted from it and uploaded to database.Everything works fine,but how can i skip the empty rows and continue reading the rows with data in it.
This is the code where i extract data from csv file
if (($source = fopen( $csv_file, "r")) !== FALSE)
{
//read data from excel
while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
{
$question=$data[0];
$point=$data[1];
$header=$data[2];
$footer=$data[3];
$type_value=$data[4];
$group_name=$data[5];
echo $question;
}// while end
}
If you use PHP's SplFileObject instead of the basic fgetcsv() function, it has built-in options to skip empty lines:
$file = new SplFileObject($csv_file);
$file->setFlags(SplFileObject::READ_CSV SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
foreach ($file as $data) {
...
}
if ($data[0] == NULL)
continue;
This is because fgetcsv returns a non-empty array with a null element inside.
Try it with
if ($data === null) continue;
I didn't test it, but I sthink it should work.
Try this
if (($source = fopen( $csv_file, "r")) !== FALSE)
{
// read data from excel
while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
{
if ((string) $data[0] != '0' and empty($data[0]))
{
continue;
}
$question = $data[0];
$point = $data[1];
$header = $data[2];
$footer = $data[3];
$type_value = $data[4];
$group_name = $data[5];
echo $question;
}
}

Download Image from URL/Link in the CSV in PHP

I have a CSV list which contains URL/Link of some image.
So I had tried this below code;
if (($handle = fopen($csv_file, "r")) !== FALSE) {
fgetcsv($handle);
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
col1 = $col[0];
for ($i = 0; $i < count($col1); $i++){
if ( !$col1[$i] == null){
echo $col1[$i]. ",<br>";
file_put_contents("images/img.jpg", $elements[$i] . "\n", FILE_APPEND);
}
}
}
fclose($handle);
}
the code is working, but it's replacing the image at the end of execution I'm getting only one pic which's Last row of csv & Getting error Max_execution time.
Try this out.
$handle = fopen($csv_file, "r");
$destination = 'images/';
if ($handle) {
while ($columns = fgetcsv($handle)) {
foreach ($columns as $imageUrl) {
if (!empty($imageUrl)) {
file_put_contents(
$destination . basename($imageUrl),
file_get_contents($imageUrl)
);
}
}
}
}
What it does is open the CVS, loops through it, row at a time, checks that the image URL isn't empty, downloads it and puts it into your directory. You of course need to make sure that PHP has the rights to write to that directory. The code also doesn't handle conflicts in the naming of images, so a further improvement would be to add some clash detection and append a counter before the suffix.

Parsing files using PHP which has delimiter \t

IS there a way to parse the file(does not have extension) using PHP. The columns in the files are separated with tab(/t). I need to get each cells in a row with its contents.
I tried following methods
1) Converted(copied and pasted the contents and saved as a csv file) the file to
CSV and tried to read each row,but all the row contents came as a single row content.
$row = 1;
if (($handle = fopen("mynew.csv", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$row++;
echo "******* <br />";
foreach ($row as $cell) {
echo $cell . "<br/>";
}
echo "******* <br />";
}
fclose($handle);
}
2) Tried to read the file by converting(copied and pasted as save as xls) to xls file. Here the entire column in each row as considered as one column. I used PHPExcel library here to read the contents.
Is there any work around to parse the contents of a file that has a no extension using PHP?
When reading file you never specify extension of that file. You can do this code to read extensionless file as CSV:
$row = 1;
if (($handle = fopen("testFile", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$row++;
foreach ($row as $cell) {
echo $cell . "<br/>";
}
}
fclose($handle);
}

how to pass the two array?

The test.php directory is the same as csv and text file. now i want to pass all the csv file name and all the text name to the following if condition. namely, replace one.csv with all the csv file name. replace one.txt with all the txt file name.
if (($handle = fopen("one.csv", "r")) !== FALSE && ($handle2 = fopen("one.txt", 'a')) !== FALSE) { ...}
the following is my code. after run the code,i found all the content in the text file are the same. it loops too many times. how to change the code?thank you.
$files = glob("./*.csv");
$files1 = glob("./*.txt");
foreach($files as $filepath){
foreach($files1 as $filepath1){
$row = 1;
if (($handle = fopen("one.csv", "r")) !== FALSE && ($handle2 = fopen("one.txt", 'a')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row > 1) {
$url = $data[8];
foreach($result[0] as $url){
fwrite($handle2, $url."\r\n");
}
}
$row++;
}
fclose($handle);
fclose($handle2);
}
}
}
The txt file is empty. now, i want to read the csv file and extract some content of it then put them into the text file. thank you
You don't need to glob .txt files, just glob csv, and replace the extension .csv by .txt
$csv = glob('./*.csv');
foreach($csv as $file) {
$txt = preg_replace('#\.csv$#', '.txt', $file);
if (($handle = fopen($file, "r")) !== FALSE && ($handle2 = fopen($txt, 'a')) !== FALSE) {
//your code...
}
}
The problem is that you've got a loop within a loop. So every time the first loop loops it's doing everything in the second loop AGAIN.
Try something like this:
$csv_files = glob("./*.csv");
$txt_files = glob("./*.txt");
$csvs = array();
foreach($csv_files as $fp){
$csvs[] = fopen($fp, "r");
}
$txts = array();
foreach($txt_files as $fp){
$txts[] = fopen($fp, "r");
}
var_dump($csvs); // all csv file info
var_dump($txts); // all txt file info
You can test to see !== false in the separate loops if you wish. Otherwise, if you just want to use them then you can loop through the separate arrays and get the info out of them that way.

How to count number of files in a directory using PHP?

How to count the number of files in a directory using PHP?
Please answer for the following things:
1. Recursive Search: The directory (which is being searched) might be having several other directories and files.
2. Non-Recursive Search: All the directories should be ignored which are inside the directory that is being searched. Only files to be considered.
I am having the following code, but looking for a better solution.
<?php
$files = array();
$dir = opendir('./items/2/l');
while(($file = readdir($dir)) !== false)
{
if($file !== '.' && $file !== '..' && !is_dir($file))
{
$files[] = $file;
}
}
closedir($dir);
//sort($files);
$nooffiles = count($files);
?>
Recursive:
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$count = 0;
while($it->next()) $count++;
Most of the mentioned ways for "Non-Recursive Search" work, though it can be shortened using PHP's glob filesystem function.
It basically finds pathnames matching a pattern and thus can be used as:
$count = 0;
foreach (glob('path\to\dir\*.*') as $file) {
$count++;
}
The asterisk before the dot denotes the filename, and the one after denotes the file extension. Thus, its use can further be extended to counting files with specific filenames, specific extensions or both.
non-recrusive:
$dir = opendir('dir/');
$i = 0;
while (false !== ($file = readdir($dir))){
if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
}
echo "There were $i files";
recrusive:
function crawl($dir){
$dir = opendir($dir);
$i = 0;
while (false !== ($file = readdir($dir)){
if (is_dir($file) and !in_array($file, array('.', '..'))){
$i += crawl($file);
}else{
$i++;
}
}
return $i;
}
$i = crawl('dir/');
echo "There were $i files";
Might be useful for you:
http://www.php.net/manual/en/class.dir.php
http://www.php.net/manual/en/function.is-file.php
But, i think, there is no other good solutions.
Rather than posting code for you, I would provide the outline of what you should do as you seem to have the basic code already.
Place your code in a function. Have two parameters ($path, $recursive = FALSE) and within your code, separate the is_dir() and if that's true and the recursive flag is true, then pass the new path (path to the current file) back to the function (self reference).
Hope this helps you learn, rather than copy paste :-)
Something like this might work:
(might need to add some checks for '/' for the $dir.$file concatenation)
$files = array();
$dir = './items/2/l';
countFiles($dir, $files); // Recursive
countFiles($dir, $files, false); // Not recursive;
var_dump(count($files));
function countFiles($directory, &$fileArray, $recursive = true){
$currDir = opendir($directory);
while(($file = readdir($dir)) !== false)
{
if(is_dir($file) && $recursive){
countFiles($directory.$fileArray, $saveArray);
}
else if($file !== '.' && $file !== '..' && !is_dir($file))
{
$fileArray[] = $file;
}
}
}
Recursive:
function count_files($path) {
// (Ensure that the path contains an ending slash)
$file_count = 0;
$dir_handle = opendir($path);
if (!$dir_handle) return -1;
while ($file = readdir($dir_handle)) {
if ($file == '.' || $file == '..') continue;
if (is_dir($path . $file)){
$file_count += count_files($path . $file . DIRECTORY_SEPARATOR);
}
else {
$file_count++; // increase file count
}
}
closedir($dir_handle);
return $file_count;
}
Non-Recursive:
$directory = ".. path/";
if (glob($directory . "*.") != false)
{
$filecount = count(glob($directory . "*."));
echo $filecount;
}
else
{
echo 0;
}
Courtesy of Russell Dias
You can use the SPL DirectoryIterator to do this in a non-recursive (or with a recursive iterator in a recursive) fashion:
iterator_count(new DirectoryIterator($directory));
It's good to note that this will not just count regular files, but also directories, dot files and symbolic links. For regular files only, you can use:
$directory = new DirectoryIterator($directory);
$count = 0;
foreach($directory as $file ){ $count += ($file->isFile()) ? 1 : 0;}
PHP 5.4.0 also offers:
iterator_count(new CallbackFilterIterator($directory, function($current) { return $current->isFile(); }));
$dir = opendir('dir/');
$i = 0;
while (false !== ($file = readdir($dir))){
if (!in_array($file, array('.', '..' ))and (!is_dir($file)))
$i++;
}
echo "There were $i files";

Categories