我想以与 twitter 上相同的方式在背景上显示图像(通过使用 CSS 重复),但问题是我从 MySql 数据库中检索它并且 CSS 无法处理背景上的 src 标记,我使用了以下代码和链接
body{
background:url(<?php $lastid=$_SESSION['lastid']; echo "get_test.php?id=$lastid";?>) repeat;
}
http://www.webdeveloper.com/forum/showthread.php?210964-Set-CSS-Background-Image-with-PHP
http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/
我在主站点上的整个代码
<html>
<head>
<?php
$temp = tmpfile();
?>
<style>
body{
background:url(<?php $lastid=$_SESSION['lastid']; echo "get_test.php?id=$lastid"; ?>) repeat;
}
</style>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
File:<br>
<input type="file" name="image">
<input type="submit" name="submit" value="Upload">
</form>
<?php
mysql_connect("host", "nam", "database") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$file= $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = addslashes(getimagesize($_FILES['image']['tmp_name']));
if($image_size==FALSE)
{
echo "thats not an image";
}
else
{
mysql_query("INSERT INTO image_store VALUES ('','$image_name','$image')") or die(mysql_error());
$lastid=mysql_insert_id();
$_SESSION['lastid']=$lastid;
echo "the image is"."<image src=get_test.php?id=$lastid>";
}
?>
</body>
get_test.php 上的代码是
<?php
mysql_connect("host", "nam", "database") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image=mysql_query("SELECT * FROM image_store WHERE id=$id ");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-Type: image/jpeg");
echo $image;
?>