Home > Backend Development > PHP Tutorial > PHP image upload code (save the image to the database at the same time)_PHP tutorial

PHP image upload code (save the image to the database at the same time)_PHP tutorial

WBOY
Release: 2016-07-13 10:58:03
Original
1858 people have browsed it

PHP upload image code (save the image to the database at the same time) /* The PHP image upload code provided below uses PHP copy to upload files. It can not only upload the image to the server, but also save the image address to the MySQL database.

PHP tutorial to upload image code (and save image to database tutorial)
/*
The PHP image upload code provided below uses PHP copy to upload files. It can not only upload the image to the server, but also save the image address to the MySQL tutorial database.
*/

//Connect to database
$conn = mysql_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$conn);
?>
// Get the parameters of the web page
$id=$_post['id'];

// Determine whether the username 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 username already exists, please choose another
";
echo "Upload failed!
Return";
echo "
";
exit();
}

//Method 2: Save only the file name,
// When saving the file name, the file is in the upload temporary directory set in the php.ini configuration file, that is, in the upload_tmp_dir parameter

if ($photo<>""){
if (($photo_type== "image/pjpeg")or($photo_type == "image/gif")){
// c:winnttemp makes the temporary directory of the upload file set in the php.ini configuration file
$photodir="c:winnttemp/";
if(!(file_exists($photo_name))){
//Copy the image file to the set temporary directory for uploaded files
       copy($photo,$photodir.$photo_name);
}
}
else{
echo "
";
echo "or
";
echo "The file name already exists, please change the 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 ("Failed to insert data: ".mysql_error());

//Close connection
mysql_close($conn);
// Display uploaded image successfully
//Redirect to registration success page
header("location:display_image2.php?id=$id");

?>

Code 2

//Connect to database
$conn = mysql_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$conn);
?>
// Get the parameters of the web page
$id=$_post['id'];

// Determine whether the username 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 username already exists, please choose another
";
echo "Upload failed!
Return";
echo "
";
exit();
}
//Method 1: Save image files in mysql,
// If there is an image file, open the image file and use the function
to use the data in the image file // Addslashes are processed and then passed to the variable $data,
// The addslashes function adds slashes to the string so that the string can be written to the database smoothly
// In this way, the data stored in the variable $data is the data of the image 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 ("Failed to insert data: ".mysql_error());

//Close connection
mysql_close($conn);
// Display uploaded image successfully
//Redirect to registration success page
header("location:display_image1.php?id=$id");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632037.htmlTechArticlephp upload image code (while saving the image to the database) /*The php image upload code provided below is an application Use php copy to upload files. Not only can it upload images to the server,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template