Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách tải tệp lên máy chủ từ xa bằng biểu mẫu HTML đơn giản và PHP. Bạn có thể tải lên bất kỳ loại tệp nào như hình ảnh, video, tệp ZIP, tài liệu Microsoft Office, PDF, cũng như các tệp thực thi và nhiều loại tệp khác
Bước 1. Tạo biểu mẫu HTML để tải tệp lên
Ví dụ sau sẽ tạo một biểu mẫu HTML đơn giản có thể được sử dụng để tải tệp lên
File Upload Form
Upload File
Filename:
Note: Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.
Ghi chú. Ngoài trường chọn tệp, biểu mẫu tải lên phải sử dụng phương thức đăng HTTP và phải chứa thuộc tính enctype="multipart/form-data"
. Thuộc tính này đảm bảo rằng dữ liệu biểu mẫu được mã hóa dưới dạng dữ liệu MIME nhiều phần — cần thiết để tải lên số lượng lớn dữ liệu nhị phân như hình ảnh, âm thanh, video, v.v.
Bước 2. Đang xử lý tệp đã tải lên
Đây là mã hoàn chỉnh của "upload-manager" của chúng tôi. tập tin php". Nó sẽ lưu trữ vĩnh viễn tệp đã tải lên trong thư mục "tải lên" cũng như thực hiện một số kiểm tra bảo mật cơ bản như loại tệp và kích thước tệp để đảm bảo rằng người dùng tải lên đúng loại tệp và trong giới hạn cho phép
"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"];
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo[$filename, PATHINFO_EXTENSION];
if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."];
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."];
// Verify MYME type of the file
if[in_array[$filetype, $allowed]]{
// Check whether file exists before uploading it
if[file_exists["upload/" . $filename]]{
echo $filename . " is already exists.";
} else{
move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename];
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
Ghi chú. Tập lệnh trên ngăn tải lên tệp có cùng tên với tệp hiện có trong cùng thư mục. Tuy nhiên, nếu bạn muốn cho phép điều này, chỉ cần thêm vào trước tên tệp một chuỗi hoặc dấu thời gian ngẫu nhiên, chẳng hạn như $filename = time[] . '_' . $_FILES["photo"]["name"];
Bạn có thể tự hỏi mã này là gì. Chà, chúng ta hãy lần lượt xem qua từng phần của mã ví dụ này để hiểu rõ hơn về quy trình này
Giải thích mã
Sau khi biểu mẫu được gửi, thông tin về tệp đã tải lên có thể được truy cập thông qua mảng siêu toàn cục PHP có tên là $_FILES
. Ví dụ: biểu mẫu tải lên của chúng tôi chứa trường chọn tệp có tên là ảnh [i. e.
"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"];
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo[$filename, PATHINFO_EXTENSION];
if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."];
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."];
// Verify MYME type of the file
if[in_array[$filetype, $allowed]]{
// Check whether file exists before uploading it
if[file_exists["upload/" . $filename]]{
echo $filename . " is already exists.";
} else{
move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename];
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
0], nếu bất kỳ người dùng nào tải tệp lên bằng trường này, chúng tôi có thể lấy thông tin chi tiết của tệp như tên, loại, kích thước, tên tạm thời hoặc bất kỳ lỗi nào xảy ra khi cố tải lên qua mảng kết hợp "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"];
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo[$filename, PATHINFO_EXTENSION];
if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."];
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."];
// Verify MYME type of the file
if[in_array[$filetype, $allowed]]{
// Check whether file exists before uploading it
if[file_exists["upload/" . $filename]]{
echo $filename . " is already exists.";
} else{
move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename];
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
1, như sau
2 — Giá trị mảng này chỉ định tên gốc của tệp, bao gồm cả phần mở rộng tệp. Nó không bao gồm đường dẫn tệp"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"]; $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo[$filename, PATHINFO_EXTENSION]; if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."]; // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."]; // Verify MYME type of the file if[in_array[$filetype, $allowed]]{ // Check whether file exists before uploading it if[file_exists["upload/" . $filename]]{ echo $filename . " is already exists."; } else{ move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename]; echo "Your file was uploaded successfully."; } } else{ echo "Error: There was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?>
3 — Giá trị mảng này chỉ định loại MIME của tệp"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"]; $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo[$filename, PATHINFO_EXTENSION]; if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."]; // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."]; // Verify MYME type of the file if[in_array[$filetype, $allowed]]{ // Check whether file exists before uploading it if[file_exists["upload/" . $filename]]{ echo $filename . " is already exists."; } else{ move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename]; echo "Your file was uploaded successfully."; } } else{ echo "Error: There was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?>
0 — Giá trị mảng này chỉ định kích thước tệp, tính bằng byte"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"]; $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo[$filename, PATHINFO_EXTENSION]; if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."]; // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."]; // Verify MYME type of the file if[in_array[$filetype, $allowed]]{ // Check whether file exists before uploading it if[file_exists["upload/" . $filename]]{ echo $filename . " is already exists."; } else{ move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename]; echo "Your file was uploaded successfully."; } } else{ echo "Error: There was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?>
1 — Giá trị mảng này chỉ định tên tạm thời bao gồm đường dẫn đầy đủ được gán cho tệp sau khi tệp được tải lên máy chủ"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"]; $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo[$filename, PATHINFO_EXTENSION]; if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."]; // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."]; // Verify MYME type of the file if[in_array[$filetype, $allowed]]{ // Check whether file exists before uploading it if[file_exists["upload/" . $filename]]{ echo $filename . " is already exists."; } else{ move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename]; echo "Your file was uploaded successfully."; } } else{ echo "Error: There was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?>
2 — Giá trị mảng này chỉ định lỗi hoặc mã trạng thái liên quan đến tệp tải lên, e. g. nó sẽ là 0, nếu không có lỗi"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"]; $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; // Verify file extension $ext = pathinfo[$filename, PATHINFO_EXTENSION]; if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."]; // Verify file size - 5MB maximum $maxsize = 5 * 1024 * 1024; if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."]; // Verify MYME type of the file if[in_array[$filetype, $allowed]]{ // Check whether file exists before uploading it if[file_exists["upload/" . $filename]]{ echo $filename . " is already exists."; } else{ move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename]; echo "Your file was uploaded successfully."; } } else{ echo "Error: There was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?>
Mã PHP trong ví dụ sau sẽ chỉ hiển thị chi tiết của tệp đã tải lên và lưu trữ tệp đó trong một thư mục tạm thời trên máy chủ web
0]{
echo "Error: " . $_FILES["photo"]["error"] . "
";
} else{
echo "File Name: " . $_FILES["photo"]["name"] . "
";
echo "File Type: " . $_FILES["photo"]["type"] . "
";
echo "File Size: " . [$_FILES["photo"]["size"] / 1024] . " KB
";
echo "Stored in: " . $_FILES["photo"]["tmp_name"];
}
?>
Mẹo. Khi một tệp đã được tải lên thành công, nó sẽ tự động được lưu trữ trong một thư mục tạm thời trên máy chủ. Để lưu trữ tệp này vĩnh viễn, bạn cần di chuyển tệp từ thư mục tạm thời đến vị trí cố định bằng cách sử dụng hàm
"image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"];
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo[$filename, PATHINFO_EXTENSION];
if[!array_key_exists[$ext, $allowed]] die["Error: Please select a valid file format."];
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if[$filesize > $maxsize] die["Error: File size is larger than the allowed limit."];
// Verify MYME type of the file
if[in_array[$filetype, $allowed]]{
// Check whether file exists before uploading it
if[file_exists["upload/" . $filename]]{
echo $filename . " is already exists.";
} else{
move_uploaded_file[$_FILES["photo"]["tmp_name"], "upload/" . $filename];
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
3 của PHP