PHP- File Upload Example

//File upload client page


<form action="test.php" method="post" enctype="multipart/form-data">
Filename:<input type="file" name="file" />
<input type="file" name="image[]" id="image" multiple>


<br />
<input type="submit" name="submit" value="upload" />
</form>
</body>
</html>


output
NOTE:
"multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded.

//    1 upload_file.php
//   $_FILES array is used to upload files from a client computer to the remote server.
<?php
if ($_FILES["file"]["error"] > 0)  //Error
 {
 echo "Error: " . $_FILES["file"]["error"] . "<br />";
 }
else
 {
 echo "Upload: " . $_FILES["file"]["name"] . "<br />";
 echo "Type: " . $_FILES["file"]["type"] . "<br />";
 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";// change in upload_max_filesize = 2M defailt is 2mb
 echo "Stored in: " . $_FILES["file"]["tmp_name"];
 }


?>
OUTPUT
  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload


//  NOTE:The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:
move_uploaded_file($_FILES["file"]["tmp_name"],"d:/upload/". $_FILES["file"]["name"] );


Assignment:-


Reactions: