推荐答案
在 PHP 中,文件上传可以通过 $_FILES 超全局数组来处理。以下是一个简单的文件上传示例:
// www.javascriptcn.com code example
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// 检查文件是否为真实的图片
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// 检查文件是否已经存在
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// 检查文件大小
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// 允许特定文件格式
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// 检查 $uploadOk 是否为 0
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// 如果一切正常,尝试上传文件
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>本题详细解读
1. HTML 表单
文件上传的第一步是创建一个 HTML 表单,表单的 enctype 属性必须设置为 multipart/form-data,这样才能支持文件上传。
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>2. PHP 处理文件上传
当表单提交后,PHP 会通过 $_FILES 超全局数组来处理上传的文件。$_FILES 数组包含了文件的各种信息,如文件名、临时文件路径、文件大小等。
$target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
3. 文件验证
在上传文件之前,通常需要对文件进行一些验证,例如检查文件类型、文件大小、文件是否已存在等。
// www.javascriptcn.com code example
// 检查文件是否为真实的图片
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
// 检查文件是否已经存在
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// 检查文件大小
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// 允许特定文件格式
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}4. 移动上传的文件
如果所有验证都通过,可以使用 move_uploaded_file() 函数将文件从临时位置移动到目标位置。
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}5. 安全性考虑
在实际应用中,文件上传功能可能会带来安全风险,因此需要特别注意以下几点:
- 限制文件类型和大小。
- 对上传的文件进行病毒扫描。
- 不要直接将用户上传的文件名用作目标文件名,避免路径遍历攻击。
- 将上传的文件存储在 Web 根目录之外,防止直接访问。