0

我正在开发一个网页,该网页应该读取和更改服务器上特定文件的内容。

这些文件只有两个值:1 或 0。读取/更改内容将通过带有OnChange. 基本上,这个想法是通过通用输入/输出 (GPIO) 来控制电器。电子部分已经完成,我只需要完成网络编程部分并卡在上面。

我完全没有编程经验,但是通过到处找到一些片段,我能够使用 AJAX/PHP 实现其中的一部分。

到目前为止,我能够读取这些值,但即使我正在使用“escapeshellarg”构建正确的命令,也无法更改它。

另外,我希望页面中有两个交互区域,但只有原始区域在工作。

谁能指出我正确的方向?欢迎任何帮助/建议/评论。


pqp6.php


<html>
<head>
    <script>
        function showUser(str)
        {
            if (str=="")
            {
                document.getElementById("txtHint").innerHTML="";
                return;
            }

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","getinfo.php?q="+str,true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <?php                                      

        $file1="/var/www/file1";
        $file2="/var/www/file2";
        $output = shell_exec('cat '.escapeshellarg($file1));

        if($output == 0)
        {
            echo "zero ; ";
            $file1status = "off";
            $file1oposite = "on";
            $file1exec= "file1on";
        }
        else
        {
            echo "one ; ";
            $file1status= "on";
            $file1oposite= "off";
            $file1exec= "file1off";
        }

        echo "output:$output ; file1status:$file1status ; file1oposite:$file1oposite ; file1exec:$file1exec <br><br>";

        $output = shell_exec('cat '.escapeshellarg($file2));

        if($output == 0)
        {
            echo "zero ; ";
            $file2status = "off";
            $file2oposite = "on";
            $file2exec= "file2on";
        }
        else
        {
            echo "one ; ";
            $file2status= "on";
            $file2oposite= "off";
            $file2exec= "file2off";
        }

        echo "output:$output ; file2status:$file2status ; file2oposite:$file2oposite ; file2exec:$file2exec <br><br>";
        ?>

        <br>
        <br>                                    

        FILE 1:                                 
        <form>
            <select name="file1" onchange="showUser(this.value)">
                <option value="1,<?=$file1status?>"><?=$file1status?></option>
                <option value="1,<?=$file1oposite?>"><?=$file1oposite?></option>  
            </select>
        </form>
        <br>
        <div id="txtHint"><b>File 1 information will be listed here.</b>     </div>                     
        <br>
        <br>
        FILE 2:                                 
        <form>
            <select name="file2" onchange="showUser(this.value)">
                <option value="2,<?=$file2status?>"><?=$file2status?></option>
                <option value="2,<?=$file2oposite?>"><?=$file2oposite?></option>
            </select>
        </form>
        <br>
        <div id="txtHint"><b>File 2 information will be listed here.</b></div>
 </body>
</html>

获取信息.php:


<?php
$q=$_GET["q"];
$q_stripped = explode(",", $q);
$file_n = $q_stripped[0];
$file_command = $q_stripped[1];

$path="/var/www/file";

if($file_command == "on")
{
    $file_command = "1 > ";
}
else
{
    $file_command = "0 > ";
}

$command= "/bin/echo $file_command$path$file_n";
$escaped_command = escapeshellarg($command); 

echo "COMMAND: $escaped_command";
shell_exec($escaped_command);
echo "file_n=$file_n  ; file_command=$file_command ; "; 
?><?php

4

1 回答 1

0

通过应用escapeshellcmd你的> 0or> 1变成了\\> 0\\> 1我猜这就是它不起作用的原因。您没有使用 escapeshellcmd,您使用的是 escapeshellarg。

您可能还想检查文件权限。

但是,您有没有想过使用 file_existsfile_get_contentsfile_put_contents ,而不是使用系统调用。

使用这些函数和可写的目录/文件,您可以完全实现您正在做的事情,而无需进行系统调用。另外,它会更便携。

于 2013-05-29T20:11:20.457 回答