CodesJava

Easy learning with example program codes

PHP File Upload


Uploading files to a server is a simple task in PHP. PHP facilitates some unique features of uploading a file to a server:

  • Both single and multiple files can be uploaded easily in PHP.
  • Both binary and text files can be uploaded in PHP.
  • PHP provides various other functions and methods to check and to control the features associated with file uploading, like knowing a file size and its type, or to move the uploaded file to a new location.

php.ini File:

To allow file uploads in PHP, the php.ini file must be configured such that the file_uploads directive is set ON inside the php.ini file.

PHP $_FILES:

PHP provides a superglobal variable, $_FILES which contains all the informations about a file, including a file name, its type, size, temp name and any errors present in it.

Uses of $_FILES:

SYNTAX USES
$_FILES[‘filename’][‘name’] $_FILES is used to return the name of a file.
$_FILES[‘filename’][‘type’] $_FILES is used to return the MIME type of a file.
$_FILES[‘filename’][‘size’] $_FILES is used to return the bytes size of a file.
$_FILES[‘filename’][‘tmp_name’] $_FILES is used to return the temporary file name stored on the server.
$_FILES[‘filename’][‘error’] $_FILES is used to return the error code associated with a file.

PHP move_uploaded_file() Function:

PHP move_uploaded_file() function is used to move an uploaded file to a new destination.

Syntax:

move_uploaded_file($filename,$newloc)

Features of PHP move_uploaded_file() Function:

  • PHP move_uploaded_file() function moves the files uploaded via HTTP POST only.
  • PHP move_uploaded_file() function creates a new file at the destination or overwrites the existing destination file.
  • PHP move_uploaded_file() function returns a TRUE value, if the file is moved successfully and returns a FALSE value, if the process of moving the uploaded file to a new destination fails.

Example:

<!DOCTYPE html>
<html>
<body>
 
<?php  
 
$target_dir = "php_examples/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
{
echo "The file has been successfully uploaded.";
} 
else
{
echo "File not uploaded. Something is Wrong";
}
}
?>
 
</body>
</html>

Output

The file has been successfully uploaded.
Posted in PHP   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki