當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。