dth2331 2013-05-29 20:00
浏览 63
已采纳

ajax / php无法编辑文件

I'm developing a web page that should read and change the contents of specific files on the server.

These files will only have two values: 1 or 0. Reading/changing the contents will be made via combo boxes with OnChange. Basically the idea is to control appliances via General Purpose Input/Outputs (GPIO's). The electronics part is all done, I just need to finish the web programing part and got stuck on it.

I'm not experienced in programing at all, but with some snippets found here and there, I was able to implement part of it with AJAX/PHP.

So far, I'm able to read the values, but unable to change it even though I'm building the correct command with "escapeshellarg".

Also, I was expecting to have two interactive areas in the page but only the original is working.

Could anyone point me into the right direction? Any help/suggestion/comment will be welcomed.


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

  • 写回答

1条回答 默认 最新

  • doubeiji2602 2013-05-29 20:11
    关注

    By applying the escapeshellcmd your > 0 or > 1 is turned into \\> 0 and \\> 1 I guess that is why it doesnt work. You are not using escapeshellcmd, you are using escapeshellarg.

    You might want to check the file permissions also.

    However, instead of using system calls, have you ever thought about using file_exists, file_get_contents or file_put_contents .

    With those functions and writable dirs/files, you can achieve exactly what you are doing, without making system calls. Plus, it would be more portable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘