MrCastrovinci Posted June 16, 2011 Share Posted June 16, 2011 Hello all, I am trying to save my php results (post data) to a file after displaying it. I have been trying to do it using this code below but the url does not exists and it has to be a file to use file get contents. $string = file_get_contents('http://www.example.com/'); echo $string; // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // grab URL and pass it to the browser $string = curl_exec($ch); echo $string; // close cURL resource, and free up system resources curl_close($ch); Then I tried this code below but it saves both the PHP and HTML. I need only the HTML part. As if you were viewing the Source code of the HTML page. //buffer output ob_start(); //process form //your code goes here //save & flush buffer in a file $buffer = ob_get_flush(); file_put_contents('buffer.txt', $buffer, FILE_APPEND); Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/ Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 Try something like this: $data = ''; foreach($_POST as $k=>$v) { $data = $k . ": " . $v . "/r/n"; } $file = 'file.txt'; if ($fh = fopen($file, 'w')) { fwrite($fh, $data); fclose($fh); } else { trigger_error('cant open file',E_USER_ERROR); } Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/#findComment-1230576 Share on other sites More sharing options...
WebStyles Posted June 16, 2011 Share Posted June 16, 2011 file_put_contents(); is the preferred method for writing to txt files. Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/#findComment-1230619 Share on other sites More sharing options...
MrCastrovinci Posted June 16, 2011 Author Share Posted June 16, 2011 Try something like this: $data = ''; foreach($_POST as $k=>$v) { $data = $k . ": " . $v . "/r/n"; } $file = 'file.txt'; if ($fh = fopen($file, 'w')) { fwrite($fh, $data); fclose($fh); } else { trigger_error('cant open file',E_USER_ERROR); } Thanks for the reply. That would work if I was only trying to save the PHP results, I also need to save the HTML of the page. I have attached my code so you can see what I have created. There is est.html and workestimate.php3. See the page makes results inside the html. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/#findComment-1230639 Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 Oh, I see. This little test worked for me: $array = range(0,9); ob_start(); foreach($array as $a) { echo $a.'<br />'; } $buffer = ob_get_flush(); file_put_contents('test.txt',$buffer,FILE_APPEND); ob_end_clean(); test.txt contains: 0<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /> Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/#findComment-1230641 Share on other sites More sharing options...
MrCastrovinci Posted June 16, 2011 Author Share Posted June 16, 2011 Oh, I see. This little test worked for me: $array = range(0,9); ob_start(); foreach($array as $a) { echo $a.'<br />'; } $buffer = ob_get_flush(); file_put_contents('test.txt',$buffer,FILE_APPEND); ob_end_clean(); test.txt contains: 0<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /> I'm not sure I am explaining the correct way...... If i take the line for example below <td width="12%" bordercolor="#999999"> <div align="center"><b><?php echo $taxrate ?></b></div> I want the saved file to have all the HTML included and the PHP results which the saved file will look like this.... <td width="12%" bordercolor="#999999"> <div align="center"><b>tax rate=7%</b></div> </td> </tr> </td> </tr> Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/#findComment-1230680 Share on other sites More sharing options...
MrCastrovinci Posted June 16, 2011 Author Share Posted June 16, 2011 ok Solved.....LOL My second code actually worked. I was doing something wrong but it works now! Thanks all //buffer outputob_start();//process form//your code goes here//save & flush buffer in a file$buffer = ob_get_flush();file_put_contents('buffer.txt', $buffer, FILE_APPEND); Link to comment https://forums.phpfreaks.com/topic/239555-saving-php-results/#findComment-1230745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.