is_uploaded_file() function in PHP


The is_uploaded_file() function checks whether a file was uploaded via HTTP POST. The function returns TRUE if the file is uploaded via HTTP POST. It returns FALSE on failure.

Syntax

is_uploaded_file(file_path)

Parameters

  • file_path − Specify the file to be checked.

Return

The is_uploaded_file() function returns TRUE if the file is uploaded via HTTP POST. It returns FALSE on failure.

Let’s say we are uploading a file “new.txt” with the following content.

This is demo text!

Example

<?php
   // checking for file is uploaded via HTTP POST
   if (is_uploaded_file($_FILES['userfile'][‘new.txt'])) {
      echo "File ". $_FILES['userfile'][‘new.txt'] ." uploaded successfully!
";       // displaying contents of the uploaded file       echo "Reading Contents of the file:
";       readfile($_FILES['userfile'][‘new.txt']);    } else {       echo "File ". $_FILES['userfile'][‘new.txt'] ." failed in uploading! File upload attack could       be the reason!
";    } ?>

Output

File new.txt uploaded successfully!
Reading Contents of the file:
This is demo text!

Let us see another example with file “details.txt”.

Example

 Live Demo

<?php
$file = "newdetailstxt";
if(is_uploaded_file($file)) {
   echo ("Uploaded via HTTP POST");
} else {
   echo ("Not uploaded via HTTP POST");
}
?>

Output

Not uploaded via HTTP POST!

Updated on: 24-Jun-2020

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements