PHP upload image to the specified location path save to database specific implementation _php Tutorial

Source: Internet
Author: User
Tags phpmyadmin
1.conn.php
Copy CodeThe 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 " Server Connection Failed!
Please refresh and try again. ";
return true;
}
$databasename = "database";//DB name

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

if ($con ==false)
{
echo " Failed to open database!
Please refresh and try again. ";
return true;
}

?>

2.upload.php
Copy CodeThe code is as follows:
if ($_get[' action '] = = "Save") {
Include_once (' conn.php ');
Include_once (' uploadclass.php ');
$title =$_post[' title ';
$pic = $uploadfile;
if ($title = = "")
echo "";
$sql = "INSERT into upload (title,pic) VALUES (' $title ', ' $pic ')";
$result =mysql_query ($sql, $conn);
echo "";
}
?>


<title>File Upload Instance</title>







3.uploadclass.php
Copy CodeThe code is as follows:
$uploaddir = "upfiles/";//Set File save directory note include/
$type =array ("JPG", "gif", "BMP", "JPEG", "PNG");//Set the type of file allowed to upload
$patch = "upload/";//path where the program is located

Get file suffix name function
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;
}

$a =strtolower (fileext ($_files[' file ' [' name ']));
Determine file type
if (!in_array (Strtolower (fileext ($_files[' file ' [' name '])), $type))
{
$text =implode (",", $type);
echo "You can only upload the following types of files:", $text, "
";
}
Generate file name for target files
else{
$filename =explode (".", $_files[' file ' [' name ']);
Do
{
$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 " Your file has been uploaded upload picture preview:
";
echo "
Continue uploading ";
}
}

}
?>

4. Database files
--PhpMyAdmin SQL Dump
--Version 2.9.1.1
--Http://www.phpmyadmin.net
--
--Host: localhost
--Date of generation: 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 is 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 ', ' upfiles/cr-v9ary46jos.jpg '),
(9, ' patriotism ', ' upfiles/cr-xyajendf1k.jpg ');

http://www.bkjia.com/PHPjc/633588.html www.bkjia.com true http://www.bkjia.com/PHPjc/633588.html techarticle 1.conn.php Copy Code as follows:? $host = "localhost";//database server name $user = "root";//username $pwd = "1721";//Password $conn =mysql_connect ($ Host, $user, $PWD); Mysql_qu ...

  • Related Article

    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.