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.

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)