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


PHP is_uploaded_file()用法及代码示例


is_uploaded_file() 函数检查文件是否通过 HTTP POST 上传。如果文件是通过 HTTP POST 上传的,则该函数返回 TRUE。失败时返回 FALSE。

用法

is_uploaded_file(file_path)

参数

  • file_path −指定要检查的文件。

返回

如果文件是通过 HTTP POST 上传的,则 is_uploaded_file() 函数返回 TRUE。失败时返回 FALSE。

假设我们正在上传包含以下内容的文件 “new.txt”。

This is demo text!

示例

<?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!\n";
      // displaying contents of the uploaded file
      echo "Reading Contents of the file:\n";
      readfile($_FILES['userfile'][‘new.txt']);
   } else {
      echo "File ". $_FILES['userfile'][‘new.txt'] ." failed in uploading! File upload attack could       be the reason!\n";
   }
?>

输出

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

让我们看另一个例子,文件 “details.txt”。

示例

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

输出

Not uploaded via HTTP POST!

相关用法


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