All i want is to open a rsyslog file with fopen() take the first 3 lines set a variable with the last of this 3 lines. Then take the other 3 lines e.t.c.
$path_file = variable_get('$path');
$file = fopen($path_file, 'r');
for($i=0;$i<3;$i++) {
$line = fgets($file);
$line = variable_set($line);
}
fclose($file);
Use file() instead (reads everything in as an array)
Try this:
$file = file('$path');
for($x = 0; $x < count($file); $x = $x + 3)
{
if(isset($file[$x]) && isset($file[$x +1]) && isset($file[$x + 2])
{
//do something with the values.
}
}
function readFileStartingAtLineNumber($x)
{
$file = file('$path');
if(isset($file[$x]) && isset($file[$x +1]) && isset($file[$x + 2])
{
//do something with the values.
}
}
function getLog($path, $numberOfLines, $lastIndex) {
$file = fopen($path, 'r');
if (!$file) {
print 'error opening file';
}
else {
$data = '';
$i = -1;
while(($line = fgets($file)) !== FALSE) {
if(++$i < $lastIndex) continue;
if($numberOfLines-- == 0) break;
$data .= $line;
}
fclose($file);
if ($data === '') {
print 'EOF reached without getting data';
}
}
return $i;
}
Related
$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?
I want to keep just an exact number of line from this, it takes a file reverse it and echo it, but the file might get to long and want to limit it to a number of lines.
$file = file("update.log");
$file = array_reverse($file);
foreach($file as $f){
if (stripos($f, "Sale") !== false) {
$class = "sales";
} else{
$class = "row";
}
echo "<div class='".$class." scale'>".$f."</div>";
}
Use a counter and when it hits the max_lines number break out of the foreach loop.
$file = file("update.log");
$file = array_reverse($file);
$count = 0;
$max_lines = 100;
foreach($file as $f){
if ($count >= $max_lines){
break;
}
if (stripos($f, "Sale") !== false) {
$class = "sales";
} else{
$class = "row";
}
$count++;
echo "<div class='".$class." scale'>".$f."</div>";
}
$file = file("update.log");
$file = array_reverse($file);
$lineLimit = 25;
$lineCounter = 0;
foreach($file as $f){
if (stripos($f, "Sale") !== false) {
$class = "sales";
} else{
$class = "row";
}
echo "<div class='".$class." scale'>".$f."</div>";
$lineCounter++;
if($lineCounter > $lineLimit)
break; //Will exit the loop
}
I think this is faster and more flexible in avoiding memory problems. You don't have to read every line of the file so you could use something like this:
function viewLastLines($logfile,$maxlines=25) {
$handle = fopen($logfile, "r");
fseek($handle, -($maxlines),SEEK_END);
while (($line = fgets($handle)) !== false)
$outputlines[]=$line;
fclose($handle);
return $outputlines;
}
Use it like this in your case:
$file = array_reverse(viewLastLines("update.log",100));
foreach($file as $f){
if (stripos($f, "Sale") !== false) $class = "sales";
else $class = "row";
echo "<div class='".$class." scale'>".$f."</div>";
}
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.
I am using phpdocx to create a word document. It is a valid word document when I look at it on the server. However, when I try to download it, it says that the file cannot be opened because there are problems with the file. Word is able to recover the document, but it clients will complain about an invalid word document. Here is the code I use to download the file:
function readfile_chunked_remote($filename, $seek = 0, $retbytes = true, $timeout = 3) {
set_time_limit(0);
$defaultchunksize = 1024*1024;
$chunksize = $defaultchunksize;
$buffer = '';
$cnt = 0;
$remotereadfile = false;
if (preg_match('/[a-zA-Z]+:\/\//', $filename))
$remotereadfile = true;
$handle = #fopen($filename, 'rb');
if ($handle === false) {
return false;
}
stream_set_timeout($handle, $timeout);
if ($seek != 0 && !$remotereadfile)
fseek($handle, $seek);
while (!feof($handle)) {
if ($remotereadfile && $seek != 0 && $cnt+$chunksize > $seek)
$chunksize = $seek-$cnt;
else
$chunksize = $defaultchunksize;
$buffer = #fread($handle, $chunksize);
if ($retbytes || ($remotereadfile && $seek != 0)) {
$cnt += strlen($buffer);
}
if (!$remotereadfile || ($remotereadfile && $cnt > $seek))
echo $buffer;
ob_flush();
flush();
}
$info = stream_get_meta_data($handle);
$status = fclose($handle);
if ($info['timed_out'])
return false;
if ($retbytes && $status) {
return $cnt;
}
return $status;
}
Thanks!
<?php
function scan_dir($dirname) {
$file_count = 0 ;
$dir_count = 0 ;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
++$file_count;
if(is_dir($dirname."/".$file)) {
++ $dir_count;
scan_dir($dirname."/".$file);
}
}
}
closedir($dir);
echo "There are $dir_count catalogues and $file_count files.<br>";
}
$dirname = "/home/user/path";
scan_dir($dirname);
?>
Hello,
I have a recursive function for count files and catalogues. It returns result for each catalogue.
But I need a common result. How to change the script?
It returns :
There are 0 catalogues and 3 files.
There are 0 catalogues and 1 files.
There are 2 catalogues and 14 files.
I want:
There are 2 catalogues and 18 files.
You could tidy up the code a lot with RecursiveDirectoryIterator.
$dirs = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(dirname(__FILE__))
, TRUE);
$dirsCount = $filesCount = 0;
while ($dirs->valid()) {
if ($dirs->isDot()) {
$dirs->next();
} else if ($dirs->isDir()) {
$dirsCount++;
} else if ($dirs->isFile()) {
$filesCount++;
}
$dirs->next();
}
var_dump($dirsCount, $filesCount);
You can return values from each recursive call, and sum those and return back to its caller.
<?php
function scan_dir($dirname) {
$file_count = 0 ;
$dir_count = 0 ;
$dir = opendir($dirname);
$sub_count = 0;
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
++$file_count;
if(is_dir($dirname."/".$file)) {
++ $dir_count;
$sub_count += scan_dir($dirname."/".$file);
}
}
}
closedir($dir);
echo "There are $dir_count catalogues and $file_count files.<br>";
return $sub_count + $dir_count + $file_count;
}
$dirname = "/home/user/path";
echo "Total count is ". scan_dir($dirname);
?>
The code will give you the net count of every item.
With a simple modification. Just, for example, keep the counts in an array that you can return from the function to add up to the previous counts, like so:
<?php
function scan_dir($dirname) {
$count['file'] = 0;
$count['dir'] = 0;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
$count['file']++;
if(is_dir($dirname."/".$file)) {
$count['dir']++;
$counts = scan_dir($dirname."/".$file);
$count['dir'] += $counts['dir'];
$count['file'] += $counts['file'];
}
}
}
closedir($dir);
return $count;
}
$dirname = "/home/user/path";
$count = scan_dir($dirname);
echo "There are $count[dir] catalogues and $count[file] files.<br>";
?>
In my opnion, you should separate counting file & counting dir to 2 different function. It will clear things up:
<?php
function scan_dir_for_file($dirname) {
$file_count = 0 ;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if($file != "." && $file != "..") {
if(is_file($dirname."/".$file))
{
++$file_count;
} else {
$file_count = $file_count + scan_dir($dirname."/".$file);
}
}
}
return $file_count
}
?>
The directory_count function is similar.