首页 > 代码库 > php -- 文件上传下载

php -- 文件上传下载

----- 026-upload.php -----

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="content-type" content="text/html; charset=utf-8">
 5     <title>上传图片</title>
 6 </head>
 7 <body align="left">
 8 <h2>上传图片</h2>
 9 <p style="font-size: 16pt">
10 <form method="POST"enctype="multipart/form-data" style="border:1px solid; width:400px" >
11     上传图片:<input type="file" name=‘file‘ id="file"><br/>
12     放在这里:<input type="text" name="txt"><br/>
13     <input type="submit" name="upload" value="http://www.mamicode.com/开始上传">
14 </form>
15 <?php
16     foreach ($_POST as $key => $value) {
17         echo $key, "=>", $value, "<br/>";
18     }
19     if(isset($_POST["upload"])){
20         echo "就绪了啊!!<br/>";
21         echo $_FILES[‘file‘][‘type‘];
22         if($_FILES[‘file‘][‘type‘] == "image/pjpeg")
23         {
24             echo "文件名:", $_FILES[‘file‘][‘name‘], "<br/>";
25             echo "文件类型:", $_FILES[‘file‘][‘type‘], "<br/>";
26             echo "文件大小:", $_FILES[‘file‘][‘size‘], "<br/>";
27             echo "副本名称:", $_FILES[‘file‘][‘tmp_name‘], "<br/>";
28             move_uploaded_file($_FILES[‘file‘][‘tmp_name‘], "uploads/".$_FILES[‘file‘][‘name‘]);
29             echo "文件储存在:",  "uploads/".$_FILES[‘file‘][‘name‘], "<br/>";
30             echo "<img src="http://www.mamicode.com/,"uploads/".$_FILES[‘file‘][‘name‘], " width=300>";
31 
32         }
33     }
34 ?>
35 </p>
36 </body>
37 </html>

----- 027-download.php -----

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="content-type" content="text/html; charset=utf-8">
 5     <title>下载文件</title>
 6 </head>
 7 <body>
 8 <fieldset>
 9 <legend>文件下载</legend>
10 <img src="http://www.mamicode.com/bietaichang.jpg" height="200")/>
11 <a href="http://www.mamicode.com/?action=download">下载文件到本地<br/></a>
12 <?php
13     $file_name = "bietaichang.jpg";
14     $file_dir = "uploads/";
15     echo "文件大小:", filesize($file_dir.$file_name), "\n";
16     if(isset($_GET["action"]))
17     {
18         $file = fopen($file_dir.$file_name, "rb");
19         header("Content-Type: application/octet-stream");
20         header("Accept-Ranges: bytes");
21         header("Accept-Length: ".filesize($file_dir.$file_name));
22         header("Content-Disposition: attachment; filename=".$file_name);
23         echo fread($file, filesize($file_dir.$file_name));
24         fclose($file);
25         exit;
26     }
27 ?>
28 </fieldset>
29 </body>
30 </html>