Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

PHP $_FILES

Giới thiệu

Biến toàn cục $_FILES là mảng liên kết chứa mục tải lên qua HTTP POST. Tải file cần form POST với enctype multipart/form-data.

$HTTP_POST_FILES cũng chứa thông tin tương tự nhưng không phải siêu toàn cục, đã lỗi thời.

Mảng _FILES chứa các thuộc tính:

  • $_FILES['file']['name'] - Tên gốc file tải lên.
  • $_FILES['file']['type'] - Loại MIME file.
  • $_FILES['file']['size'] - Kích thước file (byte).
  • $_FILES['file']['tmp_name'] - Tên file tạm trên server.
  • $_FILES['file']['error'] - Mã lỗi tải file.

File test.html chứa form enctype multipart/form-data và input file:

<form action="testscript.php" method="POST" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="submit"> </form>

Script PHP:

Ví dụ

<?php echo "Filename: " . $_FILES['file']['name'] . "<br>"; echo "Type : " . $_FILES['file']['type'] . "<br>"; echo "Size : " . $_FILES['file']['size'] . "<br>"; echo "Temp name: " . $_FILES['file']['tmp_name'] . "<br>"; echo "Error : " . $_FILES['file']['error'] . "<br>"; ?>

Kết quả

Filename: hello.html Type : text/html Size : 56 Temp name: C:\xampp\tmp\php32CE.tmp Error : 0