Caricamento dei file PHP

With PHP, you can upload files to the server.

Create a file upload form

Allowing users to upload files from the form is very useful.

Please see the following HTML form for uploading files:

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Please note the following information about this form:

The enctype attribute of the <form> tag specifies which content type should be used when submitting the form. Use "multipart/form-data" when the form needs binary data, such as file content.

The type="file" attribute of the <input> tag specifies that the input should be treated as a file. For example, when previewing in the browser, you will see a browse button next to the input box.

Commento:Allowing users to upload files is a huge security risk. Please allow only trusted users to perform file upload operations.

Create an upload script

"upload_file.php" file contains the code for uploading files:

<?php
if ($_FILES[\"file\"][\"error\"] > 0)
  {
  echo "Errore: " . $_FILES[\"file\"][\"error\"] . "<br />";
  }
else
  {
  echo "Caricamento: " . $_FILES["file"]["name"] . "<br />";
  echo "Tipo: " . $_FILES["file"]["type"] . "<br />";
  echo "Dimensione: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Salvato in: " . $_FILES[\"file\"][\"tmp_name\"];
  }
?>

Utilizzando l'array globale $_FILES di PHP, puoi caricare file da un computer client a un server remoto.

Il primo parametro è il nome del campo input del modulo, il secondo indice può essere "name", "type", "size", "tmp_name" o "error". Ecco come fare:

  • $_FILES[\"file\"][\"name\"] - Nome del file caricato
  • $_FILES[\"file\"][\"type\"] - Tipo del file caricato
  • $_FILES[\"file\"][\"size\"] - Dimensione del file caricato, in byte
  • $_FILES[\"file\"][\"tmp_name\"] - Nome della copia temporanea del file memorizzata sul server
  • $_FILES[\"file\"][\"error\"] - Codice di errore causato dal caricamento del file

Questo è un modo molto semplice per caricare file. Per motivi di sicurezza, dovresti aggiungere limitazioni su chi ha il permesso di caricare file.

Limitazioni di caricamento

In questo script, abbiamo aggiunto limitazioni per il caricamento dei file. Gli utenti possono caricare solo file .gif o .jpeg, la dimensione dei file deve essere inferiore a 20 kb:

<?php
if ((($_FILES[\"file\"][\"type\"] == "image/gif"))
|| ($_FILES[\"file\"][\"type\"] == "image/jpeg")
|| ($_FILES[\"file\"][\"type\"] == "image/pjpeg"))
&& ($_FILES[\"file\"][\"size\"] < 20000))
  {
  if ($_FILES[\"file\"][\"error\"] > 0)
    {
    echo "Errore: " . $_FILES[\"file\"][\"error\"] . "<br />";
    }
  else
    {
    echo "Caricamento: " . $_FILES["file"]["name"] . "<br />";
    echo "Tipo: " . $_FILES["file"]["type"] . "<br />";
    echo "Dimensione: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Salvato in: " . $_FILES[\"file\"][\"tmp_name\"];
    }
  }
else
  {
  echo "File non valido";
  }
?>

Commento:Per IE, il tipo di file jpg deve essere pjpeg, per Firefox deve essere jpeg.

Salvare il file caricato

L'esempio sopra ha creato una copia temporanea del file caricato nel cartella temporanea PHP del server.

Questa copia temporanea del file scompare alla fine dello script. Per salvare il file caricato, dobbiamo copiarlo in un'altra posizione:

<?php
if ((($_FILES[\"file\"][\"type\"] == "image/gif"))
|| ($_FILES[\"file\"][\"type\"] == "image/jpeg")
|| ($_FILES[\"file\"][\"type\"] == "image/pjpeg"))
&& ($_FILES[\"file\"][\"size\"] < 20000))
  {
  if ($_FILES[\"file\"][\"error\"] > 0)
    {
    echo "Codice di ritorno: " . $_FILES[\"file\"][\"error\"] . "<br />";
    }
  else
    {
    echo "Caricamento: " . $_FILES["file"]["name"] . "<br />";
    echo "Tipo: " . $_FILES["file"]["type"] . "<br />";
    echo "Dimensione: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "File temporaneo: " . $_FILES["file"]["tmp_name"] . "<br />";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Salvato in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "File non valido";
  }
?>

Il脚本 sopra ha verificato se il file esiste già, se non esiste, il file viene copiato nella cartella specificata.

Commento:Questo esempio ha salvato il file in una nuova cartella chiamata "upload".