csandbach Posted August 10, 2007 Share Posted August 10, 2007 Hi, I'm new to PHP, im used to coding in asp, so please bare with me. What i would like is to output the result to a text file <?php $_ip = $_SERVER['REMOTE_ADDR']; echo "<b>Enter the IP or the domain name of the server that you are trying to route.</b><br>"; echo "<form method='post' action='?do=route'><input type='text' name='domain' class='input_login' value='$_ip'> <input type='submit' value='Route' class='input_login'></form>"; if($_GET['do'] == 'route') { $_domain = $_GET['domain']; echo "<pre>"; system ("tracert $_domain") ; echo "</pre>"; } echo "<br>"; $your_data = "This is the data to be stored in the text file."; // Open the file and erase the contents if any $fp = fopen("hostnamehere.txt", "w"); // Write the data to the file fwrite($fp, $your_data); // Close the file fclose($fp); ?> I have combined 2 files, a tracert file and one that writes to a text file, how do i define the output of the traceroute as a variable so i can pass it to the part that writes to a text file, i have tried a few ways but end up getting errors. Any help appreciated. Chris. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 Instead of system() use exec() or passthru(), which passes the output to an array. exec("tracert $_domain", $arr); $content = ""; foreach($arr as $value){ $content .= $value; } Then use fwrite($fp, $content) to write it to a file. Quote Link to comment Share on other sites More sharing options...
csandbach Posted August 10, 2007 Author Share Posted August 10, 2007 <?php $_ip = $_SERVER['REMOTE_ADDR']; echo "<b>Enter the IP or the domain name of the server that you are trying to route.</b><br>"; echo "<form method='post' action='?do=route'><input type='text' name='domain' class='input_login' value='$_ip'> <input type='submit' value='Route' class='input_login'></form>"; if($_GET['do'] == 'route') { $_domain = $_GET['domain']; exec("tracert $_domain", $arr); $content = ""; foreach($arr as $value){ $content = $value; } // Open the file and erase the contents if any $fp = fopen("hostnamehere.txt", "w"); // Write the data to the file fwrite($fp, $content); // Close the file fclose($fp); ?> I changed my code to the above, it does not do anything, im running 2k3 with iis 6 latest version of php 5 stable, should i look at changing anything in my php ini file? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 10, 2007 Share Posted August 10, 2007 Which operating system is this hosted on? If it's *nix: <?php $cmds = Array(); $cmds[] = "pwd"; $cmds[] = "ls -lh"; $cmds[] = "mysql -v"; $outfile = "/path/to/file/out.txt"; $redir = ">"; foreach($cmds as $cmd){ exec("echo \"{$cmd}\n\" {$redir} {$outfile}"); $redir = ">>"; exec("{$cmd} {$redir} {$outfile};echo \"\n\n\" {$redir} {$outfile}"); } ?> (EDIT) im running 2k3 with iis 6 latest version of php 5 stableBah! I need to read more carefully; I don't know if file redirection works in DOS. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 it is $content .= $value . Anyway try in a different file using echo exec('dir') or smth to see if it is executed. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 Also from the php manual When safe mode is enabled, you can only execute executables within the safe_mode_exec_dir. For practical reasons it is currently not allowed to have .. components in the path to the executable. It applies to other execute commands such as system(), so make sure safe_mode is 0. Quote Link to comment 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.