Files
luowei/upload_1.php
2025-01-06 19:52:30 +08:00

47 lines
1.3 KiB
PHP

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<?php
// 定义文件上传目录
$uploadDir = __DIR__ . "/upload/tmp/";
// 检查并创建目录
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
// 处理文件上传
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["upload_file"])) {
$file = $_FILES["upload_file"];
$fileName = basename($file["name"]);
$targetFile = $uploadDir . $fileName;
// 检查文件是否上传成功
if ($file["error"] === UPLOAD_ERR_OK) {
// 移动文件到指定目录
if (move_uploaded_file($file["tmp_name"], $targetFile)) {
echo "<p>文件上传成功!文件名:{$fileName}</p>";
echo "<p>存储路径:{$targetFile}</p>";
} else {
echo "<p>文件上传失败,请检查目录权限!</p>";
}
} else {
echo "<p>文件上传出错,错误码:" . $file["error"] . "</p>";
}
}
?>
<!-- 文件上传表单 -->
<form action="upload_1.php" method="post" enctype="multipart/form-data">
<label for="upload_file">请选择文件:</label>
<input type="file" id="upload_file" name="upload_file">
<br><br>
<input type="submit" id="ok" name="ok" value="上传">
</form>
</body>
</html>