PHP | is_uploaded_file( ) Function

The is_uploaded_file() function in PHP is an inbuilt function which is used to check whether the specified file uploaded via HTTP POST or not. The name of the file is sent as a parameter to the is_uploaded_file() function and it returns True if the file is uploaded via HTTP POST. This function can be used to ensure that a malicious user hasn’t tried to trick the script into working on files upon which it should not be working.

Syntax:

bool is_uploaded_file($file)

Parameters Used: This function accepts single parameter $file.



  • $file: It is a mandatory parameter which specifies the file.

Return Value: It returns True if the $file uploaded via HTTP POST. It returns true on success or false in failure. For proper working, the function is_uploaded_file() needs an argument like $_FILES[‘userfile’][‘tmp_name’], – the name of the uploaded file on the clients machine $_FILES[‘userfile’][‘name’] does not work.

Exceptions

  • An E_WARNING is emitted on failure.
  • The result of this function are cached and therefore the clearstatcache() function is used to clear the cache.
  • is_uploaded_file() function returns false for non-existent files.

Below programs illustrate the is_uploaded_file() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate is_uploaded_file() function.
$myfile = "gfg.txt";
  
// checking whether the file is uploaded via HTTP POST
if (is_uploaded_file($file))
    echo ("$file is uploaded via HTTP POST");
else
    echo ("$file is not uploaded via HTTP POST");
?>

chevron_right


Output:

gfg.txt is not uploaded via HTTP POST

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// checking whether the file is uploaded via HTTP POST
if (is_uploaded_file($_FILES['userfile']['gfg.txt'])) 
{
    echo "File ". $_FILES['userfile']['gfg.txt'] .
                      " uploaded successfully.\n";
                        
    // displaying contents of the uploaded file
    echo "Contents of the file are :\n";
    readfile($_FILES['userfile']['gfg.txt']);
else
{
    echo "File ". $_FILES['userfile']['gfg.txt'] .
                  " not uploaded successfully.\n";
}
?>

chevron_right


Output:

File gfg.txt uploaded successfully.
Contents of the file are :
Portal for geeks!

Reference:
http://php.net/manual/en/function.is-uploaded-file.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.