溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用PHP上傳與下載文件

發(fā)布時間:2020-11-06 16:26:52 來源:億速云 閱讀:141 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)使用PHP上傳與下載文件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

主頁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">
 <h3>圖片上傳和下載</h3>
 <!----------------文件上傳表單------------->
 <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>序號</th><th>圖片</th><th>添加時間</th><th>操作</th>
 </tr>
 <&#63;php
  //1.打開目錄
 $dir = opendir("./imgs");
 //2.遍歷目錄
 $i = 0;
 $color = "#ff0";
 while ($f = readdir($dir)){ //$f代表這每個文件的名字
   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&#63;name={$f}'>下載</a></td>";
  echo "</tr>";
 // echo $f." ";
 }
 &#63;>
 </table>
 </div>
</body>
</html>

上傳doupload.php

<&#63;php
 /* echo "<pre>";
 var_dump($_FILES);
 echo "</pre>";*/
 //1.獲取上傳文件信息
 $upfile = $_FILES["pic"]; 
 $path = "./imgs/";
 //2、過濾錯誤信息
 if ($upfile["error"] > 0) {
 die("上傳文件錯誤");
 }
 //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("上傳失敗");
 }
 }
&#63;>

下載download.php

<&#63;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);
&#63;>

使用PHP上傳與下載文件

上述就是小編為大家分享的使用PHP上傳與下載文件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI