PHP Upload Image code (while picture saved to database) _php Tutorial

Source: Internet
Author: User
PHP Upload the image code (while the picture is saved to the database)/* The PHP upload image code provided below is a PHP copy to upload the file, he can not only upload the image to the server, but also the image of the address saved in the MySQL database Oh.

PHP Tutorial upload Image code (while the picture is saved to the database tutorial)
/*
The following PHP upload image code is a use of PHP copy to upload files, he can not only upload the image to the server, but also the image of the address saved to the MySQL tutorial database inside OH.
*/

Connecting to a database
$conn = mysql_connect ("localhost", "phpdb", "phpdb");
mysql_select_db ("Test", $conn);
?>
Get parameters for Web pages
$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 "

";
echo "The user name already exists, please select another
";
echo "Upload failed!
Return ";
echo "
";
Exit ();
}

Method Two: Save only the file name,
When you save the file name, the files are set in the upload temp directory 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:winnttemp make a temporary directory of upload files that are set in the php.ini configuration file
$photodir = "c:winnttemp/";
if (! ( File_exists ($photo _name))) {
Copy the picture file to the Set upload file temp directory
Copy ($photo, $photodir. $photo _name);
}
}
else{
echo "
";
echo "or
";
echo "file name already exists, please change a file name 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 picture Successful
Redirect to Registration Success page
Header ("location:display_image2.php?id= $id");

?>

Code two

Connecting to a database
$conn = mysql_connect ("localhost", "phpdb", "phpdb");
mysql_select_db ("Test", $conn);
?>
Get parameters for Web pages
$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 "

";
echo "The user name already exists, please select another
";
echo "Upload failed!
Return ";
echo "
";
Exit ();
}
Method One: Save the picture file in MySQL,
If you have a picture file, open the picture file and use the data in the image file function
Addslashes is processed and then passed 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
So the variable $data is the data of the picture file.
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 picture Successful
Redirect to Registration Success page
Header ("location:display_image1.php?id= $id");

http://www.bkjia.com/PHPjc/632037.html www.bkjia.com true http://www.bkjia.com/PHPjc/632037.html techarticle PHP upload Image code (while the picture is saved to the database)/* The PHP upload image code provided below is a PHP copy to upload files, he can not only upload pictures to the server, ...

  • 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.