To implement PHP upload pictures to the specified location path to save to the database

Source: Internet
Author: User
Tags file upload hash phpmyadmin

  This article for you to introduce PHP upload pictures to the specified location path to save the specific implementation of the database, interested friends do not miss

1.conn.php  code is as follows: <?  $host = "localhost"; Database server name   $user = "root"; User name   $PWD = "1721"; Password   $conn =mysql_connect ($host, $user, $pwd);  mysql_query ("set  character_set_connection=gb2312,   character_set_results=gb2312,  character_set_client=binary ", $conn);    if ($conn ==false)   {  echo <center> server connection failed! <br> please refresh and try again. </center> ";  return true; }  $databasename =" db ";//Database name     do  {  $con =mysql_select_db ($databasename, $conn); }while (! $con);    if ($con ==false)   {  echo "< Center> failed to open database! <br> please refresh and try again. </center> ";  return true; }   ?>    2.upload.php  code is as follows: <?php  if ( $_get[' action '] = = "Save") {  include_once (' conn.php ');  include_once (' uploadclass.php ');  $title =$_ post[' title '];  $pic = $uploadfile;  if ($title = = "")   echo <script>window.alert (' Sorry! YouThe information entered is not complete! '); History.back () </Script> ";  $sql =" INSERT into upload (title,pic) VALUES (' $title ', ' $pic ');  $result = mysql_query ($sql, $conn); //echo "<script>window.alert (' information added success '); location.href= ' upload.php ' </ Script> "; } ?>  <html>  <head>  <title> File Upload instance </title>   </head>  <body>  <form method= "POST" action= "Action=save" enctype= "multipart/ Form-data ">  <table border=0 cellspacing=0 cellpadding=0 align=center width=" 100% ">  <tr>   <TD width=55 height=20 align= "center" > </TD>  <td height= "" >    <table Width= "48%" height= "border=" 0 "cellpadding=" 0 "cellspacing=" 0 ">  <tr>  <td> title: </td >  <td><input name= "title" type= "text" id= "title" ></td>  </tr>  <tr>   <td> files: </td>  <td><label>  <input name= "filE "type=" file "value=" browse ">  <input type=" hidden "name=" max_file_size "value=" 2000000 ">  </label ></td>  </tr>  <tr>  <td> </td>  <td><input type= " Submit "value=" Upload "name=" upload "></td>  </tr>  </table></td>  </tr>   </table>  </form>    </body>  </html>    3. The uploadclass.php  code is as follows: <?php  $uploaddir = "upfiles/";//settings File Save directory Note contains/  $type =array ("JPG", "gif", " BMP "," JPEG "," PNG ");/set the type to allow uploading of files   $patch =" upload/";//program path    //Get file suffix name functions   function Fileext ($ FileName)   {  return substr (STRRCHR ($filename, '. '), 1); } //Generate random file name function   function random ($ Length)   {  $hash = ' cr-';  $chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';   $max = strlen ($chars)-1;  Mt_srand (double) microtime () * 1000000);  For ($i = 0; $i < $length; $i + +)   {  $hash. = $chars [Mt_rand (0, $max)]; }  return $hash; }&nbs P   $a =strtolower (fileext ($_files[' file '] [' name ']); //Judge file type   if!in_array (strtolower files[' file ' [' name ']])   {  $text =implode (",", $type);  echo "You can only upload the following types of files:", $text, "<br > "; } //Generate target file file name   else{  $filename =explode (". ", $_files[' file '] [' name ']);  do  {   $filename [0]=random (10); Set random number length   $name =implode (".", $filename); //$name 1= $name. ". MCNCC ";  $uploadfile = $uploaddir. $name; }    while (file_exists ($uploadfile));    if ( Move_uploaded_file ($_files[' file '] [' tmp_name '], $uploadfile))   {  if (is_uploaded_file ($_files[' file '] [' Tmp_name '])   {    echo "Upload failed!";  }  else  {//Output picture preview   echo "<center> your files have been uploaded upload picture preview: </center><br><center ><img src= ' $uploadfile ' ></center> ";   echo "<br><center><a href= ' upload.htm ' > continue to upload </a></center>"; } }    } ?>    4. Database file  --phpMyAdmin SQL dump --Version 2.9.1.1 --http://www .phpmyadmin.net -- --Host: localhost --Build Date: July 30, 2009 21:43 --Server version: 5.0.11 --PHP Version: 5.1.1 -- --databases: ' Database '  --   ---------------------------------------------------- ------   -- --table structure ' news '  --    CREATE table ' News ' (  ' id ' int (one) unsigned not NU LL auto_increment,  ' title ' varchar not null,  ' content ' varchar (m) not null,  ' path ' text not NULL,&N Bsp PRIMARY KEY (' id ')   engine=innodb DEFAULT charset=gb2312 auto_increment=8;   -- --Export the data in the table ' News '  --    INSERT into ' news ' (' id ', ' title ', ' content ', ' path ') values  (1, ' AG ', ' AG ', ' 1.html '), &nbs P (2, ' Hello 2 ', ' Hello, news content 2 ', ' 2. html '),  (3, ', ', ' 3.html '),  (4, ' GA ', ' AG ', ' 4.html '),  (5, ' AG ', ' AG ', ' 1.html '),  (6, ' AG ', ' AG ', ' 2.html '),  (7, ' AG ', ' AG ', ' 3.html ');   ---------------------------------------------------- ------   -- --table's structure ' upload '  --    CREATE table ' upload ' (  ' uploadid ' int (one) unsig Ned not NULL auto_increment,  ' title ' varchar (m) not null,  ' pic ' varchar (+) not null,  PRIMARY KEY (' UPL Oadid ')  ) Engine=innodb DEFAULT charset=gb2312 auto_increment=10;   -- --Export the data in the table ' upload '  - -    INSERT into ' upload ' (' uploadid ', ' title ', ' pic ') values  (1, ' ga ', ' upfiles/cr-xpvdk12dsi.jpg '), &nbs P (2, ' GA ', ' upfiles/cr-hkc6fbfatw.jpg '),  (3, ' hello ', ' upfiles/cr-e8ohxk6kxb.jpg '),  (4, ', '),  (5, ', ') ),  (6, ', ' upfiles/cr-92xaug6jhu.jpg '),  (7, ', ' upfiles/cr-a7niazlhfw.jpg '),  (8, ' Handsome Man ', ' upfiles/ Cr-v9ary46jos.jpg '), (9, ' patriotic ', ' upfiles/cr-xyajendf1k.jpg ');   

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.