单图上传:
效果图:
uploda.html 带预览通过js实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
选择文件进行上传:
<input type="file" name="file" id="pic" onchange="xmTanUploadImg(this)">
<div>
<img width="10%" height="10%" id="xmTanImg">
<input type="submit" value="上传">
</div>
</form>
</body>
<script>
//选择图片,马上预览
function xmTanUploadImg(obj) {
var file = obj.files[0];
//file.size 单位为byte 可以做上传大小控制
console.log("file.size = " + file.size);
var reader = new FileReader();
//读取文件过程方法
reader.onloadstart = function (e) {
console.log("开始读取....");
}
reader.onprogress = function (e) {
console.log("正在读取中....");
}
reader.onabort = function (e) {
console.log("中断读取....");
}
reader.onerror = function (e) {
console.log("读取异常....");
}
reader.onload = function (e) {
console.log("成功读取....");
var img = document.getElementById("xmTanImg");
img.src = e.target.result;
//或者 img.src = this.result; //e.target == this
}
reader.readAsDataURL(file)
}
</script>
</html>
upload.php
<?php
$fileInfo = $_FILES['file'];
//var_dump($_FILES);
function upload_file($fileInfo,$upload = "./uploadfile",$imagesExt = ['gif','png','jpg'] )
{
if($fileInfo['error']=== 0){
//获取文件后缀
$ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
//判断是否存在定义的数组中
if(!in_array($ext,$imagesExt)){
return "非法文件类型";
}
if(!is_dir($upload)){
mkdir($upload,0777,true);
}
$fileName = md5(uniqid(microtime(true),true)) . "." .$ext;
$destName = $upload . "/" . $fileName;
//移动
if (!move_uploaded_file($fileInfo['tmp_name'],$destName)){
return "文件上传失败";
}
return "文件上传成功";
}else{
switch ($fileInfo['error']){
case 1:
echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值(2M)';
break;
case 2:
echo '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
echo '文件只有部分被上传';
break;
case 4:
echo '没有文件被上传';
break;
case 6:
echo '找不到临时文件夹';
break;
case 7:
echo '文件写入失败';
break;
}
}
}
var_dump(upload_file($fileInfo));
多图上传:
uploads.html
<body>
<form action="uploads.php" method="post" enctype="multipart/form-data">
选择文件进行上传: <input type="file" name="file[]" multiple=""><br>
<input type="submit" value="上传">
</form>
</body>
uploads.php
<?php
echo "<pre>";
var_dump($_FILES);
$files = [];
function getFile()
{
$i = 0;
foreach ($_FILES as $file) {
if (is_string($file['name'])) {
$files['$i'] = $file;
$i++;
} elseif (is_array($file['name'])) {
foreach ($file['name'] as $k => $v) {
$files[$i]['name'] = $file['name'][$k];
$files[$i]['type'] = $file['type'][$k];
$files[$i]['tmp_name'] = $file['tmp_name'][$k];
$files[$i]['error'] = $file['error'][$k];
$files[$i]['size'] = $file['size'][$k];
$i++;
}
}
}
return $files;
}
//对多个文件的上传信息进行解析
function uploads_file($fileInfo, $upload = "./uploads", $imagesExt = ['gif', 'png', 'jpg'])
{
$res = [];
if ($fileInfo['error'] === 0) {
$ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $imagesExt)) {
$res['mes'] = "文件非法类型";
}
if (!is_dir($upload)) {
mkdir($upload, 0777, true);
}
if ($res) {
return $res;
}
$fileName = md5(uniqid(microtime(true), true)) . "." . $ext;
$destName = $upload . "/" . $fileName;
if (!move_uploaded_file($fileInfo['tmp_name'], $destName)) {
$res['mes'] = "文件上传失败!";
}
$res['mes'] = $fileInfo['name'] . "文件上传成功!";
$res['dest'] = $destName;
return $res;
} else {
switch ($fileInfo['error']) {
case 1:
echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
echo '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
echo '文件只有部分被上传';
break;
case 4:
echo '没有文件被上传';
break;
case 6:
echo '找不到临时文件夹';
break;
case 7:
echo '文件写入失败';
break;
}
}
}
$files = getFile();
foreach ($files as $fileInfo) {
$res = uploads_file($fileInfo);
echo $res['mes'];
//var_dump($res['dest']);
}
视频上传:
uploadvideo.html
<form action="uploadvideo.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000000">
上传视频:<input type="file" name="upfile" size="20">
<input type="submit" value="上传">
</form>
</body>
uploadvideo.php
<?php
$file = $_FILES['upfile'];
function upload_video($files, $path = "./videos",$imagesExt=['mp4'])
{
// 判断错误号
if (@$files['error'] == 00) {
// 判断文件类型
$ext = strtolower(pathinfo(@$files['name'],PATHINFO_EXTENSION));
if (!in_array($ext,$imagesExt)){
return "非法文件类型";
}
// 判断是否存在上传到的目录
if (!is_dir($path)){
mkdir($path,0777,true);
}
// 生成唯一的文件名
$fileName = md5(uniqid(microtime(true),true)).'.'.$ext;
// 将文件名拼接到指定的目录下
$destName = $path."/".$fileName;
// 进行文件移动
if (!move_uploaded_file($files['tmp_name'],$destName)){
return "文件上传失败!";
}
return "文件上传成功!";
} else {
// 根据错误号返回提示信息
switch (@$files['error']) {
case 1:
echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
break;
case 2:
echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
break;
case 3:
echo "文件只有部分被上传";
break;
case 4:
echo "没有文件被上传";
break;
case 6:
case 7:
echo "系统错误";
break;
}
}
}
//调用方法
echo upload_video($file);
?>