亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php封裝單文件上傳到數(shù)據(jù)庫(kù)(路徑)

 更新時(shí)間:2017年10月15日 15:54:14   作者:jacklove617  
這篇文章主要介紹了php封裝單文件上傳到數(shù)據(jù)庫(kù)(路徑) 的相關(guān)資料,需要的朋友可以參考下

1.首先思考一個(gè)問(wèn)題上傳到數(shù)據(jù)庫(kù)是上傳的圖片還是圖片地址這里我們上傳的是圖片地址,因?yàn)閳D片或音頻存數(shù)據(jù)庫(kù)中過(guò)大,數(shù)據(jù)庫(kù)會(huì)崩掉。

下面是封裝的文件上傳的方法:

<?php
/*
*@prame string key
*@prame string path
*@prame String maxSize
*@prame array allowMime
*@prame array allowFiletype
*@prame bool true
*
*auther wulei
*/
function upload($key,$path,$maxSize,$allowMime,$allowType,$ifFileName = true){
  //第一步 判斷錯(cuò)誤碼
  if($_FILES[$key]['error']){
    switch($_FILES[$key]['error']){
      case 1:
        $str = "上傳的文件超過(guò)了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值。";
        break;
      case 2:
        $str = "上傳文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。";
        break;
      case 3:
        $str = "文件只有部分被上傳。";
        break;
      case 4:
        $str = "沒(méi)有文件被上傳。";
        break;
      case 6:
        $str = "找不到臨時(shí)文件夾。";
        break;
      case 7:
        $str = "文件寫(xiě)入失敗";
        break;
    }
    return [0,$str];
  }
  //判斷文件大小
  if($_FILES[$key]['size']>$maxSize){
    return [0,'傳的文件超過(guò)最大限制'];
  }
  //判斷文件的mime類(lèi)型
  if(!in_array($_FILES[$key]['type'],$allowMime)){
    return [0,'不符合的mime類(lèi)型'];
  }
  //判斷文件的后綴
  $info = pathinfo($_FILES[$key]['name']);
  $sub = $info['extension'];
  if(!in_array($sub,$allowType)){
    return [0,'不符合的文件后綴'];
  }
  //判斷是否是隨機(jī)文件
  if($ifFileName){
    $name = uniqid().'.'.$sub;
  }else{
    $name = $info;
  }
  //拼接路徑
  $path = rtrim($path,'/').'/'.date('Y/m/d').'/';
  //判斷文件是否存在,不存在則創(chuàng)建
  if(!file_exists($path)){
    mkdir($path,0777,true);
  }
  //判斷是否是上傳文件
  if(is_uploaded_file($_FILES[$key]['tmp_name'])){
    if(move_uploaded_file($_FILES[$key]['tmp_name'],$path.$name)){
      echo '文件上傳成功';
      return [1,$path.$name];
    }else{
      return[0,'上傳文件失敗'];
    }
  }else{
    return [0,'文件不存在'];
  }
  }

2.html 頁(yè)面

<html>
<head>
  <title>文件上傳</title>
  <meta charset = "utf-8"/>
</head>
<body>
  <form action = "onUpload.php" method = "post" enctype ="multipart/form-data">
    <!--<input type = "text" name = "username"/><br/>-->
    <input type = "file" name = "file"/><br/>
    <input type = "submit" value ="提交"/>
  </form>
</body>

3、下面我們鏈接數(shù)據(jù)庫(kù)

這里我們直接使用了,看不懂的可以去看前面的封裝的數(shù)據(jù)庫(kù)方法那一篇文章

<?php
  //包含方法
  include 'uploed.php';
  include 'common.php';
  //得到方法
  $data = upload('file','image',pow(1024,2)*2,[
        'image/png','image/jpeg','image/gif','image/wbmp'
      ],['png','jpg','jpeg','jpe','pjpeg','gif','wbmp','bmp']);
  //這里進(jìn)行數(shù)據(jù)庫(kù)操作
  if($data[0]){
    $date['img_path'] = $data[1];
  }
  insert($link,'user',$date);

總結(jié)

以上所述是小編給大家介紹的php封裝單文件上傳到數(shù)據(jù)庫(kù)(路徑),希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論