Jump to content

Noob in need of help output to textfile


csandbach

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/64325-noob-in-need-of-help-output-to-textfile/
Share on other sites

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.

<?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?

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 stable
Bah!  I need to read more carefully; I don't know if file redirection works in DOS.

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.