Php uploads images to a specified location and saves them to the database
Source: Internet
Author: User
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;
}
Echo "Upload failed! ";
}
Else
{// Preview the output image
Echo" You have uploaded the file and uploaded the image to preview it:
";
Echo"
Continue Upload ";
}
}
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.