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


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