html 前端 文件名 :upload.html
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<html>
<head>
<title>File Upload Example</title>
</head>
<body>
<h1>File Upload Example</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Select file to upload:</label>
<input type="file" name="file" id="file">
<br><br>
<input type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
php 后端 文件名:upload.php(此文件名要和html代码中写<-form action=“upload.php”>要对应)
<?php
// Set upload directory
$target_dir = "./upload/";
// Check if file was submitted
if(isset($_POST["submit"])) {
// Get file name and size
$file_name = basename($_FILES["file"]["name"]);
$file_size = $_FILES["file"]["size"];
// Set max file size (in bytes)
$max_size = 500 * 1024 * 1024; // 500MB
// Check if file size is within limit
if($file_size > $max_size) {
echo "Error: File is too large (max 500MB)";
exit();
}
// Set target file path
$target_file = $target_dir . $file_name;
// Check if file already exists
if(file_exists($target_file)) {
echo "Error: File already exists";
exit();
}
// Upload file to target directory
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "File uploaded successfully";
} else {
echo "Error: Failed to upload file";
}
}
?>