下面是简单粗略的excel文件上传和下载,可以参考下。
html部分:
<html>
<meta charset="utf-8">
<head></head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file_stu" id="file_stu"><br>
<input type="submit" name="submit" value="提交">
</form>
<button>
<a href = "http://www.test.com/upload/20191010/20191010025711.xlsx"/>
下载文件
</button>
</body>
</html>
php部分:
<?php
$tmp_file = $_FILES ['file_stu'] ['tmp_name'];
$file_types = explode ( ".", $_FILES ['file_stu']['name'] );
$file_type = $file_types [count ( $file_types ) - 1];
/*判别是不是.xls文件,判别是不是excel文件*/
/*设置上传路径*/
$year = date('Y');
$month= date('m');
$day= date('d');
$str = $year.$month.$day;
$path = getcwd()."/upload/".$str;
echo "$path";
if(!file_exists($path))//判断文件夹是否存在
{
mkdir($path);
}
/*以时间来命名上传的文件*/
$strs = date ( 'Ymdhis' );
$file_name = $strs . "." . $file_type;
/*是否上传成功*/
if (!move_uploaded_file($tmp_file, $path ."/". $file_name)){
$this->error ( '上传失败' );
} else {
echo "yes";
}