Skip to content

Instantly share code, notes, and snippets.

@LouLeGrain
Created April 24, 2018 18:41
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 LouLeGrain/524e09052411992d3f958e75f37c9204 to your computer and use it in GitHub Desktop.
Save LouLeGrain/524e09052411992d3f958e75f37c9204 to your computer and use it in GitHub Desktop.
[Uploading script] Uploading a file (an image) #PHP
public static function uploadImg()
{
$dossier = 'participations/';
if (!file_exists($dossier)) {
mkdir($dossier, 0700);
}
$fichier = basename($_FILES['imgParticipation']['name']);
$taille_maxi = 3000000;
$taille = filesize($_FILES['imgParticipation']['tmp_name']);
$extensions = array('.png', '.gif', '.jpg', '.jpeg');
$extension = strrchr($_FILES['imgParticipation']['name'], '.');
//Début des vérifications de sécurité...
if (!in_array($extension, $extensions)) //Si l'extension n'est pas dans le tableau
{
$_SESSION['infos']['warning'] = 'Vous devez uploader un fichier de type png, gif, jpg, ou jpeg';
header('Location: tableaudebord.php');
exit();
}
if ($taille > $taille_maxi) {
$_SESSION['infos']['warning'] = 'Le fichier est trop gros... La taille maximale est de 3Mo';
header('Location: tableaudebord.php');
exit();
}
//On formate le nom du fichier ici...
$fichier = strtr($fichier,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$fichier = preg_replace('/([^.a-z0-9]+)/i', '-', $fichier);
if (move_uploaded_file($_FILES['imgParticipation']['tmp_name'], $dossier . $fichier)) //Si la fonction renvoie TRUE, c'est que ça a fonctionné...
{
// Do some validation stuff / notification, etc ...
} else //Sinon (la fonction renvoie FALSE).
{
// Do some error 'handling' stuff
}
header('Location: tableaudebord.php');
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment