Ajax /php无法编辑文件


ajax/php not able to edit files

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

这些文件只有两个值: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>

getinfo.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

通过应用escapeshellcmd,您的> 0> 1变成''> 0''> 1,我想这就是为什么它不起作用。你没有使用escapeshellcmd,你使用的是escapeshellarg

您可能还需要检查文件权限。

然而,除了使用系统调用,您是否考虑过使用File_exists, file_get_contents或file_put_contents .

有了这些函数和可写的dirs/文件,你可以准确地完成你要做的事情,而不需要进行系统调用。另外,它更便于携带。