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
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>Upload an Image</title>
<style type="text/css" title="text/css" media="all">
.error {
font-weight: bold;
color: #A00
}
</style>
</head>
<body>
<?php #Script upload_image.php
// Vérifier si le formulaire a été envoyé
if(isset($_POST['submitted'])) {
// Vérifier s'il y a un fichier téléchargé
if(isset($_FILES['upload'])) {
// Valider le type JPEG ou PNG uniquement
$allowed=array('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png');
if(in_array($_FILES['upload']['type'],$allowed)) {
// Déplacer le fichier
if(move_uploaded_file($_FILES['upload']['tmp_name'], "../upload/{$_FILES['upload']['name']}")) {
echo '<p><em>The file has been uploaded.</em></p>';
} // Fin de if move
} else { // Type invalide
echo '<p class="error">Please upload a JPEG or PNG Image.</p>';
}
} // Fin de l'instruction if isset($_FILES['upload'])
// Vérifier s'il y a une erreur
if($_FILES['upload']['error']>0) {
echo '<p class="error">The file could not be uploaded because: <strong>';
// Imprimer un message en fonction de l'erreur
switch($_FILES ['upload']['error']) {
case 1:
print 'The File exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk';
break;
case 8:
print 'File uploaded stopped';
break;
default:
print 'A system error occured';
break;
} // Fin de l'instruction switch
print '</strong></p>';
} // Fin de l'instruction if pour les erreurs
// Supprimer le fichier s'il existe toujours
if(file_exists($_FILES['upload']['tmp_name'])&&is_file($_FILES['upload']['tmp_name']))
{
unlink($_FILES['upload']['tmp_name']);
}
} // Fin de l'instruction conditionnelle d'envoi
?>
<form enctype="multipart/form-data" action="upload_image.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<fieldset><legend>Select any JPEG or PNG image of 1MB or smaller to be uploaded : </legend>
<p><b>File :</b><input type="file" name="upload"></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit"></div>
<input type="hidden" name="submitted" value="TRUE">
</form>
</body>
</html> |