PHP upload pictures to the specified location path save to the specific implementation of the database _php skills

Source: Internet
Author: User
Tags phpmyadmin
1.conn.php
Copy Code code 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 = "database";//DB name

Todo
{
$con =mysql_select_db ($databasename, $conn);
}while (! $con);

if ($con ==false)
{
Echo <center> failed to open the database! <br> please refresh and try again. </center> ";
return true;
}

?>

2.upload.php
Copy Code code 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! The information you 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 successfully '); location.href= ' upload.php ' </Script> ';
}
?>
<title> File Upload Instance </title>
<body>
<form method= "POST" action= "Action=save" enctype= "Multipart/form-data" >
<table border=0 cellspacing=0 cellpadding=0 align=center width= "100%" >
<tr>
&LT;TD width=55 height=20 align= "center" > </TD>
&LT;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> Documents: </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>

3.uploadclass.php
Copy Code code as follows:

<?php
$uploaddir = "upfiles/";//settings File Save directory Note contains/
$type =array ("JPG", "gif", "BMP", "JPEG", "PNG");//Set the type of file allowed to be uploaded
$patch = "upload/";//program Path

Get file suffix name function
function Fileext ($filename)
{
Return substr (STRRCHR ($filename, '. '), 1);
}
Generate a 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;
}

$a =strtolower (fileext ($_files[' file ' [' name '])];
Determining File Types
if (Strtolower (Fileext ($_files[' file ' [' name ']), $type))!in_array
{
$text =implode (",", $type);
echo "You can only upload the following types of files:", $text, "<br>";
}
Generate file name for target file
else{
$filename =explode (".", $_files[' file '] [' name ']);
Todo
{
$filename [0]=random (10); Set the length of a random number
$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></ Center> ";
echo "<br><center><a href= ' upload.htm ' > continue to upload </a></center>";
}
}

}
?>

4. Database files
--phpMyAdmin SQL Dump
--Version 2.9.1.1
--Http://www.phpmyadmin.net
--
--Host: localhost
--Date Created: July 30, 2009 21:43
--Server version: 5.0.11
--PHP version: 5.1.1
--
--Databases: ' Database '
--

-- --------------------------------------------------------

--
--The structure of the table ' News '
--

CREATE TABLE ' News ' (
' id ' int (one) unsigned not NULL auto_increment,
' title ' varchar Not NULL,
' content ' varchar not NULL,
' Path ' text not NULL,
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 '),
(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 ');

-- --------------------------------------------------------

--
--The structure of the table ' upload '
--

CREATE TABLE ' upload ' (
' Uploadid ' int (one) unsigned not NULL auto_increment,
' title ' varchar Not NULL,
' pic ' varchar not NULL,
PRIMARY KEY (' Uploadid ')
) 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 '),
(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.