index.php使用GET POST DELETE PUT方法

时间:2015-03-16 01:31:25

标签: php forms .htaccess

我在使用.htaccess操作index.php时遇到了一些问题,当我提交表单然后选择删除/放置按钮时,它不会进入条件if($ _ SERVER [“REQUEST_METHOD”] == DELETE /放) 它进入请求方法为GET的条件,对于方法POST我必须将form.html文件移动到另一个目录,以便当我在浏览器上启动它时它不会更改其URL 我尝试在互联网上搜索,但我找不到可以提供帮助的东西,如果有人能提供帮助,我真的很感激!下面是index.php的源代码:

<?php
function ajouterPortraits($f){

            $f = fopen($f,"a+");

            if($_POST != null){
                fwrite($f,"\n");
                fwrite($f, count(file("galerie.txt"))+1);
                fwrite($f," | ");

                if(isset($_POST["prenom"])){

                    fwrite($f, $_POST["prenom"]." | ");
                    unset($_POST["prenom"]);
                }
                if(isset($_POST["nom"])){
                    fwrite($f, $_POST["nom"]." | ");
                    unset($_POST["nom"]);
                }
                if(isset($_POST["sexe"])){
                    fwrite($f, $_POST["sexe"]." | ");
                    unset($_POST["sexe"]);
                }
                if(isset($_FILES)){
                    $tmpname = $_FILES['photo']['tmp_name'];
                    $newname= $_FILES['photo']['name'];
                    $result=move_uploaded_file($tmpname,$newname);
                    fwrite($f,$newname);
                }
            }
        fclose($f);

        }

function lireFichier($file){
            $tab = array();
            $personnes = file($file);

            foreach($personnes as $key => $var){
                $ar = explode('|',$var);
                $tab[$key]['identifiant']= $ar[0];
                $tab[$key]['nom']= $ar[1];
                $tab[$key]['prenom']= $ar[2];
                $tab[$key]['sexe']= $ar[3];
                $tab[$key]['photo']= $ar[4];
            }
            //var_dump($tab);
            return $tab;
        }

        function generateGallery($tab){

            echo "<table>";
            echo "<tr>";
            echo "  <th>Prenom</th>";
            echo "  <th>Nom</th> ";
            echo "  <th>Sexe</th> ";
            echo "  <th>Photo</th>";
            echo "  </tr>";

                $bouton1 = '<form method = "DELETE" action="index.php"><input type="submit" name = "request" value="DELETE"></form>';
                $bouton2 = '<form method = "PUT" action="index.php"><input type="submit" name = "request" value="PUT"></form>';
                for($i=0 ; $i < count($tab) ; $i++){
                    echo " <tr>";
                    echo " <td>".$tab[$i]['prenom']."</td> ";
                    echo " <td>".$tab[$i]['nom']."</td> ";
                    echo " <td>".$tab[$i]['sexe']."</td> ";
                    echo " <td> <img src= ".$tab[$i]['photo']." width=\"100\"></td> "; 
                    echo " <td>".$bouton1."</td>";
                    echo " <td>".$bouton2."</td>";
                    echo "</tr>";
                }
            echo"</table>";
        }

if($_SERVER["REQUEST_METHOD"]== "GET"){

        $personnes = lireFichier("galerie.txt");
        generateGallery($personnes);

}

if($_SERVER["REQUEST_METHOD"] == "POST"){

    ajouterPortraits("galerie.txt");
    $personne = lireFichier("galerie.txt");
    generateGallery($personne);

}

if($_SERVER["REQUEST_METHOD"] == "DELETE"){
    /*
    function supprimerPersonneDansGalerie() {
        $id_suppr = $_GET["request"];
        $file = fopen("galerie.txt", "w");

        if($file){ 
        while(!feof($fic)){ 
            fputs(""); 
        }
        for($i=0; $i<count($personnes); $i++){
            if(!($i == ($id_suppr-1))){
                $file = fputs($personnes[$i]);
            }
        }
        fclose($f);
        return $personnes;
        }
    }
    //APPELS DES FONCTIONS//
    $personnes = supprimerPersonneDansGalerie();
    generateGallery($personnes);*/
}


if($_SERVER["REQUEST_METHOD"] == "PUT"){


}

.htaccess代码:

    <IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME] !-f
RewriteCond %{REQUEST_FILENAME] !-d
RewriteRule TP3M/(.*)$ TP3M/index.php?request=$1 [QSA,NC,L]
</IfModule>

和form.html代码:

<form action="TP3M/index.php" method="POST" enctype="multipart/form-data" >
        <fieldset>
            <legend>Ajout de personne</legend>
            <label for="prenom">Prenom :</label>
            <input type="text" name="prenom" id="prenom" ><br>
            <label for="nom">nom :</label>
            <input type="text" name="nom" id="nom"><br>
            <input type="radio" name="sexe" value="Homme"/>Homme<br>
            <input type="radio" name="sexe" value="Femme"/>Femme<br>
            <input type="file" name="photo" accept="image/jpg"><br>

            <input type="submit" value="Valider">
        </fieldset>
    </form>

:)

0 个答案:

没有答案