Home >php教程 >PHP源码 >PHP将图片存入mysql后再显示出来(支持多张)

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

PHP中文网
PHP中文网Original
2016-05-25 17:12:101232browse

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

<?php
$form_description = $_POST[&#39;form_description&#39;];  
$form_data_name = $_FILES[&#39;form_data&#39;][&#39;name&#39;];
$form_data_size = $_FILES[&#39;form_data&#39;][&#39;size&#39;];
$form_data_type = $_FILES[&#39;form_data&#39;][&#39;type&#39;];
$form_data = $_FILES[&#39;form_data&#39;][&#39;tmp_name&#39;];
//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 (&#39;$form_description&#39;,&#39;$data&#39;,&#39;$form_data_name&#39;,&#39;$form_data_size&#39;,&#39;$form_data_type&#39;)");  

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


MYSQL_CLOSE();
?>

2. [文件] get_data.php 

<?php  
if(isset($_GET[&#39;id&#39;])) {  
$id = $_GET[&#39;id&#39;];
$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[&#39;pic_id&#39;];
    $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=&#39;style:height:100px;width:100px;&#39; 
    	src=&#39;get_data.php?id=".$out[&#39;id&#39;]."&#39; onmouseover=&#39;see_big(".$out[&#39;id&#39;].")&#39; /><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:&#39;text&#39;,
			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)!


Statement:
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
Previous article:PHP 的单例模式Next article:阴历-农历-转换类