This article introduces how php uploads images to a specified path and saves them to a database. if you are interested, do not miss 1. conn. php.
The 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"
An error occurred while connecting to the server!
Please refresh and try again. ";
Return true;
}
$ Databasename = "database"; // database name
Do
{
$ Con = mysql_select_db ($ databasename, $ conn );
} While (! $ Con );
If ($ con = FALSE)
{
Echo" An error occurred while opening the database!
Please refresh and try again. ";
Return true;
}
?>
2. upload. php
The 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 "";
}
?>
File upload instance
3. uploadclass. php
The code is as follows:
$ Uploaddir = "upfiles/"; // Set the file storage directory to include/
$ Type = array ("jpg", "gif", "bmp", "jpeg", "png"); // you can specify the object type that can be uploaded.
$ Patch = "upload/"; // path of the program
// Obtain the file suffix function
Function fileext ($ filename)
{
Return substr (strrchr ($ filename, '.'), 1 );
}
// Generate a random file name function
Function random ($ length)
{
$ Hash = 'cr -';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ 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 the 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 the object name
Else {
$ Filename = explode (".", $ _ FILES ['file'] ['name']);
Do
{
$ Filename [0] = random (10); // you can specify the length of a random number.
$ Name = implode (".", $ filename );
// $ Name1 = $ 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
{// Preview the output image
Echo"
You have uploaded the file and uploaded the image to preview it:
";
Echo"
Continue Upload ";
}
}
}
?>
4. database files
-- PhpMyAdmin SQL Dump
-- Version 2.9.1.1
Http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation date: July 30, 2009
-- Server version: 5.0.11
-- PHP version: 5.1.1
--
-- Database: 'database'
--
----------------------------------------------------------
--
-- Table structure 'news'
--
Create table 'news '(
'Id' int (11) unsigned not null auto_increment,
'Title' varchar (50) not null,
'Content' varchar (50) not null,
'Path' text not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = gb2312 AUTO_INCREMENT = 8;
--
-- Export the data 'news' in the table'
--
Insert into 'news' ('id', 'title', 'content', 'PATH') VALUES
(1, 'AG', 'AG', '1.html '),
(2, 'Hello 2', 'Hello, news content2', '2.html '),
(3, '','', '3.html '),
(4, 'Gal', 'AG', '4.html '),
(5, 'AG', 'AG', '1.html '),
(6, 'AG', 'AG', '2.html '),
(7, 'AG', 'AG', '3.html ');
----------------------------------------------------------
--
-- Table structure 'upload'
--
Create table 'upload '(
'Uploadid' int (11) unsigned not null auto_increment,
'Title' varchar (50) not null,
'Pic 'varchar (50) not null,
Primary key ('uploadid ')
) ENGINE = InnoDB default charset = gb2312 AUTO_INCREMENT = 10;
--
-- Export the data 'upload' in the table'
--
Insert into 'upload' ('uploadid', 'title', 'Pic ') VALUES
(1, 'ga ', 'upfiles/CR-xpvDk12dsI.jpg '),
(2, 'ga ', 'upfiles/CR-HkC6fBfAtW.jpg '),
(3, '', 'upfiles/CR-E8Ohxk6KxB.jpg '),
(4 ,'',''),
(5 ,'',''),
(6, ''', 'upfiles/CR-92xaug6jhu.jpg '),
(7, ''', 'upfiles/CR-a7niAZlhfW.jpg '),
(8, 'handsome guy ', 'upfiles/CR-v9Ary46JoS.jpg '),
(9, 'patriotic ', 'upfiles/CR-XyaJEndF1K.jpg ');