PHP實(shí)現(xiàn)文件上傳與下載
更新時(shí)間:2020年08月28日 11:36:13 作者:shofe的菜鳥人生
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)文件上傳與下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了PHP實(shí)現(xiàn)文件上傳與下載的具體代碼,供大家參考,具體內(nèi)容如下
主頁index.php
<html> <head> <title>圖片操作</title> <style> #contains { width: 500px; margin: 0 auto; text-align: center; color: #0F0; } th { background: #ccc; } td { width: 150px; height: 50px; text-align: center; } </style> </head> <body> <div id="contains"> <h2>圖片上傳和下載</h2> <!----------------文件上傳表單-------------> <form action="doupload.php" method="post" enctype="multipart/form-data"> <input type="hidden" value="10000000" /> <!---避免上傳后發(fā)現(xiàn)文件過大---> <input type="file" name="pic"/> <input type="submit" value="上傳"/> </form> <!---------------------------------------> <table width="500" border="0"> <tr> <th>序號(hào)</th><th>圖片</th><th>添加時(shí)間</th><th>操作</th> </tr> <?php //1.打開目錄 $dir = opendir("./imgs"); //2.遍歷目錄 $i = 0; $color = "#ff0"; while ($f = readdir($dir)){ //$f代表這每個(gè)文件的名字 if ($f == '.' || $f == "..") continue; //處理特殊隱藏的文件 $i++; if ($i % 2 == 0) $color = "#ccc"; else $color = "#ffa"; echo "<tr bgcolor=$color>"; echo "<td>{$i}</td>"; echo "<td><img src='./imgs/{$f}' width='150' height='50'/></td>"; echo "<td>".date("Y-m-d", filectime('./imgs/'.$f))."</td>"; echo "<td><a href='./imgs/{$f}'>查看</a> <a href='download.php?name={$f}'>下載</a></td>"; echo "</tr>"; // echo $f." "; } ?> </table> </div> </body> </html>
上傳doupload.php
<?php /* echo "<pre>"; var_dump($_FILES); echo "</pre>";*/ //1.獲取上傳文件信息 $upfile = $_FILES["pic"]; $path = "./imgs/"; //2、過濾錯(cuò)誤信息 if ($upfile["error"] > 0) { die("上傳文件錯(cuò)誤"); } //3、本次上傳文件的大小過濾 if ($upfile["size"] > 10000000) { die("上傳文件超出限制"); } //4、處理文件類型 $typelist = array("jpeg","jpg","png","gif"); $arr = explode(".", basename($upfile['name'])); //以'.'分割字符串為數(shù)組 $bz = array_pop($arr); //獲取文件的后綴名 if (!in_array($bz, $typelist)) { //如果給定的值 value 存在于數(shù)組 array 中則返回 true die("上傳文件類型非法!".$upfile["type"]); } //5、設(shè)置相同文件的名字不同 $newfile = date("YmdHis").rand(100, 999).".".$bz; // if (is_uploaded_file($upfile["tmp_name"])) { //判斷文件是否是通過post上傳 //執(zhí)行文件上傳 if (move_uploaded_file($upfile["tmp_name"], $path.$newfile)) {//將上傳的文件保存在新位置 echo "上傳成功!"; echo "<a href='index.php'>瀏覽</a>"; }else { die("上傳失敗"); } } ?>
下載download.php
<?php //1.獲取于要下載的文件名 $file = "./imgs/".$_GET["name"]; // echo $file; //2.重設(shè)響應(yīng)類型 $info = getimagesize($file); //獲取文件大小 // var_dump($info); header("Content-Type:".$info["mime"]); //3.執(zhí)行下載文件名 header("Content-Disposition:attachment;filename=".$_GET["name"]); //4.指定文件大小 header("Content-Length:".filesize($file)); //5.響應(yīng)內(nèi)容 readfile($file); ?>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP隨機(jī)獲取未被微信屏蔽的域名(微信域名檢測(cè))
這篇文章主要介紹了PHP隨機(jī)獲取未被微信屏蔽的域名(微信域名檢測(cè)),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Laravel學(xué)習(xí)基礎(chǔ)之migrate的使用教程
這篇文章主要給大家介紹了關(guān)于Laravel學(xué)習(xí)基礎(chǔ)之migrate使用的相關(guān)資料,文中通過示例代碼介紹非常詳細(xì),分別介紹了生成遷移、遷移結(jié)構(gòu)、運(yùn)行遷移和回滾遷移等實(shí)現(xiàn)的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10thinkphp 3.2框架視圖模型 實(shí)例視圖查詢結(jié)果的二維數(shù)組合并操作示例
這篇文章主要介紹了thinkphp 3.2框架視圖模型 實(shí)例視圖查詢結(jié)果的二維數(shù)組合并操作,結(jié)合實(shí)例形式分析了thinkPHP3.2針對(duì)視圖查詢結(jié)果的數(shù)組合并相關(guān)操作技巧,需要的朋友可以參考下2020-03-03