PHP upload picture to the specified location path save to database __ Database

Source: Internet
Author: User
1.conn.php
?
$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";//DB name

Todo
{
$con =mysql_select_db ($databasename, $conn);
}while (! $con);

if ($con ==false)
{
Echo <center> failed to open the database. <br> please refresh and try again. </center> ";
return true;
}

?>

2.upload.php

<?php
if ($_get[' act ion '] = = "Save") {
include_on CE (' conn.php ');
include_on CE (' uploadclass.php ');
$title =$_post[' title '];
$pic = $uploadfile;
if ($title = = "")
echo "<script>window.alert" (' Sorry. The information you entered is not complete! '); 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> ';
}
?>
<title> File Upload Instance </title>
<body>
<form method= "POST" act ion= "Act ion=save" enctype= "Multipart/form-da ta" >
<table border=0 cellspacing=0 cellpadding=0 align=center width= "100%" >
<tr>
&LT;TD width=55 height=20 align= "center" >&nbsp;</TD>
&LT;TD height= ">"

<table width= "48%" height= "border=" "0" cellpadding= "0" cellspacing= "0" >
<tr>
<td> title:</td>
<td><input name= "title" type= "text" id= "title" ></td>
</tr>
<tr>
<td> Documents: </td>
<td><label>
<input name= "File" type= "file" value= "Browse" >
<input type= "hidden" name= "max_file_size" value= "2000000" >
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type= "Submit" value= "Upload" name= "upload" ></td>
</tr>
</table></td>
</tr>
</table>
</form>

</body>
3.uploadclass.php
<?php
$uploaddir = "upfiles/";//settings File Save directory Note contains/
$type =array ("JPG", "gif", "BMP", "JPEG", "PNG");//Set the type of file allowed to be uploaded
$patch = "upload/";//program Path

Get 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 '])];
Determining File Types
if (Strtolower (Fileext ($_files[' file ' [' name ']), $type))!in_array
{
$text =implode (",", $type);
echo "You can only upload the following types of files:", $text, "<br>";
}
Generate file name for target file
else{
$filename =explode (".", $_files[' file '] [' name ']);
Todo
{

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.