如何从我的数据库中显示图像

时间:2014-12-29 10:17:19

标签: php image

这是我尝试过的另一堆代码:

form.html

<form method='post' action='upload2.php' enctype='multipart/form-data'>
Select file :<input type='file' name='filename' size='10' />
<input type='submit' value='upload'>

upload.php的

  <?include "connect.php";
   $name = $_FILES['filename']['name'];
   $image = $_FILES['filename']['tmp_name'];

   $sql = "INSERT INTO files ". "VALUES ('', '$name', '$image')";

   mysql_query($sql) or die(mysql_error());?> 

Display.php的

 <?php
  include "connect.php";
  $fid = $_FILES['fid'];

  // do some validation here to ensure id is safe
  $sql = "SELECT * FROM files WHERE fid='$fid'";
  $result = mysql_query($sql) or die(mysql_error());
  $row = mysql_fetch_assoc($result);
  $image = $row['content'];

  echo $image;?>

这里我调用display.php来查看文件:

  <?
  echo "<img src=display.php?fid=1  />";
  ?>

它只显示空白页

在我忘记之前我已经忘了..这是我的表:

 CREATE TABLE `files` ( `fid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT
 'unique id', `name` VARCHAR( 30 ) NOT NULL COMMENT 'file name', `content` BLOB NOT
  NULL COMMENT 'actual file' ) ENGINE = MYISAM COMMENT = 'Uploaded files' 

这次只是一个空白页面 图像成功保存在数据库中但我无法在页面中显示它。我的错误...我已经通过这里查看了解决方案,但我仍然无法使其工作..它要么不显示任何内容,要么显示损坏的图像

3 个答案:

答案 0 :(得分:0)

您可以将图像插入mysql数据库

$image = addslashes(file_get_contents($_FILES['filename']['tmp_name']));
$sql = "INSERT INTO files ". "VALUES ('', '$name', '$image')";

从表中获取图片并使用<img src>

嵌入页面
$sql = "SELECT * FROM files WHERE fid='$fid'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$image = $row['content'];

echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';

PS。未推荐使用mysql_ *函数。相反,您可以使用mysqli_ *或pdo_ *函数。

答案 1 :(得分:0)

感谢大家的帮助......现在我明白了我的错误..问题是我的文件没有转移到.. @madforstrength感谢链接 the tutorial

首先是form.html

  <!DOCTYPE html>
  <html>
  <body>

  <form action="upload.php" method="post" enctype="multipart/form-data">
   Select image to upload:
   <input type="file" name="fileToUpload" id="fileToUpload">
   <input type="submit" value="Upload Image" name="submit">
   </form>

   </body>
   </html> 

和上传脚本

 <?php
 $target_dir = "uploads/";
 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been
uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}?> 

这段代码我提供了设置的文件夹将文件保存到

$target_dir = "uploads/";

现在只是尝试在几个部分中应用数据库信息 显示需要写的文件,     接近这个的东西

谢谢大家

答案 2 :(得分:-1)

您需要将图像文件从临时文件夹移动到主图像目录,然后从那里提供服务。

可以找到一个完整的示例here