PHP File Upload

In php you can upload file to server. To upload a file to the server, you need to create a form to specify which file you want to upload.


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

  • The action attribute points to a .php file. This is the file that will process the uploaded file.
  • There is an attribute called enctype, and its value is multipart/form-data.
  • One of the input fields has type="file".
Once you press submit button the file is uploaded into a temporary directory on the server. than you need to move the file from temporary directory to another location where you want to store and get file information, and when a file is uploaded, you can find out certain information about the file including its name, type, size, as well as the name of the temporary file on the server. These details are made available to you via a PHP global array called $_FILES.


<?php
if ($_FILES["file"]["error"] == 0) {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temporary filename: " . $_FILES["file"]["tmp_name"];
  } 
  else {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
?>
Submit an Idea
  Thank you for your valuable feedback.
Your E-mail (optional):
Select Option:
Description:
 
Feedback
Your Name / email address
Feedback as
Your feedback
close
Quickly sign in with:

Create a FATALWEB account here:

First name:

Last name:

E-mail address:

Join Fatalweb

Already have a fatalweb account? Log in here