-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveMovie.php
143 lines (106 loc) · 4.36 KB
/
saveMovie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
session_start();
require_once("query.php");
if ($_POST['action'] == 'save') {
$_SESSION["englishTitle"] = $_POST["englishTitle"];
$_SESSION["releaseDate"] = $_POST["releaseDate"];
$_SESSION["director"] = $_POST["director"];
$_SESSION["originalTitle"] = $_POST["originalTitle"];
$_SESSION["previousPage"] = 'insertMovie.php';
$englishTitle = cleanString($_POST["englishTitle"]);
$releaseDate = trim(str_replace("'", "\\'", $_POST["releaseDate"]), " \t\n");
$director = trim(str_replace("'", "\\'", $_POST["director"]), " \t\n");
$region = trim(str_replace("'", "\\'", $_POST["selectRegion"]), " \t\n");
$originalTitle = trim(str_replace("'", "\\'", $_POST["originalTitle"]), " \t\n");
$sql= "INSERT INTO `movie` (`enTitle`, `releaseDate`, `director`, `originalTitle`, `region_idregion`) VALUES ('".$englishTitle."', '".$releaseDate."', '".$director."', '".$originalTitle."', '".$region."')";
// valider champs
if(!empty($englishTitle) && !empty($releaseDate) && !empty($director) && !empty($region) && !empty($originalTitle)){
// envoyer fichier
$target_dir = "./pic/";
$fileName = basename($_FILES["fileToUpload"]["name"]);
$fileNameBase = date("Y-m-d-H:i:s")."jpg";
$target_file = $target_dir.$fileNameBase;
$uploadOk = TRUE;
$msgErro = "Erreur lors de l'envoie du fichier";
$imageFileType = substr(pathinfo($fileName, PATHINFO_EXTENSION), -3);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = TRUE;
} else {
$uploadOk = FALSE;
}
// Check if file already exists
if (file_exists($target_file)) {
$uploadOk = FALSE;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
$msgErro = "La taille du fichier ne peut pas dépasser 5M!";
$uploadOk = FALSE;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
$msgErro = "Seulement les fichiers JPG, JPEG, PNG & GIF sont autorisés!";
$uploadOk = FALSE;
}
// s
if ($uploadOk) {
// choisir la requete selon le succes de l'envoye du fichier
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// Préparation de la requête
$sql= "INSERT INTO `movie` (`enTitle`, `releaseDate`, `director`, `originalTitle`, `photoAddress`, `region_idregion`) VALUES ('".$englishTitle."', '".$releaseDate."', '".$director."', '".$originalTitle."','".$fileNameBase."', '".$region."')";
}
if ($conn->query($sql) === TRUE) {
// stocker l`id du film qu`on vient de rajouter
$_SESSION['newmovieId'] = $conn->insert_id;
// libere variables
unset($_SESSION["englishTitle"]);
unset($_SESSION["releaseDate"]);
unset($_SESSION["director"]);
unset($_SESSION["originalTitle"]);
unset($_SESSION["regionName"]);
unset($_SESSION["previousPage"]);
$conn->close();
// appeler le nouveau formulaire
include 'insertTranslation.php';
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
$conn->close();
}
// afficher message d'erreur liee 'a l'envoie du fichier
} else {
echo "<script>
alert('".$msgErro."');
window.location.href='insertMovie.php';
</script>";
}
} else {
echo "<script>
alert('Tous les champs doivent être remplis. Ressayez, svp!');
window.location.href='insertMovie.php';
</script>";
}
} elseif($_POST['action'] == 'insertRegion'){
$_SESSION["englishTitle"] = $_POST["englishTitle"];
$_SESSION["releaseDate"] = $_POST["releaseDate"];
$_SESSION["director"] = $_POST["director"];
$_SESSION["originalTitle"] = $_POST["originalTitle"];
$_SESSION["regionName"] = '';
$_SESSION["previousPage"] = 'insertMovie.php';
// afficher formularie rajouter region
include 'insertRegion1.php';
}else{
// retourner `a la page d`accueil
session_destroy();
include 'index.php';
}
function cleanString($char){
$char = str_replace("'", "\\'", $_POST["englishTitle"]);
$char = str_replace("\t", '', $char);
$char = str_replace("\r", '', $char);
$char = str_replace("\n", '', $char);
$char = trim($char, " ");
return $char;
}
?>