/gist:d3c4d9dc096a464c374e Secret
Created
July 5, 2010 20:22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Uploader</title> | |
<script type="text/javascript"> | |
var addInputLink, | |
currentFileNumber = 1; | |
window.onload = function() { | |
/* Lien pour ajouter un fichier */ | |
addInputLink = document.getElementById('addInput'); | |
addInputLink.onclick = function() { | |
currentFileNumber++; | |
var p = document.createElement('p'); | |
var label = document.createElement('label'); | |
label.setAttribute('for','fichier'+currentFileNumber); | |
label.innerHTML = 'Fichier '+currentFileNumber+' : '; | |
var input = document.createElement('input'); | |
input.type = 'file'; | |
input.name = 'fichiers[]'; | |
input.id = 'fichier'+currentFileNumber; | |
p.appendChild(label); | |
p.appendChild(input); | |
document.forms[0].appendChild(p); | |
return false; | |
}; | |
}; | |
</script> | |
</head> | |
<body> | |
<?php | |
/* Types MIME autorisés et extensions */ | |
$allowedTypes = array('image/jpeg','image/jpg','image/png','image/gif'); | |
$allowedExtensions = array('jpeg', 'jpg', 'gif', 'png', 'bmp'); | |
/* Configuration des champs */ | |
define('UPFORM_FILEINPUT', 'fichiers'); | |
define('UPFORM_MAILINPUT', 'email'); | |
/* Configuration pour les vérif. et l'enregistrement */ | |
define('UPFORM_MAXSIZE', 3145728); | |
define('UPFORM_PATH', 'up/'); | |
define('UPFORM_WEBROOT', 'http://localhost/ccm/'); | |
/* Configuration des emails */ | |
define('UPFORM_SENDMAIL', false); | |
define('UPFORM_ADMINMAIL', 'postmaster@localhost'); | |
define('UPFORM_TOPMAIL', 'Bonjour, '."\n" | |
. 'Voici le(s) lien(s) vers le(s) fichier(s) envoyé(s) :'."\n\n\n"); | |
define('UPFORM_BOTTOMMAIL', "\n".'Cordialement, Example.com'); | |
define('UPFORM_MAILSUBJECT', '[Example.com] Vos fichiers envoyés'); | |
$mailContent = ''; | |
if(isset($_FILES['fichiers']) && isset($_POST[UPFORM_MAILINPUT]) && preg_match('#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#i', $_POST[UPFORM_MAILINPUT])) { | |
$nb = 0; | |
$limit = 1; | |
$savedFiles = 0; | |
foreach($_FILES['fichiers']['error'] as $fichier) { | |
if($limit <= 5) { | |
echo '<h4>Fichier '.$limit.'</h4>'; | |
$uniqname = uniqid(md5($_SERVER['REQUEST_TIME']).'_'); | |
$fileDirectoryData = pathinfo($_FILES['fichiers']['name'][$nb]); | |
$extension = strtolower($fileDirectoryData['extension']); | |
$uniqname .= '.'.$extension; | |
if(in_array($_FILES['fichiers']['type'][$nb],$allowedTypes) && $_FILES['fichiers']['error'][$nb] === UPLOAD_ERR_OK && in_array($extension, $allowedExtensions)) { | |
if($_FILES['fichiers']['size'][$nb] <= UPFORM_MAXSIZE) { | |
move_uploaded_file($_FILES['fichiers']['tmp_name'][$nb], UPFORM_PATH.$uniqname); | |
$savedFiles++; | |
$url = UPFORM_WEBROOT.UPFORM_PATH.$uniqname; | |
/* Code HTML */ | |
echo 'Code HTML :<br/>' | |
. '<pre><a href="'.$url.'">'."\n" | |
. "\t".'<img src="'.$url.'" alt="Image postée sur Example.com" />'."\n" | |
. '</a></pre><br/><br/>'; | |
/* Code BBcode */ | |
echo 'Code BBcode :<br/>' | |
. '<pre>[url='.$url.'][img]'.$url.'[/img][/url]</pre><br/><br/>'; | |
/* Lien vers le fichier */ | |
echo 'Lien vers le fichier :<br/>' | |
. '<a href="'.$url.'"><img src="'.$url.'" alt="Image" style="max-width:75px;" /></a>'; | |
$mailContent .= $url."\n"; | |
} else { | |
echo 'Le fichier fait plus de '.UPFORM_MAXSIZE.' octets'; | |
} | |
} else { | |
echo 'Le type de fichier n\'est pas autorisé ou une erreur s\'est produite pendant l\'envoie.'; | |
} | |
echo '<hr />'; | |
$limit++; | |
$nb++; | |
} | |
} | |
if($savedFiles > 0 && UPFORM_SENDMAIL) { | |
mail($_POST[UPFORM_MAILINPUT], UPFORM_MAILSUBJECT, UPFORM_TOPMAIL.$mailContent.UPFORM_BOTTOMMAIL, 'From: '.UPFORM_ADMINMAIL); | |
} | |
} | |
?> | |
<form method="post" action="" enctype="multipart/form-data"> | |
<p> | |
<a href="#" id="addInput">Ajouter un fichier</a><br/> | |
Seuls les fichiers JPG, JPEG, GIF et PNG sont acceptés. La limite est de <?php echo UPFORM_MAXSIZE; ?> octets par fichier et un total de 5 fichiers.<br/> | |
<label for="email">Votre adresse email : </label><input type="text" name="email" id="email" /> | |
<input type="submit" value="Enregistrer" /><br/> | |
</p> | |
<p> | |
<label for="fichier1">Fichier 1 :</label> | |
<input type="file" name="fichiers[]" id="fichier1" /> | |
</p> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment