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

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

PHP中文网
Release: 2016-05-25 17:12:10
Original
1278 people have browsed it

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();
?>
Copy after login

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"]; 
}
?>
Copy after login

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>
Copy after login

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


Related labels:
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
Latest Articles by Author
Latest Issues
php - mysql monitoring problem
From 1970-01-01 08:00:00
0
0
0
PHP toolbox mysql cannot start
From 1970-01-01 08:00:00
0
0
0
How to upgrade mysql in php toolbox
From 1970-01-01 08:00:00
0
0
0
Can the php toolbox change the mysql version?
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template