Hi I'm really puzzled on this one. I'm sending data from a form to be written on a line inside a file. However I've tested this a few times now and occasionally the line will end early when writing and it really messes things up due to another process depending on the data inside this file.

An example of the file would be:

28 Aug - 15:27||Sydney||Istanbul||15000||99
28 Aug - 15:31||Paris||Tripoli||1990||98
28 Aug - 15:38||Frankfurt||Tunis||1483||97
28 Aug - 15:41||Dublin||Thessaloniki||2608||99
28 Aug - 15:41||Novosibirsk||Sofia||4000||99
28 Aug - 16:01||Hong Kong||Sydney||7200||99
28 Aug - 16:14||London||Melbourne||15000||99
28 Aug - 16:24||Tunis||Mogadishu||5277||97
28 Aug - 16:36||Graz||Tunis||1219||95
28 Aug - 16:37||Graz||Istanbul||1274||95
28 Aug - 17:42||Dublin
||Dubai
||3000||99
28 Aug - 17:43||Kuala Lumpar
||Dubai
||5000||99
28 Aug - 17:47||Kuala Lumpar
||Sydney
||5000||99
28 Aug - 18:07||New York
||Rome
||6000||95
28 Aug - 18:07||New York
||Rome
||6000||99
28 Aug - 18:11||New York||Rome||6000||95

Snippet of the PHP code:

$route_dist = preg_replace('/[^0-9]/', '', $route_dist);
	
		// Weekly files for routes
		$filename = ROUTES_DIR . '/' . date('W-My') . '.txt';
	
		// Connect to routes file
		if($fp = fopen($filename, 'a+')) do {
		
			$line = fgets($fp);
						
			if(!empty($line)) { 
			
				list($date, $dept, $dest, $dist, $demd) = explode('||', $line);
			
				$row++;
				$exist[$row] = array($date, $dept, $dest, $dist, $demd);
			
			}
		
		} while (!feof($fp));
		
		unset($row);

$add_route = fwrite($fp, date('d M - H:i') . '||' . $route_dept . '||' . $route_dest . '||' . $route_dist . '||' . $route_demd . "\n");

fclose($fp);

Recommended Answers

All 2 Replies

Looks like you have carriage returns in the data do

fwrite($fp, str_replace("\n", '', (date('d M - H:i') . '||' . $route_dept . '||' . $route_dest . '||' . $route_dist . '||' . $route_demd) . "\n");

Thanks, worked a treat :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.