在 PHP 中实现文件上传,核心在于正确配置 php.ini 中的关键指令并配合前端表单的 enctype 属性。必须确保 file_uploads = On 开启上传功能,根据业务需求调整 upload_max_filesize(单文件大小限制)和 post_max_size(POST 请求总大小限制),同时设置 max_execution_time 防止大文件超时,并在代码中使用 move_uploaded_file() 安全地将临时文件移至目标目录。

php.ini 核心上传参数解析
PHP 处理文件上传时,服务器端的配置直接决定了能否接收以及能接收多大的文件。最关键的三个参数是 file_uploads、upload_max_filesize 和 post_max_size。

file_uploads = On 是基础开关,默认通常为开启状态。若关闭,任何上传请求都会被拒绝。upload_max_filesize 定义了单个文件允许的最大大小,例如设置为 10M 表示只能上传不超过 10MB 的文件。需要注意的是,这个值不能超过 post_max_size,因为文件上传是通过 POST 方法发送的,所有数据都包含在 POST body 中。post_max_size 限制了整个 POST 请求体的最大大小,如果上传多个文件或文件较大,需确保此值大于 upload_max_filesize 加上其他表单字段的大小。max_execution_time 应适当调大,避免在处理大文件传输时脚本超时被终止。
完整可运行的上传示例程序
以下是一个完整的 PHP 上传脚本示例,包含 HTML 表单和后端处理逻辑。将代码保存为 upload.php,并确保同级目录下有一个名为 uploads 的文件夹且拥有写入权限。通过浏览器访问该文件即可测试。

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>PHP File Upload Test</title>
</head>
<body>
<h2>Upload a File</h2>
<!-- enctype 必须设置为 multipart/form-data -->
<form action="" method="POST" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="myFile" id="myFile"><br>
<input type="submit" value="Upload File" name="submit">
</form>
<?php
// 检查是否有提交动作
if(isset($_POST["submit"])) {
$target_dir = "uploads/";
// 确保目标目录存在
if (!file_exists($target_dir)) {
mkdir($target_dir, 0755, true);
}
$target_file = $target_dir . basename($_FILES["myFile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["myFile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".<br>";
} else {
echo "File is not an image.<br>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.<br>";
$uploadOk = 0;
}
// Check file size (optional client-side check, server config overrides)
if ($_FILES["myFile"]["size"] > 500000) {
echo "Sorry, your file is too large.<br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.<br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["myFile"]["tmp_name"], $target_file)) {
echo "The file " . htmlspecialchars( basename( $_FILES["myFile"]["name"])). " has been uploaded.<br>";
} else {
echo "Sorry, there was an error uploading your file.<br>";
}
}
}
?>
</body>
</html>
运行上述代码后,选择一个小图片点击上传,若成功会在页面显示 “has been uploaded” 并将文件存入 uploads 目录。若遇到 “Sorry, your file was not uploaded”,请检查 php.ini 中的 upload_max_filesize 是否小于所选文件,或确认 uploads 目录权限是否为 755 或 775。通过观察 $_FILES['myFile']['error'] 的值可以精确定位是配置限制或路径权限问题。
该程序先检查请求是否为 POST,然后验证文件是否成功上传且无错误。接着使用 move_uploaded_file() 将临时文件移动到指定目录。这是排查上传失败时的标准参考实现。
常见错误码与调试策略
当上传失败时,开发者应优先检查 $_FILES['file']['error'] 返回的错误码,而非盲目修改代码。不同的错误码对应不同的配置或操作问题。
错误码 UPLOAD_ERR_INI_SIZE (1) 表示文件大小超过了 php.ini 中 upload_max_filesize 的限制;UPLOAD_ERR_FORM_SIZE (2) 表示超过了表单中 MAX_FILE_SIZE 隐藏域指定的限制;UPLOAD_ERR_PARTIAL (3) 通常意味着网络中断或服务器负载过高导致文件未完全上传;UPLOAD_ERR_NO_TMP_DIR (6) 则提示服务器缺少临时文件夹。解决这些问题的关键在于对照错误码调整对应的配置项或检查文件系统权限。