Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. upload.php
  2.  
  3. <html>
  4. <head>
  5. <title>Upload page</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  7. <link rel="stylesheet" href="style.css" type="text/css">
  8. </head>
  9. <body>
  10. <?php
  11. include("uploader.php");
  12. ?>
  13. </body>
  14. </html>
  15. ______________________________________________________________________________________________
  16.  
  17. uloader.php
  18.  
  19. <?php
  20. // ############################################################ //
  21. // Récupération du dossier dans lequel le fichier sera uploadé //
  22. $DESTINATION_FOLDER = $_POST["folder"]; //
  23. // Taille maximale de fichier, valeur en bytes //
  24. $MAX_SIZE = 5000000; //
  25. // Récupération de l'url de retour //
  26. $RETURN_LINK = $_SERVER['HTTP_REFERER']; //
  27. // Définition des extensions de fichier autorisées (avec le ".")//
  28. $AUTH_EXT = array(".doc", ".pdf", ".jpg", ".ppt", ".xls", //
  29. ".bmp", ".gif"); //
  30. // ############################################################ //
  31.  
  32. // Fonction permettant de créer un lien de retour automatique
  33.  
  34. function createReturnLink(){
  35. global $RETURN_LINK;
  36. echo "<a href='".$RETURN_LINK."'>Retour</a><br>";
  37. }
  38.  
  39. // Fonction permettant de vérifier si l'extension du fichier est
  40. // autorisée.
  41.  
  42. function isExtAuthorized($ext){
  43. global $AUTH_EXT;
  44. if(in_array($ext, $AUTH_EXT)){
  45. return true;
  46. }else{
  47. return false;
  48. }
  49. }
  50.  
  51. // On vérifie que le champs contenant le chemin du fichier soit
  52. // bien rempli.
  53.  
  54. if(!empty($_FILES["file"]["name"])){
  55.  
  56. // Nom du fichier choisi:
  57. $nomFichier = $_FILES["file"]["name"] ;
  58. // Nom temporaire sur le serveur:
  59. $nomTemporaire = $_FILES["file"]["tmp_name"] ;
  60. // Type du fichier choisi:
  61. $typeFichier = $_FILES["file"]["type"] ;
  62. // Poids en octets du fichier choisit:
  63. $poidsFichier = $_FILES["file"]["size"] ;
  64. // Code de l'erreur si jamais il y en a une:
  65. $codeErreur = $_FILES["file"]["error"] ;
  66. // Extension du fichier
  67. $extension = strrchr($nomFichier, ".");
  68.  
  69. // Si le poids du fichier est de 0 bytes, le fichier est
  70. // invalide (ou le chemin incorrect) => message d'erreur
  71. // sinon, le script continue.
  72. if($poidsFichier <> 0){
  73. // Si la taille du fichier est supérieure à la taille
  74. // maximum spécifiée => message d'erreur
  75. if($poidsFichier < $MAX_SIZE){
  76. // On teste ensuite si le fichier a une extension autorisée
  77. if(isExtAuthorized($extension)){
  78. // Ensuite, on copie le fichier uploadé ou bon nous semble.
  79. $uploadOk = move_uploaded_file($nomTemporaire, $DESTINATION_FOLDER.$nomFichier);
  80. if($uploadOk){
  81. echo("L'upload a réussi !<br><br>");
  82. echo(createReturnLink());
  83. }else{
  84. echo("L'upload a échoué !<br><br>");
  85. echo(createReturnLink());
  86. }
  87. }else{
  88. echo ("Les fichiers avec l'extension $extension ne peuvent pas être uploadés !<br>");
  89. echo (createReturnLink()."<br>");
  90. }
  91. }else{
  92. $tailleKo = $MAX_SIZE / 1000;
  93. echo("Vous ne pouvez pas uploader de fichiers dont la taille est supérieure à : $tailleKo Ko.<br>");
  94. echo (createReturnLink()."<br>");
  95. }
  96. }else{
  97. echo("Le fichier choisi est invalide !<br>");
  98. echo (createReturnLink()."<br>");
  99. }
  100. }else{
  101. echo("Vous n'avez pas choisi de fichier !<br>");
  102. echo (createReturnLink()."<br>");
  103. }
  104. ?>
  105. ______________________________________________________________________________________________
  106.  
  107. choix_fichier.php
  108.  
  109. <html>
  110. <head>
  111. <title>Upload de fichiers</title>
  112. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  113. <link rel="stylesheet" href="style.css" type="text/css">
  114.  
  115. </head>
  116. <body>
  117. <h3>Uploader des fichiers</h3>
  118. <br>
  119. <br>
  120. <form name="upload" enctype="multipart/form-data" method="post" action="upload.php">
  121. <input type="file" name="file">
  122. <br><br>
  123. Choisir le dossier de destination :<br>
  124. <select name="folder">
  125. <option value="folder01/">Dossier 1</option>
  126. <option value="folder02/">Dossier 2</option>
  127. <option value="folder03/">Dossier 3</option>
  128. <option value="folder04/">Dossier 4</option>
  129. </select>
  130. <br><br>
  131. <input type="submit" name="bouton_submit" value="Envoyer le fichier">
  132. </form>
  133. <br>
  134. <br>
  135. </body>
  136. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement