php表单与exec,file_get_contents和explode

时间:2013-06-22 00:33:12

标签: php forms exec file-get-contents trim

此表单的目的是更新服务器上的文件,并在表单生成后将更新的更改反映在表单上。以下是我在一些用户的帮助下获得的当前代码:

<?php
    $file = "/var/www/html/colors/color.choices";
    $contents = file($file, FILE_SKIP_EMPTY_LINES);
    $color = explode("=", $contents[0]);
    $size = explode("=", $contents[1]);
    $shape = explode("=", $contents[2]);

    if(!empty($_REQUEST['color_choice'])){
        exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /var/www/html/colors/color.choices");
        echo 'File color choice has been updated';
    }
    if(!empty($_REQUEST['size_choice'])){
        exec('sed -i '.escapeshellarg('s/size=.*/size='.$_REQUEST['size_choice'].'/g')." /var/www/html/colors/color.choices");
        echo 'File size choice has been updated';
    }
    if(!empty($_REQUEST['shape_choice'])){
    exec('sed -i '.escapeshellarg('s/shape=.*/shape='.$_REQUEST['shape_choice'].'/g')." /var/www/html/colors/color.choices");
    echo 'File shape choice has been updated';
    }

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="color_choice">;
        <option value="red" <?php if($color[1] == 'red'){?>selected="selected"<?php }?>>red</option>;
        <option value="black" <?php if($color[1] == 'black'){?>selected="selected"<?php }?>>black</option>;
    </select>
    <input type="submit" name="Submit" value="Submit" />
</form>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="size_choice">;
        <option value="large" <?php if($size[1] == 'large'){?>selected="selected"<?php }?>>large</option>;
        <option value="small" <?php if($size[1] == 'small'){?>selected="selected"<?php }?>>small</option>;
    </select>
    <input type="submit" name="Submit" value="Submit" />
</form>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="shape_choice">;
        <option value="round" <?php if($shape[1] == 'round'){?>selected="selected"<?php }?>>round</option>;
        <option value="square" <?php if($shape[1] == 'square'){?>selected="selected"<?php }?>>square</option>;
    </select>
    <input type="submit" name="Submit" value="Submit" />
</form>

更新:我已经调整了代码,似乎几乎让它工作了。最后一个选项现在始终按预期工作。因此,在这种情况下,“形状”值现在可以完美地运行。但是,“颜色”和“大小”选项会更新color.choices文件,但更新不会反映在表单本身上。为什么最后一个选项现在适用于表单,但前两个选项不起作用?

1 个答案:

答案 0 :(得分:0)

你可以这样做。我会给你两个选择。

第一个选项是使用file();

<?php
    $file = "/var/www/html/colors/color.choices";
    $contents = file($file, FILE_SKIP_EMPTY_LINES);
    $color = explode("=", $contents[0]);

    if(!empty($_REQUEST['color_choice'])){
        exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /var/www/html/colors/color.choices");
        echo 'File color choice has been updated';
    }
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="color_choice">;
        <option value="red" <?php if($color[1] == 'red'){?>selected="selected"<?php }?>>red</option>;
        <option value="blue" <?php if($color[1] == 'blue'){?>selected="selected"<?php }?>>blue</option>;
        <option value="black" <?php if($color[1] == 'black'){?>selected="selected"<?php }?>>black</option>;
        <option value="orange" <?php if($color[1] == 'orange'){?>selected="selected"<?php }?>>orange</option>;
    </select>
    <input type="submit" name="Submit" value="Submit" />
</form>

因此,如果您的color.choices文件在下面有这些选项。然后你的数组将从0,1,2开始,就像我上面用你的颜色一样$contents[0]将是颜色线。 $content[1]将是形状线。然后,您仍然需要将它们分解为新的变种。

color=blue
shape=round
size=large

第二个选项推荐)您可以这样做。

我要做的是将它作为ini文件。您已经以ini的形式使用该文件,那么为什么不让它更容易使用值?

color.choices重命名为color_choices.ini

那么你的代码将是这样的。

<?php
    $attr = parse_ini_file("/var/www/html/colors/color_choices.ini");
    $color = $attr["color"];
    $size = $attr["size"];
    $shape = $attr["shape"];

    if(!empty($_REQUEST['color_choice'])){
        exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /var/www/html/colors/color_choices.ini");
        echo 'File color choice has been updated';
    }
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="color_choice">;
        <option value="red" <?php if($color == 'red'){?>selected="selected"<?php }?>>red</option>;
        <option value="blue" <?php if($color == 'blue'){?>selected="selected"<?php }?>>blue</option>;
        <option value="black" <?php if($color == 'black'){?>selected="selected"<?php }?>>black</option>;
        <option value="orange" <?php if($color == 'orange'){?>selected="selected"<?php }?>>orange</option>;
    </select>
    <input type="submit" name="Submit" value="Submit" />
</form>

现在里面的color_choices.ini看起来像这样。

[attributes]
color=blue
shape=round
size=large

使用ini文件更好,因为您可以使用key名称,而无需知道它们在文件中的顺序。解析后,它已经是数组的形式,因此您只需调用$attr["color"]$attr["shape"]等,并且它们在color_choice.ini文件中的顺序无关紧要。您可以根据需要添加任意数量的属性。另请注意,表单变量也会更改$color

e.g。

echo $attr["size"]; //value will be large