PHP 中的 is_uploaded_file() 函数
phpprogrammingserver side programming更新于 2025/7/9 12:22:17
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
// 检查文件是否通过 HTTP POST 上传
if (is_uploaded_file($_FILES['userfile']['new.txt'])) {
echo "File ". $_FILES['userfile']['new.txt'] ." 上传成功!
";
// 显示上传文件的内容
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!
";
}
?>
输出
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!

