当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP is_uploaded_file( )用法及代码示例


PHP中的is_uploaded_file()函数是一个内置函数,用于检查指定文件是否通过HTTP POST上传。文件名作为参数发送到is_uploaded_file()函数,如果文件通过HTTP POST上传,则返回True。此函数可用于确保恶意用户没有试图欺骗脚本来处理不应在其上运行的文件。

用法:

bool is_uploaded_file($file)

使用的参数:该函数接受单个参数$file。


  • $file: 它是指定文件的必需参数。

返回值:如果$file通过HTTP POST上传,则返回True。成功返回true,失败返回false。为了正常工作,函数is_uploaded_file()需要一个$_FILES ['userfile'] ['tmp_name']之类的参数,-客户端计算机$_FILES ['userfile'] ['name']上载文件的名称无效。

异常

  • 失败时发出E_WARNING。
  • 此函数的结果被缓存,因此使用clearstatcache()函数清除缓存。
  • is_uploaded_file()函数为不存在的文件返回false。

以下示例程序旨在说明is_uploaded_file()函数。

程序1:

<?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"); 
?>

输出:

gfg.txt is not uploaded via HTTP POST

程序2:

<?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"; 
} 
?>

输出:

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

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



相关用法


注:本文由纯净天空筛选整理自Shubrodeep Banerjee大神的英文原创作品 PHP | is_uploaded_file() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。