Hướng dẫn file upload in php - tải lên tệp trong php
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ử Show 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
0Chú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à:
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 ở 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 2Hiệ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ó: 4 Phần tử chọn file trong form làinput với kiểu type="file" . Tên phần tử là 74 Phần tử chọn file trong form là input với kiểu type="file" . Tên phần tử là 7 Code PHP xử lý Upload FileCode 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 8Code xử lý upload file để trong file 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à 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 2 và 9 ở trên, trong thư mục này tạo thư mục con 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 FileTrong 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
Đ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] 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 |