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