Advertisement
marongiuchristian93

Upload di file nel nostro sito

Jan 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1.     <form action="pagina.php" method="post" enctype="multipart/form-data">
  2.     <span style="color: #DA3A11;">Estensioni permesse:</span> JPG, JPEG, GIF, PNG, PSD, TXT, PDF, DOC, DOCX, MP3, MP4, AVI, ZIP, RAR<br><br>
  3.     <label for="file">Scegli il file: </label>
  4.     <input type="file" name="file" id="file">
  5.     <br>
  6.     <br>
  7.     <input type="submit" name="send" value="Invia File">
  8.     <br>
  9.     <br>   
  10.     <span style="color: #DA3A11;">Dimensione massima del file:</span> 3 KB
  11.     <br>
  12.     <br>
  13.     </form>
  14.     <!-- FINE UPLOAD FILE -->
  15. <?php
  16. if(isSet($_POST['send'])) { // Se richiedo l'upload del file
  17. // Estensioni permesse
  18. $allowedExts = array("gif", "jpeg", "jpg", "png", "txt", "pdf", "doc", "docx", "mp3", "avi", "mp4", "psd", "zip", "rar"); // Array di estensioni permesse
  19. $extension = end(explode(".", $_FILES['file']['name'])); // Prendo l'estensione del file caricato
  20. $max_fsize = 3500; // Limite massimo grandezza file impostata a 3 KB per le prove
  21. $errore = ""; // Imposto errore
  22. if($_FILES['file']['size'] > $max_fsize) {
  23.     // Se il file รจ troppo grande
  24.     $errore .= "<span style='color: #DA3A11;'>La dimensione del file deve essere inferiore a 3 KB</span><br>";
  25. }
  26. if(!in_array(strtolower($extension), $allowedExts)) {
  27.     // Se il file e' di una estensione non permessa
  28.     $errore = "<span style='color: #DA3A11;'>L'estensione del file (".$extension.") non e' consentita</span><br>";
  29.     if($extension == "php" || $extension == "asp" || $extension == "html" || $extension == "js" || $extension == "htaccess") $errore .= "<span style='color: #33CCFF;'>Estensione non permessa per la sicurezza del sito!</span><br>";
  30. }
  31. if($errore) {
  32.     // Se ho un errore specifico
  33.     echo "<span style='color: #FFFFFF;'>Errore:</span> " . $errore . "<br><br>";
  34. }
  35. else {
  36.     // Se non ci sono errori specifici
  37.     // Cerco errori generici di altro tipo
  38.     if($_FILES['file']['error'] > 0) {
  39.     echo "<span style='color: #FFFFFF;'>Codice errore:</span><span style='color: #DA3A11;'> " . $_FILES['file']['error'] . "</span><br>";
  40.     }
  41.     else {
  42.     // Se non ci sono nemmeno errori generici di altro tipo
  43.     // Inizio il caricamento del file
  44.             echo "<span style='color: #DA3A11;'>Caricamento del file:</span> " . $_FILES['file']['name'] . "<br>";
  45.             echo "<span style='color: #DA3A11;'>Tipo di file:</span> " . $_FILES['file']['type'] . "<br>";
  46.             echo "<span style='color: #DA3A11;'>Estensione:</span> " . $extension . "<br>";
  47.         echo "<span style='color: #DA3A11;'>Grandezza file:</span> " . floor($_FILES['file']['size'] / 1024) . " kB<br><br>";          
  48.         $numero = rand(10000, 9999999); // Numero da 10.000 a 9.999.999
  49.         $nome_assegnato = $numero . "." . $extension; // Imposto un nome da assegnare al file
  50.  
  51.         while(file_exists("" . $nome_assegnato)) {
  52.         // Se il file che sto salvando esiste gia'
  53.         // Genero un altro numero e provo con quello
  54.                 $numero = rand(10000, 99999); // Numero da 10.000 a 99.999
  55.             $nome_assegnato = $numero . "." . $extension;
  56.             }    
  57.             move_uploaded_file($_FILES['file']['tmp_name'], "art_files/articolo_10_upload_file/" . $nome_assegnato);
  58.             echo "<span style='color: #DA3A11; font-size: 16px;'>File salvato:</span> <a target='_blank' href='http://chrmar.altervista.org/articoli/art_files/articolo_10_upload_file/" . $nome_assegnato . "'>". $nome_assegnato . "</a><br><br>";      
  59.     }
  60. }
  61. } // Fine isSet
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement