Adding carriage return within a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Adding carriage return within a text file

    I am creating a text file from an array. I need a specific format with blank lines inserted between lines of data. I need "0D 0A 0D 0A", but all I am getting is "0A 0A".

    My app is run on a windows xP platform.

    Here is how I store the data in the array
    Code:
    		array_push($cData, 	array(
    			$aProject["Project_Name"],
    			$aProject["Point_Page1_Depth"],
    			$aProject["Water_Unit_Wt"],
    			$aProject["Coeff_of_Consol_Factor"],
    			$aProject["Name"],
    			$aProject["Datum"]));
    
    //note the empty cell which in my previous language gave me 0D 0A, but in php only generates 0A
    		[B]array_push($cData, 	array(""));[/B]
    		array_push($cData, 	array("**POINT"));

    I steam out the data for download like this
    Code:
    			/*
    			-------------------------------------------------------
    			create handle for raw data file
    			-------------------------------------------------------
    			*/
    			$rawData = "c:/temp/test.csv";
    
    			/*
    			-------------------------------------------------------
    			look for file if found delete it because "into outfile" 
    			will not overwrite existing file
    			-------------------------------------------------------
    			*/
    			$fh = fopen($rawData, 'w') or die("can't open file");
    			fclose($fh);
    			unlink($rawData);
    
    			/*
    			-------------------------------------------------------
    			open file using handle
    			-------------------------------------------------------
    			*/
    			$fh = fopen($rawData, 'w') or die("can't open file");
    
    			/*
    			-------------------------------------------------------
    			loop through main array $cData
    			and write each sub-array data to file as a line of data
    			then close input file.
    			-------------------------------------------------------
    			*/
    			foreach ($cData as $fields) {
    				fputcsv($fh, $fields);
    			}
    			
    			fclose($fh);
    
    			/*
    			-------------------------------------------------------
    			create handle for gInt file 
    			-------------------------------------------------------
    			*/
    			$outputFileName = 'gINT_upload.txt';
    			$fullOutputName = "c:/temp/".$outputFileName;
    
    			/*
    			-------------------------------------------------------
    			look for exiting output file, if found delete it.
    			-------------------------------------------------------
    			*/
    			$fh = fopen($fullOutputName, 'w') or die("can't open file");
    			fclose($fh);
    			unlink($fullOutputName);
    
    			/* 
    			-------------------------------------------------------
    			create final output file 
    			-------------------------------------------------------
    			*/
    			$fh = fopen($fullOutputName,"a+") or die("can't open file");
    
    			/*
    			-------------------------------------------------------
    			write into output file
    			-------------------------------------------------------
    			*/
    			fputs( $fh,rtrim(file_get_contents($rawData)) );
    
    			/* 
    			-------------------------------------------------------
    			close output file 
    			-------------------------------------------------------
    			*/
    			fclose($fh);
    
    			/*
    			-------------------------------------------------------
    			force download request to open on client.
    			-------------------------------------------------------
    			*/
    			if($result) {
    				header('Pragma: public');
    				header('Expires: 0');
    				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    				header('Cache-Control: public');
    				header('Content-Description: File Transfer');
    				header('Content-Type: plain/text');
    				header('Content-Disposition: attachment; filename='.$outputFileName);
    				header('Content-Transfer-Encoding: ascii');
    				header('Content-Length: ' . filesize($fullOutputName));
    
    				@readfile($fullOutputName);
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    I see my problem is "fputcsv($f h, $fields);" - this only adds 0a to the end of each line

    whereas I can add 0a 0d with "fputs ($fh, "\r\n" );"

    So the question becomes is there a way to add the "0d" with fputcsv?

    The nice feature with fputcsv it it automatically adds delimiters around my text that contains commas.

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Well fputs( $fh, "\r\n" ); is the way I got it to work.

      Comment

      Working...