Computer >> Máy Tính >  >> Lập trình >> PHP

hàm is_uploaded_file () trong PHP

Hàm is_uploaded_file () kiểm tra xem tệp có được tải lên qua HTTP POST hay không. Hàm trả về TRUE nếu tệp được tải lên qua HTTP POST. Nó trả về FALSE khi không thành công.

Cú pháp

is_uploaded_file(file_path)

Tham số

  • file_path - Chỉ định tệp sẽ được kiểm tra.

Quay lại

Hàm is_uploaded_file () trả về TRUE nếu tệp được tải lên qua HTTP POST. Nó trả về FALSE khi không thành công.

Giả sử chúng tôi đang tải lên tệp “new.txt” có nội dung sau.

This is demo text!

Ví dụ

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

Đầu ra

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

Hãy để chúng tôi xem một ví dụ khác với tệp “details.txt”.

Ví dụ

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

Đầu ra

Not uploaded via HTTP POST!