PHP handling file uploads

      No Comments on PHP handling file uploads

PHP handling file uploads- most people know that the HTTP protocol is involved only in transfer of files from server to user’s browser. But HTTP protocol can also be used for transfer of files of any kind including PDFs, word documents, images.

PHP handling file uploads

A file uploading form must contain one or more input tags with attribute type having value equals to “file”. The code for a file uploading forms follows:

<form method="post" action="#" enctype="multipart/form-data"> 
  <h2>PHP handling file uploads</h2>
  <hr>
  <input type="text" placeholder="Enter user name" />
  <input type="file" name="userfile" />
  <input type="submit" value="upload"/>
</form>

enctype=”multipart/form-data” is necessary for file uploads.The output of the following code will be:
PHP handling file uploads
PHP handling file uploads

$_FILES array

$_FILES super-global stores various information about the file being uploaded to the server.

  • $_FILES[‘userfile’][‘error’]: returns information regarding the outcome of upload attempt.
  • $_FILES[‘userfile’][‘name’]: returns the original name of the file with extension.
  • $_FILES[‘userfile’][‘size’]: specifies the size of file, in bytes.
  • $_FILES[‘userfile’][‘tmp_name’]: specifies the temporary name assigned to file after it has been uploaded.
  • $_FILES[‘userfile’][‘type’]: specifices the MIME type of file

PHP’s file upload function

boolean is_uploaded_file(string filename)

This function is used to check whether the file name specified in the function has been uploaded.

boolean move_uploaded_file(string filename, string destination)

. This function is used to move the uploaded file from temporary location to final location.It ensures that the file denoted by the filename in input parameter was uploaded by HTTP.

Simple example:

<form method="post" action="#" enctype="multipart/form-data"> 
  <h2>PHP handling file uploads</h2>
  <hr>
  <input type="text" placeholder="Enter user name" />
  <input type="file" name="userfile" />
  <input type="submit" value="upload"/>
</form>

 <?php
// Set a constant

define ("FILESTORE","/uploads/12/131a/images/"); 

// Make sure that the file was POSTed.
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
 {
    // Was the file a PDF?
   if ($_FILES['userfile']['type'] != "application/pdf")
  { 
    echo "<p>File is in PDF format.</p>";
  }
  else
  {
     // Move uploaded file to final destination.
   $name = $_POST['name'];
   $result = move_uploaded_file($_FILES['userfile']['tmp_name'], FILESTORE.$_FILES['userfile']['name']);
     if ($result == 1) 
     echo "<p>File successfully uploaded.</p>";
     else echo "<p>There was a problem uploading the file.</p>";
  }
}
?>

Summary
PHP handling file uploads
Article Name
PHP handling file uploads
Description
PHP handling file uploads- most people know that the HTTP protocol is involved only in transfer of files from server to user's browser. But HTTP protocol can also be used for transfer of files of any kind including PDFs, word documents, images.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *