欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP将图片存入mysql后再显示出来(支持多张)

程序员文章站 2022-04-10 12:20:53
...
PHP将图片存入mysql后再显示出来(支持多张)
<?php
$form_description = $_POST['form_description'];  
$form_data_name = $_FILES['form_data']['name'];
$form_data_size = $_FILES['form_data']['size'];
$form_data_type = $_FILES['form_data']['type'];
$form_data = $_FILES['form_data']['tmp_name'];
//echo "winson";
// ßB½Óµ½ÙYÁÏŽì  
$connect = MYSQL_CONNECT( "localhost", "root", "admin") or die("Unable to connect to MySQL server");
mysql_select_db("blogsystem") or die("Unable to select database");

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

//echo "mysqlPicture=".$data;

$result=MYSQL_QUERY( "INSERT INTO ccs_image (description,bin_data,filename,filesize,filetype) 
VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");  

$id= mysql_insert_id();  
print "<p>This file has the following Database ID: <a href='get_data.php?id=$id'><b>$id</b></a>";


MYSQL_CLOSE();
?>

2. [文件] get_data.php

<?php  
if(isset($_GET['id'])) {  
$id = $_GET['id'];
$connect = MYSQL_CONNECT("localhost", "root", "admin") or die("Unable to connect to MySQL server");
mysql_select_db("blogsystem") or die("Unable to select database");

$query = "select bin_data,filetype from ccs_image where id=$id";  
$result = @MYSQL_QUERY($query);  

$out=mysql_fetch_array($result);
$data=$out["bin_data"];
$type=$out["filetype"];

Header( "Content-type: $type");  
echo $data;  
}
if(isset($_GET["pic_id"]))
{
	$pic_id = $_GET['pic_id'];
    $connect = MYSQL_CONNECT("localhost", "root", "admin") or die("Unable to connect to MySQL server");
    mysql_select_db("blogsystem") or die("Unable to select database");

    $query = "select * from ccs_image where id=$pic_id";  
    $result = @MYSQL_QUERY($query);
    $out=mysql_fetch_array($result);
    echo $out["id"]; 
}
?>

3. [文件] new_file.php

<?php
    $connect = MYSQL_CONNECT( "localhost", "root", "admin") or die("Unable to connect to MySQL server");
    mysql_select_db("blogsystem") or die("Unable to select database");
    $sql="select * from ccs_image";
    $result=mysql_query($sql) or die(mysql_error());
    while($out=@mysql_fetch_array($result))
    {
    	echo "<img style='style:height:100px;width:100px;' 
    	src='get_data.php?id=".$out['id']."' onmouseover='see_big(".$out['id'].")' /><br/>";
    }
?>
<script src="jquery.js?1.1.6"></script>
<script type="text/javascript">
$(document).ready(function(){

});
	function see_big(id)
	{
		var pic_id=id;
		$.ajax({
			type: "GET",
			data: "pic_id="+pic_id,
       		url: "get_data.php",
			dataType:'text',
			success:function(data)
			{
				$("#big_pic img").attr("src","get_data.php?id="+data);
			}
		})
	}
</script>
<p id="big_pic" style="height: 500px;width: 600px;position: absolute;margin-left: 200px;margin-top: -550px;">
<img /></p>
<center>
<form method="post" action="form.php" enctype="multipart/form-data">  
File Description:  
<input type="text" name="form_description" size="40">  
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000"> <br>
File to upload/store in database:  
<input type="file" name="form_data" size="40">  
<p><input type="submit" name="submit" value="submit">  
</form>  
</center>

以上就是PHP将图片存入mysql后再显示出来(支持多张)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关标签: PHP,mysql,图片