Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hullaballo2001/e5121ef6f2ad7036320fb0e40b2d0f83 to your computer and use it in GitHub Desktop.
Save Hullaballo2001/e5121ef6f2ad7036320fb0e40b2d0f83 to your computer and use it in GitHub Desktop.
Quête _PHP_Avancé_Laisse pas traîner ton file.
<form method="POST" enctype="multipart/form-data">
<label for="imageUpload">Upload an profile image</label>
<br/>
<input type="file" name="avatar" id="imageUpload" />
<br/>
<button name="send">Send</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" ){
// Si Homer send le fichier
if (isset($_POST['send'])){
// sécurisation
// vérification extension autorisée
$extension = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
$extensions_ok = ['jpg','png', 'gif', 'webp'];
if (!in_array($extension, $extensions_ok)){
echo 'Veuillez sélectionner une image de type Jpg ou Jpeg ou Png ou Gif !';
die;
}
// vérification du poids de l'image
$fileSize = $_FILES['avatar']['size'];
$maxFileSize = 1048576;
if ($fileSize > $maxFileSize){
echo 'Votre fichier doit faire 1Mo maxi !';
die;
}
// Si tout est ok on upload
$uploadDir = 'public/uploads/'; // dossier
$uniqueName = md5(uniqid(rand(), true)); // nom de fichier unique
$uploadFile = "{$uploadDir} {$uniqueName}.{$extension}"; // chemin complet avec extension
// $uploadFile = $uploadDir . basename($_FILES['avatar']['name']); // avant le nom unique
move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadFile); // copie du fichier tmp à sa place
echo "Image uploaded successfully :)";
echo "<br>";
echo "<br>";
// Afficher sa photo
echo "<img src='", $uploadFile, "'", 'alt="Homer Simpson"', "'", " />";
// Affiche nom et prénom en dur
echo '<div align="left"><font face="arial" size="3" color="blue"> Homer Simpson </font><br /> ';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment