Hướng dẫn is uploaded file php? - được tải lên tập tin php?

  • Tổng quan về kỹ thuật Upload file PHP
  • Tạo form HTML file
  • Code Php xử lý upload
  • Code Php xử lý upload nhiều file

Tổng quan về kỹ thuật Upload file trong PHP

Để HTML FORM có thể gửi file lên server (http server) thì form phải có thuộc tính enctype="multipart/form-data", phần tử trong form để chọn và gửi file có dạng (tức là dùng phần tử input với thuộc tính type="file"). Cấu trúc tóm tắt là: enctype="multipart/form-data", phần tử trong form để chọn và gửi file có dạng (tức là dùng phần tử input với thuộc tính type="file"). Cấu trúc tóm tắt là:

Khi file Upload lên Server (chạy PHP) thì file sẽ lưu vào một thư mục tạm (temporary directory, như trên Linux đó là /tmp) của hệ thống PHP, và toàn bộ thông tin về file upload sẽ lưu trong biến mảng toàn cục

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
0

Chúng ta cần đọc thông tin từ

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
0 để nhanh chóng di chuyển file được upload vào thư mục tạm vào một nơi lưu trữ lâu dài (nếu không xử lý gì, thì sau một khoảng thời gian file này bị xóa).

Mảng biến

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
0 khi var_dump có cấu trúc dạng như sau:

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18

Các thành phần đó là:

  • myfile : chỉ số mảng tương ứng với tên phần tử input, upload file. : chỉ số mảng tương ứng với tên phần tử input, upload file.
  • name : tên gốc (ban đầu) của file. : tên gốc (ban đầu) của file.
  • type : kiểu file (tùy phần mở rộng có thể là
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    3,
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    4,
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    5 ...)
    : kiểu file (tùy phần mở rộng có thể là
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    3,
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    4,
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    5 ...)
  • tmp_name : nơi lưu tạm file upload lên, nếu muốn di chuyển nó ra khỏi thư mục tạm dùng hàm
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    6.
    : nơi lưu tạm file upload lên, nếu muốn di chuyển nó ra khỏi thư mục tạm dùng hàm
    array (size=1)
        'myfile' =>
            array (size=5)
                'name' => string 'somefile.txt' (length=12)
                'type' => string 'text/plain' (length=10)
                'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
                'error' => int 0
                'size' => int 18
    
    6.
  • error : mã lỗi, nếu mã này bằng 0 là không lỗi. : mã lỗi, nếu mã này bằng 0 là không lỗi.
  • size : cỡ file (byte). : cỡ file (byte).

Bằng việc đọc

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
0 bạn sẽ biết các thông tin về file được upload, nơi file đang lưu tạm, bạn cần phải xử lý bằng các hàm PHP để di chuyển file ra một vị trí theo cấu trúc thư mục của ứng dụng.

Nếu sau khi upload vào thư mục tạm một thời gian (như trên là

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
8) một thời gian (do cấu hình hệ thống) mà bạn không di chuyển nó đến vị trí thích hợp, nó sẽ tự xóa.

Tạo form HTML upload file

Để thực hành upload file trong PHP, trước tiên tạo

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
9 cho phép người dùng chọn file và gửi file đó lên server. Giả sử code PHP để xử lý upload các file hình ảnh nằm ở
Chọn file để upload:
0 thì bạn có thể xây dựng
array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
9 như sau:

Có thể tạo file

Chọn file để upload:
2

Chọn file để upload:

Hiện thị

array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
9 có dạng:

Lưu ý khi tạo form để uplad ảnh (file) thì cần thiết đặt thuộc tính form có:

Chọn file để upload:
4 Phần tử chọn file trong form là input với kiểu type="file". Tên phần tử là
Chọn file để upload:
7
Chọn file để upload:
4
Phần tử chọn file trong form là input với kiểu type="file".
Tên phần tử là
Chọn file để upload:
7

Code PHP xử lý Upload File

Code xử lý upload sau là xử lý chỉ cho upload các file ảnh, với các loại file là jpg, png, jpeg, gif. File Upload file có cỡ < 0.8 MB. File Upload sẽ lưu vào thư mục

Chọn file để upload:
8

Code xử lý upload file để trong file

Chọn file để upload:
9 có nội dung như sau:

 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>

Giải thích code trên:

Các

 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
0 mà
array (size=1)
    'myfile' =>
        array (size=5)
            'name' => string 'somefile.txt' (length=12)
            'type' => string 'text/plain' (length=10)
            'tmp_name' => string '/tmp/phpDC66.tmp' (length=16)
            'error' => int 0
            'size' => int 18
9 HTML upload nên, được PHP lưu ở một thư mục tạm hệ thống, và tất cả các thông tin về file được lưu ở biến mảng
 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
2, cấu trúc biến này như sau:

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )

 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
3 tên key tương ứng với tên phần tử form upload file, ví dụ trên là
Chọn file để upload:
7

Như vậy khi xủ lý upload file, bạn truy cập vào $_FILE và di chuyển file vừa upload từ

 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
5 sang vị trí mới bạn muốn lưu
 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
6 bằng hàm
 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
7

Kiểm tra file upload có phải là file ảnh (dùng cho trường hợp bạn chỉ cho phép up ảnh) thì đầu tiên gọi

 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
8 để xem kích thước ảnh, nếu hàm phân tích được có nghĩa là ảnh, ngược lại là một loại file khác.

Kiểm tra phần mở rộng của tên file: Lấy phần mở rộng

 $maxfilesize)
  {
      echo "Không được upload ảnh lớn hơn $maxfilesize (bytes).";
      $allowUpload = false;
  }


  // Kiểm tra kiểu file
  if (!in_array($imageFileType,$allowtypes ))
  {
      echo "Chỉ được upload các định dạng JPG, PNG, JPEG, GIF";
      $allowUpload = false;
  }


  if ($allowUpload)
  {
      // Xử lý di chuyển file tạm ra thư mục cần lưu trữ, dùng hàm move_uploaded_file
      if (move_uploaded_file($_FILES["fileupload"]["tmp_name"], $target_file))
      {
          echo "File ". basename( $_FILES["fileupload"]["name"]).
          " Đã upload thành công.";

          echo "File lưu tại " . $target_file;

      }
      else
      {
          echo "Có lỗi xảy ra khi upload file.";
      }
  }
  else
  {
      echo "Không upload được file, có thể do file lớn, kiểu file không đúng ...";
  }
?>
9 rồi so sánh với các mẫu cho phép.

NHANH CHÓNG CHẠY THỬ CODE TRÊN

Bạn có thể tạo ra một thư mục đặt tên

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
0, để trong đó 2 file
Chọn file để upload:
2 và
Chọn file để upload:
9 ở trên, trong thư mục này tạo thư mục con
Chọn file để upload:
8 để lưu file - cấu trúc này như tại đây: uploadfile

Sau đó vào thư mục

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
0 và gõ lệnh chạy Server HTTP của PHP

php -S 0.0.0.0:8088 -t uploadfile/

Từ trình duyệt truy cập

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
5 để chạy thử

Code PHP Upload nhiều File

Trong thẻ

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
6 nếu có thuộc tính
$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
7 và tên phần tử chỉ ra là mảng, thì cho phép chọn một lúc nhiều file để upload.

Ví dụ HTML như sau:

Lúc này tại PHP

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
8 mỗi thành phần sau đều là mảng chứa số lượng tương ứng của file upload

  • $_FILE = Array
        (
            [file1] => Array
                (
                    [name] => 'test2.txt';//một tên file người dùng upload
                    [type] => text/plain  (kiểu nội dung file text)
                    [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                    [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                    [size] => 123   (kích thước file - bype)
                )
    
            [file2] => Array
                (
                    [name] => test.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/php/php6hst32
                    [error] => UPLOAD_ERR_OK
                    [size] => 98174
                )
        )
    
    9 chứa các tên file upload (up 3 file thì là mảng 3 phần tử các tên)
  • php -S 0.0.0.0:8088 -t uploadfile/
    0 các kiểu file
  • php -S 0.0.0.0:8088 -t uploadfile/
    1 chứa các vị trí lưu tạm file
  • php -S 0.0.0.0:8088 -t uploadfile/
    2 mảng báo lỗi
  • php -S 0.0.0.0:8088 -t uploadfile/
    3 các kích thước file

Đoạn code sau hiện thị FORM để bạn chọn một lúc nhiều file upload, sau đó code PHP sẽ hiện thị các thông tin về các file mà bạn upload lên. Hãy chạy và chọn thử nhiều file upload để kiểm tra

File

php -S 0.0.0.0:8088 -t uploadfile/
4

";
                echo "Tên file: $names[$i] 
"; echo "Lưu tại: $tmp_names[$i]
"; echo "Cỡ file: $sizes[$i]

"; //Code xử lý di chuyển file đến thư mục cần thiết ở đây (bạn tự thực hiện) //Ví dụ move_uploaded_file($tmp_names[$i], /upload/'.$names[$i]); } } echo "Tổng số file upload: " .$numfiles; } ?>

Chọn file để upload: (Cỡ lớn nhất mà PHP đang cấu hình cho phép upload là )

Source code: uploadfile (Git), hoặc tải rphp-uploadfile

Chạy thử bằng cách để file này vào thư mục

$_FILE = Array
    (
        [file1] => Array
            (
                [name] => 'test2.txt';//một tên file người dùng upload
                [type] => text/plain  (kiểu nội dung file text)
                [tmp_name] => /tmp/php/php1h4j1 //vị trí lưu file tạm thời trên server.
                [error] => UPLOAD_ERR_OK (= 0 là không lỗi)
                [size] => 123   (kích thước file - bype)
            )

        [file2] => Array
            (
                [name] => test.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
0 như trên, và truy cập:
php -S 0.0.0.0:8088 -t uploadfile/
6 để kiểm tra