lynxus Posted March 3, 2009 Share Posted March 3, 2009 Save what my script echos to a file. Hi guys, I have a load of PHP scripts that get some basic data from a form and then echo shed loads of config for a cisco device.. Lets say something liek below.. ( This is only a basic example ) $ip = $_POST['ip']; $subnet = $_POST['subnet']; $description = $_POST['description']; echo ' conf t <br> interface x/x <br> ip address '.$ip.' '.$subnet.' <br> description '.$description.' <br> no shutdown <br> '; This is a basic example that would output the config to paste onto a device. My question is HOW can i get this output to save to a file when the script runs? I need something simple to implement as i have a lot of these and some are MASSIVE. So a easy way to catch the script output that gets sent to teh browser each time it gets run from a post would be GrEAT.. ie: ScriptOutput > file $description_$date.cfg Any ideas? Thanks L Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/ Share on other sites More sharing options...
Mark Baker Posted March 3, 2009 Share Posted March 3, 2009 Take a look at the output buffering functions Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775618 Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 More accurately ob_start ob_get_contents and ob_end_clean Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775620 Share on other sites More sharing options...
lynxus Posted March 3, 2009 Author Share Posted March 3, 2009 Cheers guys, From what i can tell though is it will output the data only once? I still want data to be sent to teh browser, But i also want it sent to a file . Hummm. Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775624 Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 ob_end_flush You just have to use a different function to send it to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775627 Share on other sites More sharing options...
Mark Baker Posted March 3, 2009 Share Posted March 3, 2009 ob_start(); // Enable output buffer echo 'Hello World'; echo '<br /> more stuff here'; echo '<br />farewell cruel world'; $output = ob_get_contents(); // Get all the output that we've just buffered into a single variable ob_end_flush(); // Send the output buffer to the browser file_put_contents('outFile.txt',$output); // Write the output that we put in $output to a file Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775632 Share on other sites More sharing options...
lynxus Posted March 3, 2009 Author Share Posted March 3, 2009 Humm , ive tried: ob_start(); $ip = $_POST['ip']; $subnet = $_POST['subnet']; $description = $_POST['description']; echo ' conf t <br> interface x/x <br> ip address '.$ip.' '.$subnet.' <br> description '.$description.' <br> no shutdown <br> '; $LogToFile = ob_get_contents(); $myFile = "test.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "$LogToFile"; fwrite($fh, $stringData); fclose($fh); ob_end_clean(); But that doesnt seem to work, ( No errors mind ) Maybe im just being stupid. Sorry im not that great with php Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775636 Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 You did not use "flush" you used "clean" instead. ob_start(); $ip = $_POST['ip']; $subnet = $_POST['subnet']; $description = $_POST['description']; echo ' conf t <br> interface x/x <br> ip address '.$ip.' '.$subnet.' <br> description '.$description.' <br> no shutdown <br> '; $LogToFile = ob_get_contents(); $myFile = "test.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "$LogToFile"; fwrite($fh, $stringData); fclose($fh); ob_end_flush(); // note the flush not clean. Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775643 Share on other sites More sharing options...
lynxus Posted March 3, 2009 Author Share Posted March 3, 2009 Ah sweet, Looks like it gets the data YAY However im having permission errors with apache. On the off chance, I dont suppose anyone knows how to resolve it ? [Tue Mar 03 17:55:29 2009] [error] [client 195.216.xxx.xxx] PHP Warning: file_put_contents(outFile.txt) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Permission denied in /var/www/html/101.php on line 318, referer: http://omitted.com/101.html ( Its a linux server ) If no one knows how to fix then thanks very much for you help on getting the output into a string for me Its been a pest for a while and now im closer than ever to get it working yay Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775652 Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 The file being written to needs to have Write permissions. chmod 0777 will grant it full permissions to anyone. Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775658 Share on other sites More sharing options...
lynxus Posted March 3, 2009 Author Share Posted March 3, 2009 It will be a dynamiccly created file ( with a date and time in it when ive finished . ) How do i allow the script to create files in the dir Logs . Thsi is what i have for the Dir at the mo. drw-rw-rw- 2 apache apache 4096 Mar 3 18:00 Logs Ive tried root root , But still nada. Thanks L Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775663 Share on other sites More sharing options...
lynxus Posted March 3, 2009 Author Share Posted March 3, 2009 Ah sweet, i just 777'd the directory. Probably not uber secure, but ive touched an index.html in that dir. Thanks so much for your help!!! Thanks again L Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775665 Share on other sites More sharing options...
Mark Baker Posted March 3, 2009 Share Posted March 3, 2009 Well apache shouldn't be running as root anyway, typically it would be running as daemon Check the entries for: User daemon Group daemon in httpd.conf Quote Link to comment https://forums.phpfreaks.com/topic/147757-solved-save-what-my-script-echos-to-a-file/#findComment-775666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.