php Encapsulation Single File Upload to Database (Path)

  • 2021-08-10 06:57:26
  • OfStack

1. First of all, think about a question. Is it an uploaded picture or a picture address that is uploaded to the database? Here, we upload a picture address, because the picture or audio is too large in the database, and the database will collapse.

Here is how to upload encapsulated files:


<?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){
  // No. 1 1 Step   Judgment error code 
  if($_FILES[$key]['error']){
    switch($_FILES[$key]['error']){
      case 1:
        $str = " Uploaded more files than  php.ini  Medium  upload_max_filesize  Gets or sets the value restricted by the. ";
        break;
      case 2:
        $str = " The size of the uploaded file exceeds  HTML  In the form  MAX_FILE_SIZE  The value specified by the. ";
        break;
      case 3:
        $str = " Only part of the file was uploaded. ";
        break;
      case 4:
        $str = " No files were uploaded. ";
        break;
      case 6:
        $str = " Temporary folder could not be found. ";
        break;
      case 7:
        $str = " File write failure ";
        break;
    }
    return [0,$str];
  }
  // Determine file size 
  if($_FILES[$key]['size']>$maxSize){
    return [0,' The file transferred exceeds the maximum limit '];
  }
  // Determine the file's mime Type 
  if(!in_array($_FILES[$key]['type'],$allowMime)){
    return [0,' Non-conforming mime Type '];
  }
  // Determine the suffix of the file 
  $info = pathinfo($_FILES[$key]['name']);
  $sub = $info['extension'];
  if(!in_array($sub,$allowType)){
    return [0,' Incompatible file suffix '];
  }
  // Judge whether it is a random file 
  if($ifFileName){
    $name = uniqid().'.'.$sub;
  }else{
    $name = $info;
  }
  // Splice path 
  $path = rtrim($path,'/').'/'.date('Y/m/d').'/';
  // Determine whether the file exists , Create if it does not exist 
  if(!file_exists($path)){
    mkdir($path,0777,true);
  }
  // Determine whether the file is uploaded or not 
  if(is_uploaded_file($_FILES[$key]['tmp_name'])){
    if(move_uploaded_file($_FILES[$key]['tmp_name'],$path.$name)){
      echo ' File uploaded successfully ';
      return [1,$path.$name];
    }else{
      return[0,' Failed to upload file '];
    }
  }else{
    return [0,' File does not exist '];
  }
  }

2. html page


<html>
<head>
  <title> File upload </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 =" Submit "/>
  </form>
</body>

3. Let's link to the database below

Here we use it directly. If you can't understand it, you can see the encapsulated database method in front of that 1 article


<?php
  // Include method 
  include 'uploed.php';
  include 'common.php';
  // Get method 
  $data = upload('file','image',pow(1024,2)*2,[
        'image/png','image/jpeg','image/gif','image/wbmp'
      ],['png','jpg','jpeg','jpe','pjpeg','gif','wbmp','bmp']);
  // Database operations are performed here 
  if($data[0]){
    $date['img_path'] = $data[1];
  }
  insert($link,'user',$date);

Summarize

The above is the site to introduce the php encapsulation file uploaded to the database (path), I hope to help you!


Related articles: