ive been having some problems manipulating the index.php with the .htaccess , when i submit the form and then choose the Delete/Put button, it doesnt go in the condition if($_SERVER["REQUEST_METHOD"]==DELETE/PUT) it goes in the the condition where the request method is GET , for the method POST i had to move the form.html file to another directory so that its doesn't change its url when i launch it on the browser i tried searching on the internet but i couldn't find something that could help, id really appreciate it if anyone can help! here below is the source code of 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 code:
<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>
and the form.html code :
<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>
:)