Notice: Constant WP_MEMORY_LIMIT already defined in /home/javawith/public_html/codesjava.com/wp-config.php on line 107

Notice: Constant WP_MAX_MEMORY_LIMIT already defined in /home/javawith/public_html/codesjava.com/wp-config.php on line 108
PHP File Upload - CodesJava

CodesJava

Easy learning with example program codes

PHP File Upload


Notice: Undefined variable: footnotelist in /home/javawith/public_html/codesjava.com/wp-content/plugins/latex2html/inc/core.php on line 475

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.
Please follow and like us:
error
Posted in PHP   








Copyright © 2019 CodesJava DMCA.com Protection Status