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 "<center> server connection failed! <Br> Please refresh and try again. </Center> ";
Return true;
}
$ Databasename = "database"; // 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
Copy codeThe 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! The information you entered is incomplete! '); 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> ";
}
?>
<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 = "16">
<Table width = "48%" height = "93" border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
<Td> title: </td>
<Td> <input name = "title" type = "text" id = "title"> </td>
</Tr>
<Tr>
<Td> file: </td>
<Td> <label>
<Input name = "file" type = "file" value = "">
<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. uploadclass. php
Copy codeThe Code is as follows:
<? Php
$ 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, "<br> ";
}
// 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 "<center> your file has been uploaded. Upload image preview: </center> <br> <center> </center> ";
Echo "<br> <center> <a href0000'upload.htm '> continue upload </a> </center> ";
}
}
}
?>
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 ');