【问题标题】:execute a script that outputs a result in a txt file or ini using the cmd line in php使用 php 中的 cmd 行执行在 txt 文件或 ini 中输出结果的脚本
【发布时间】:2012-11-08 10:40:55
【问题描述】:

我想在 Windows 的命令行中运行一个简单的行来运行一个读取 IP 地址的脚本,并且我希望它在文本文件中输出。

这是脚本

<?php
     $lines = file('ipaddresses.ini', FILE_IGNORE_NEW_LINES); 
     foreach($lines as $line){
         echo $line;  

         $host   = $line;
         echo("Ping Output:");
         system("ping -n 4 $host",$result);

         if ($result == 0)
             echo "Ping successful!";
         else
             echo "Ping unsuccessful!";

     } 
 ?> 

这是命令行

C:\wamp\bin\php\php5.3.13>php.exe -f "C:\wamp\www\hello.php"

【问题讨论】:

  • 呃……那么所有&lt;br&gt; 标签是怎么回事?这是某种疯狂的伪 PHP/HTML/废话,我喜欢它!
  • @deed02392 rab 以错误的方式编辑它。

标签: php command-line export output


【解决方案1】:

可以通过命令行将输出保存到文件中。将&gt; [name of file] 附加到命令中,然后它将脚本的所有输出存储到指定的文件名中。

C:\wamp\bin\php\php5.3.13>php.exe -f "C:\wamp\www\hello.php" > C:\output.txt

【讨论】:

  • 当我运行 cmd 行并提供要保存的文件时。它给了我这个错误:警告:文件(ipaddresses.ini):无法打开流:第 2 行的 C:\wamp\www\hello.php 中没有这样的文件或目录调用堆栈:0.0005 326792 1. {main}( ) C:\wamp\www\hello.php:0 0.0005 326912 2. file() C:\wamp\www \hello.php:2 警告:在 C:\wamp\www\hello 中为 foreach() 提供的参数无效第 3 行的 .php 调用堆栈:0.0005 326792 1. {main}() C:\wamp\www\hello.php:0
【解决方案2】:

将其保存到C:\wamp\www\hello.php,然后像这样执行:

>php.exe -f "C:\wamp\www\hello.php" > C:\wamp\www\output.txt

带有 Windows 命令提示符。确保有一个名为 ipaddresses.ini 的文件仅包含换行符分隔的有效 IP 地址。

<?php
$lines = file('ipaddresses.ini', FILE_IGNORE_NEW_LINES);
foreach($lines as $line)
{ 
    echo $line . "\n";
    $ip = $line;
    exec("ping -n 3 $ip", $outcome, $status);

    if ($status == 0)
    {
        echo "Ping successful!\n";
    }
    else
    {
        echo "Ping unsuccessful!\n";
    }
}
?>​

【讨论】:

  • 错误仍然出现我删除了所有其他 IP 地址并只添加了一个,错误说 \confirm.php 中没有文件或目录,这意味着看起来确实正确
  • 当我使用 cmdline 时,我的 fopen 和 foreach 语句似乎不想运行
  • confirm.php 来自哪里?您的 ipaddresses.ini 文件存储在哪里?
  • confirm.php 存储在 c:\wamp\www\ping\ 和 ipaddresses.ini 存储在同一位置。当我使用 cmdline 调用脚本时,我似乎确实喜欢 fopen
  • fopen 不在乎是谁调用的。如果您在 Windows 中使用 PHP 中的路径转义反斜杠或使用正斜杠,请不要忘记。 C:\\foo\\barC:/foo/bar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 2020-12-21
相关资源
最近更新 更多