html文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>文件上传</title>
</head>
<body>
<div>
<form action="fileup.php" method="post" enctype="multipart/form-data">
<label for="myfile">请上传图片:</label>
<input type="file" name="myfile" id="myfile">
<button>提交</button>
</form>
</div>
</body>
</html>
fileup.php
<?php
// 1. 配置上传参数
$fileType=['jpg','png','gif'];
$fileSize=3145728;
$filePath='/mvc/';
//得到上传文件名
$fileName=$_FILES['myfile']['name'];
//给上传文件一个临时文件名
$tempFile=$_FILES['myfile']['tmp_name'];
//2. 判断是否上传成功
$uploadError=$_FILES['myfile']['error'];
if($uploadError>0){
switch ($uploadError){
case 1:
case 2:die('文件上传过大');
case 3:die('文件上传不完整');
default :die('未知');
}
}
// 3. 判断文件上传类型?
$extension = explode('.', $fileName)[1];
if (!in_array($extension, $fileType)){
die('请上传格式为:jpg、png.gif');
}
//生成临时文件名
$fileName= date('YmdHis',time()).md5(mt_rand(1,99)).'.'.$extension;
//文件上传
if (is_uploaded_file($tempFile)){
if (move_uploaded_file($tempFile,__DIR__.$filePath.$fileName)) {
echo '上传成功';
}else {
echo '上传失败';
}
}else {
die('非法操作');
}
exit;
总结
文件上传还是比较简单就是其中的几个函数还是得好好练习一下不然记不住