php upload images to the specified location path to save to the database specific implementation
- 2020-12-09 00:45:19
- OfStack
1.conn.php
2.upload.php
3.uploadclass.php
4. Database files
-- phpMyAdmin SQL Dump
-- version 2.9.1.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generated at 21:43 on 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 table data 'news'
--
INSERT INTO `news` (`id`, `title`, `content`, `path`) VALUES
(1, 'ag ', 'ag ', '1.html'),
(2, 'Hello 2',' Hello News 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 table data '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 boy', 'upfiles/CR - v9Ary46JoS. jpg'),
(9, 'patriotic ', 'upfiles/ ES142en-ES143en1jpg ');
<?
$host="localhost"; // Database server name
$user="root"; // The 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
<?php
if ($_GET['action'] == "save"){
include_once('conn.php');
include_once('uploadclass.php');
$title=$_POST['title'];
$pic=$uploadfile;
if($title == "")
echo"<Script>window.alert(' I'm 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=" browse " >
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
</label></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value=" on the " name="upload"></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
3.uploadclass.php
<?php
$uploaddir = "upfiles/";// Set the file save directory Note that contain /
$type=array("jpg","gif","bmp","jpeg","png");// Sets the type of file allowed to be uploaded
$patch="upload/";// Program path
// Gets the file suffix name function
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
// Generate the random filename 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,"<br>";
}
// Generate the filename 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 picture preview
echo "<center> Your file has been uploaded Preview of uploaded photos : </center><br><center><img src='$uploadfile'></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
-- Generated at 21:43 on 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 table data 'news'
--
INSERT INTO `news` (`id`, `title`, `content`, `path`) VALUES
(1, 'ag ', 'ag ', '1.html'),
(2, 'Hello 2',' Hello News 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 table data '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 boy', 'upfiles/CR - v9Ary46JoS. jpg'),
(9, 'patriotic ', 'upfiles/ ES142en-ES143en1jpg ');