PHP Upload Image code (while the picture is saved to the database)

Source: Internet
Author: User
Tags ini
The code is as follows Copy Code

Connecting to a database
$conn = mysql_connect ("localhost", "phpdb", "phpdb");
mysql_select_db ("Test", $conn);
?>
?
Get the parameters of a Web page
$id =$_post[' id '];

Determine if the user name already exists
$checksql = "SELECT * from image where id= ' $id '";
$check _re=mysql_query ($checksql, $conn);
$num =mysql_num_rows ($check _re);
if ($num!=0) {
echo "<table width= ' 100% ' align=center><tr><td align=center>";
echo "This username already exists, please select another <br>";
echo "<font color=red> upload failed! </font><br><a href= ' web effects: history.back ( -1) ' > Return </a> ';
echo "</td></tr></table>";
Exit ();
}

Method Two: Save only the file name,
When you save the file name, the file is in the upload temporary directory set in the php.ini configuration file, which is the Upload_tmp_dir parameter

if ($photo <> "") {
if ($photo _type== "Image/pjpeg") or ($photo _type = = "Image/gif")) {
C:WINNT EMP Enables temporary directory of upload files set in php.ini configuration file
$photodir = "C:winnt emp/";
if (!) ( File_exists ($photo _name))) {
Copy the picture file to the Set upload file temp directory
Copy ($photo, $photodir. $photo _name);
}
}
else{
echo "<script Language=javascript>alert (' uploading photos can only be jpg or gif! '); History.go ( -1) </script><br> ";
echo "or <br>";
echo "FileName already exists, please change a filename for the picture";
Exit
}
}
else{
$photo _name= "";
}
$sql = "INSERT into image (ID, photo) VALUES (' $id ', ' $photo _name ')";

mysql_query ($sql, $conn) or Die ("Insert data failed:". Mysql_error ());

Close connection
Mysql_close ($conn);
Show upload Image Successful
Redirect to Registration Success page
Header ("location:display_image2.php?id= $id");

?>

Code two

<?php
Connecting to a database
$conn = mysql_connect ("localhost", "phpdb", "phpdb");
mysql_select_db ("Test", $conn);
?>
?
Get the parameters of a Web page
$id =$_post[' id '];

//To determine if the user name already exists
$checksql = "SELECT * from image where id= ' $id ';
$check _re=mysql_query ($checksql, $conn);
$num =mysql_num_rows ($check _re);
if ($num!=0) {
 echo "<table width= ' 100% ' align=center><tr><td align=center>";
 echo "This username already exists, please select another <br>";
    echo "<font color=red> upload failed! </font><br><a href= ' Javascript:history.back ( -1) ' > Return </a> ';
    echo "</td></tr></table>";
 exit ();
}
//Method One: Save the picture file in MySQL,
//If there is a picture file, open the picture file, the data in the picture file with the function
//addslashes processing, and then pass to the variable $data,
// The Addslashes function is to add a slash to the string so that the string can be successfully written to the database
//The $data of the picture file is saved in the variable,
if ($photo <> "") {
  $fp =fopen ( $photo, "R");
  $data =addslashes (fread ($fp, FileSize ($photo));
}
$password =md5 ($password);
$sql = INSERT INTO image (Id,photo) VALUES (' $id ', ' $data ');

mysql_query ($sql, $conn) or Die ("Insert data failed:". Mysql_error ());

Close connection
Mysql_close ($conn);
Show upload Image Successful
Redirect to Registration Success page
Header ("location:display_image1.php?id= $id");

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.