php简单文件上传和下载

demo


php简单实现文件上传和下载
当然实际应用中还是要检测文件大小呀,格式呀什么的…

1. html

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<style>
*{
font-family:'';
}
</style>
</head>
<body>
<center>
<h2>upload a file<h2>
<form style='width:400px;height:400px;border:solid 5px red;border-radius:10px' enctype='multipart/form-data' method='post' action='upload.php'>
<input type='file' name='file'>
</br>
<input type='submit' value='submit'>
</br>
</br>
<form>
</center>
</body>
</html>

2. upload

upload.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<center>
<?php
header('content-type:text/html;charset=utf-8');
$file=$_FILES;
//echo '';
if(!is_uploaded_file($file['file']['tmp_name'])){
echo 'please select a file ...';
}else{
move_uploaded_file($file['file']['tmp_name'],$file['file']['name']);
echo "<a href='download.php?a=".$file['file']['name']."'>".$file['file']['name']."</a>";
}
?>
</center>

3. download

download.php

1
2
3
4
5
6
7
8
9
10
<?php
//$filename="http://liaoxuefeng-static.oss-cn-hangzhou.aliyuncs.com/static-img/maiziedu.jpg";
$filename=$_GET["a"];
header('content-type:text/html;charset=utf-8');
header("Content-type: octet/stream");
header("content_type:application/octed-stream");
header('Content-Disposition: attachment; filename='.$filename);
ob_clean ();
readfile($filename);
?>

4. HTML5

1
<a href="photo.jpg" download="photo_name">download</a>

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 1. html
  2. 2. 2. upload
  3. 3. 3. download
  4. 4. 4. HTML5
,