欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.首先思考一個問題上傳到數(shù)據(jù)庫是上傳的圖片還是圖片地址這里我們上傳的是圖片地址,因為圖片或音頻存數(shù)據(jù)庫中過大,數(shù)據(jù)庫會崩掉。

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

<?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){
  //第一步 判斷錯誤碼
  if($_FILES[$key]['error']){
    switch($_FILES[$key]['error']){
      case 1:
        $str = "上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值。";
        break;
      case 2:
        $str = "上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。";
        break;
      case 3:
        $str = "文件只有部分被上傳。";
        break;
      case 4:
        $str = "沒有文件被上傳。";
        break;
      case 6:
        $str = "找不到臨時文件夾。";
        break;
      case 7:
        $str = "文件寫入失敗";
        break;
    }
    return [0,$str];
  }
  //判斷文件大小
  if($_FILES[$key]['size']>$maxSize){
    return [0,'傳的文件超過最大限制'];
  }
  //判斷文件的mime類型
  if(!in_array($_FILES[$key]['type'],$allowMime)){
    return [0,'不符合的mime類型'];
  }
  //判斷文件的后綴
  $info = pathinfo($_FILES[$key]['name']);
  $sub = $info['extension'];
  if(!in_array($sub,$allowType)){
    return [0,'不符合的文件后綴'];
  }
  //判斷是否是隨機文件
  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 頁面

<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ù)庫

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

<?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']);
  //這里進行數(shù)據(jù)庫操作
  if($data[0]){
    $date['img_path'] = $data[1];
  }
  insert($link,'user',$date);

總結(jié)

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

相關(guān)文章

最新評論