Solved

Code isnt running

Posted on 2016-09-06
5
61 Views
Last Modified: 2016-09-09
<?php 
error_reporting(E_ALL);
ini_set('display_errors' ,1);


$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');
            //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') !== false || $block_start) {
                        $header_end = true; $block_start = true;
                         if(strpos($line, 'N1') !== 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 {
                        //append to the header
                        if($header_end) {
                            $footer .= $line."\n";
                           
                        } else {
                            $header .= $line."\n";
                        }
                    }
      
                }
               
                //get payor blocks and create a file foreach payor
                $new_files = array();
                foreach($payor_blocks as $block) {
                    $filename = $file . "_" . $count;
                    $count++;
                    $new_files[] = array(
                        'name' => $filename,
                        'content' => $header."\n".$block."\n".$footer	
                    	
                    );
                }
               
                //loop through new files and create them
                foreach($new_files as $new_file) {
                    //create file
                	$myfile = fopen($new_file['name'], "w");
   
                    //put contents in the file               	
                	fwrite($myfile, $new_file['content']);	
              		//close the file
                	fclose($myfile);
                }

            } else {

            	// DO what u were doing in script #1
           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($file, $directory_with_files.'/'.$filerenamed);
            		}
            		echo $refnumber . "\n";
            		
            	}
            
            	}
            	 
            }

        }

        // DONE - close the file
        fclose($handle);
    }
    
}
?>

Open in new window


I have the code above I want to be able to open it from my $directory_with_files to see the files and go through the code and put the new files then combined them into one. However, my code isnt running and no errors are showing
0
Comment
Question by:Jasmine Ikhreishi
  • 2
  • 2
5 Comments
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41786166
Its printing out, its just not putting the new files in my folder.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41786186
Maybe check the answer here?  It's not clear what you want to do.
https://www.experts-exchange.com/questions/28967928/Find-and-redo-files.html#a41786159

This seems to be a multi-part application.  Am I understanding this correctly?
(1) Collect a list of files, according to some characteristic
(2) Copy these files into a directory that is named in part with today's date
(3) Read all these files (in some order?) and combine the contents of the files into one file
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41786244
Also can you just tell me how to be able to combine all the files thats in that dir to one file that would be called combined.txt ?
I used this and it wouldnt work:
  $output = 'combined.txt';
                foreach ($myfile as $my_file) {
                	fwrite($output,file_get_contents($my_file));
                }

Open in new window

0
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41786280
You are creating your new files in the root directory instead of in the $directory_with_files. Change line 116 to include the path to the $directory_with_files.
                	$myfile = fopen($directory_with_files.DIRECTORY_SEPARATOR.$new_file['name'], "w");

Open in new window

Also, lines 71 and 127 read through the file at $handle without rewinding between them. So when the while loop at line 127 begins, the file pointer is already at the end of the file.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41786306
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question