PHP tutorial: upload files


Uploading a single and multiple files to server

In this page, you will learn to upload a single file and multiple files to web server. To upload a file to server in PHP, you need to use the move_uploadted_file($copy,$destination) built-in function. The $copy parameter is the copy of the orginal file. Generally, this copy can be accessed by writing the statement: $_FILES["file"]["tmp_name"]. The $destination parameter specifies file path to store the copy on the server. From the array of files, you also can access the file name by writing the statement: $_FILES["file"]["name"].

Upload a single file to server

To upload a file to server, you will need a file dialog to allow the user to select the file from your local machine. To display the file dialog, you need to add a file input field on your HTML form. You also need to specify the post value to method and multipart/form-data to the enctype attributes of the form. See the example below.

php-upload-file.php

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload a file</title>
</head>

<body>

<?php

$f=$_FILES["file"]["name"]; //get file name
$err=$_FILES["file"]["error"]; //get file error number
$temp=$_FILES["file"]["tmp_name"]; //get file copy
if(isset($f)){
   uploadFile($fs,$err,$temp);

}
function uploadFile($file,$err,$temp){
   global $status;
   if(file_exists($file))
       unlink($file); //override the file
   move_uploaded_file($temp,$file); //upload the file to the current directory

   if ($err> 0)
       $status.="<div style='color:#ff0011'>". $file." upload aborted</div>";

   else
      $status.="<div style='color:#0011ff'>".$file." uploaded</div>";

}

?>
<form action="php-upload-file.php" method="post" enctype="multipart/form-data" >
<table style="background:#dfdfdf">
<tr>
<th style="background: #399">Upload File</th>
</tr>
<tr>
<td>
<p>
<input name="file" id="file" type="file" />
</p></td>
</tr>
<tr>
<td style="text-align:right">
<input type="submit" name="btsubmit" id="btsubmit" value="Upload" />
</td>
</tr>
<tr>
<td>
<?php if(isset($status)) echo $status;?>
</td>
</tr>
</table>
</form>

</body>
</html>

upload a file

 

Upload multiple files to server

Uploading multiple files to server is similar to uploading a single file. However, you need to specify that the input file is an array by writing the following HTML code: <input name="file[]" multiple id="file" type="file" />. This statement will allow the user to choose multiple files from his/her local machine. In php code, you will use a foreach loop to read every file in the $_FILES array and upload it one by one to the server.

php-upload-files.php

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload files</title>
</head>

<body>

<?php

$fs=$_FILES["file"]["name"];
$errs=$_FILES["file"]["error"];
$temps=$_FILES["file"]["tmp_name"];

if(isset($fs)){

   uploadFiles($fs,$errs,$temps);
}

function uploadFiles($files,$errors,$temps){
   $count=0;
   global $status;
   foreach ($files as $file) {
      if(file_exists($file))
         unlink($file); //override the file
      move_uploaded_file($temps[$count],$file); //upload the file to the current directory

   if ($errors[$count]> 0)
      $status.="<div style='color:#ff0011'>". $file." upload aborted</div>";

   else
      $status.="<div style='color:#0011ff'>".$file." uploaded</div>";

   $count+=1;
   }
}

?>
<form action="php-upload-files.php" method="post" enctype="multipart/form-data" >
<table style="background:#dfdfdf">
<tr>
<th style="background: #399">Upload File</th>
</tr>
<tr>
<td>
<input name="file[]" multiple id="file" type="file" />

</td>
</tr>
<tr>
<td style="text-align:right">
<input type="submit" name="btsubmit" id="btsubmit" value="Upload" />
</td>
</tr>
<tr>
<td>
<?php if(isset($status)) echo $status;?>
</td>
</tr>
</table>
</form>

</body>
</html>

upload files



Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.