1.conn.php
Copy code 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 "
Server connection failed!
Please refresh and try again.";
return true;
}
$databasename="database" ;//Database name
do
{
$con=mysql_select_db($databasename,$conn);
}while(!$con);
if ( $con==FALSE)
{
echo "Failed to open the database!
Please refresh and try again.";
return true;
}
?>
2.upload.php
Copy code 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"<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>";
}
?>
File upload example < ;/head>
3.uploadclass.php
Copy Code The code is as follows:
$uploaddir = "upfiles/";//Set the file saving directory and pay attention to include/
$type=array("jpg","gif","bmp", "jpeg","png");//Set the type of file allowed to be uploaded
$patch="upload/";//The path where the program is located
//Get the 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']));
//Judge 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 file name of the target file
else{
$filename =explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10); //Set the random number length
$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
{//Output image preview
echo "
Your file has been uploaded. Upload image preview: ";
echo "
Continue uploading";
}
}
}
?>
4. Database file
-- phpMyAdmin SQL Dump
-- version 2.9.1.1
-- http://www.phpmyadmin .net
--
-- Host: localhost
-- Generated date: July 30, 2009 21:43
-- 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 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');
------------------------------------ ---------------------
--
-- 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 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 guy', 'upfiles/CR-v9Ary46JoS.jpg'),
(9, 'patriotic', 'upfiles/CR-XyaJEndF1K.jpg');
http://www.bkjia.com/PHPjc/633588.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633588.htmlTechArticle1.conn.php Copy the code as follows: ? $host="localhost"; //Database server name$ user="root"; //Username $pwd="1721"; //Password $conn=mysql_connect($host,$user,$pwd); mysql_qu...