PHP与html上传文件

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";
	}
}
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值