ฟังก์ชัน is_uploaded_file() ตรวจสอบว่าไฟล์ถูกอัปโหลดผ่าน HTTP POST หรือไม่ ฟังก์ชันจะคืนค่า TRUE หากไฟล์ถูกอัปโหลดผ่าน HTTP POST คืนค่า FALSE เมื่อล้มเหลว
ไวยากรณ์
is_uploaded_file(file_path)
พารามิเตอร์
-
file_path − ระบุไฟล์ที่จะตรวจสอบ
คืนสินค้า
ฟังก์ชัน is_uploaded_file() จะคืนค่า TRUE หากไฟล์ถูกอัปโหลดผ่าน HTTP POST คืนค่า 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!