PHP implementation encapsulates a single file upload to a database instance

Source: Internet
Author: User
This article mainly introduces the PHP package single file upload to the database (path) of the relevant information, the need for friends can refer to the following

1. First think of a question to upload to the database is the image or image address here we upload a picture address, because the picture or audio storage database is too large, the database will be collapsed.

Here's how to upload the encapsulated file:


<?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) {//First step Determine the error code if ($_files[$key [' ERROR ']) {switch ($_files[$key] [' ERROR ']) {Case 1: $str = "The uploaded file exceeds php.ini Uploa The value of the D_max_filesize option limit.        ";      Break Case 2: $str = "The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form.        ";      Break Case 3: $str = "The file is only partially uploaded.        ";      Break Case 4: $str = "No files were uploaded.        ";      Break Case 6: $str = "The temporary folder cannot be found.        ";      Break        Case 7: $str = "file Write Failed";    Break  } return [0, $str];  }//Determine file size if ($_files[$key [' Size ']> $maxSize) {return [0, ' the file passed the maximum limit '];  }//Determine the MIME type of the file 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, ' non-conforming file suffix ']; }//Determine if it is a random file if ($ifFileName) {$name = Uniqid ().  $sub;  }else{$name = $info; }//stitching path $path = RTrim ($path, '/'). ' /'. Date (' y/m/d '). '  /';  Determine if the file exists and does not exist create if (!file_exists ($path)) {mkdir ($path, 0777,true); }//Determine if the upload file if (is_uploaded_file ($_files[$key [' Tmp_name '])) {if (Move_uploaded_file ($_files[$key] [' Tmp_name '],$      Path. $name)) {echo ' file upload succeeded ';    return [1, $path. $name];    }else{return[0, ' upload file failed '];  }}else{return [0, ' file does not exist ']; }  }

2.html page



3, below we link database

Here we use directly, can not understand to see the previous package of database methods that 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 '];  The database operation is done here  if ($data [0]) {    $date [' img_path '] = $data [1];  }  Insert ($link, ' user ', $date);

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.