我正在尝试上传会员资料的图片,并使用php将其存储在数据库中,然后检索它,但它对我不起作用
这就是我尝试插入图像的方法:
<html>
<head>
<title> Upload an image </title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/from/data">
  File:
  <input type="file" name="image" > <input type="submit" value="upload">
</form>
</body>
</html>
<?php
  mysql_connect("localhost", "root","") or die ("could not connect to the server");
  mysql_select_db("project") or die ("that database could not be found");
  $file = $_FILES['image']['tmp_name'];
  if (!isset($file))
     echo "please select file";
  else
  {
     $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
     $image_name = addslashes($_FILES['image']['name']);
     $image_size = getimagesize($_FILES['image']['tmp_name']);
     if($image_size == FALSE)
        echo "that not image ";
     else
     {
         if (!$insert= mysql_query("INSERT INTO user_info (image) VALUES ('$image')"))
             echo "Problem";
         else
         {
             echo "image: <p /> your image <p /><img src='view.php?id="id"'>";
         }
     }
 }这个是用来检索图像的
<?php
   // do some validation here to ensure id is safe
   mysql_connect("localhost", "root","") or die ("could not connect to the server");
   mysql_select_db("project") or die ("that database could not be found");
   $id = addslashes($_REQUEST['id']);
   $image = mysql_query("SELECT * FROM user_info WHERE id='$id'");
   $image = mysql_fetch_assoc($image);
   $image = $image['image'];
   header("Content-type: image/jpeg");
   echo $image;
?>发布于 2013-05-05 16:28:58
我想你应该重新考虑一下你在做什么。
为什么要将文件存储在mysql数据库中?我的意思是这有什么意义呢?
文件系统是用于存储文件的专家数据库。所以最好不要把你的文件放在一个普通的数据库里,而是放在文件系统里。
图像可以在目录(0102003.gif)中使用数字命名,并可以通过mysql记录中的文件名进行寻址。
如果你真的需要在数据库中存储文件,mysql不是一个合适的工具。
看看mongoDB吧。
PS: mysql接口已弃用,请使用mysqli或PDO。
发布于 2013-05-05 16:35:27
要在php网站上存储图片,您只需将文件放在一个目录中,并将文件名保存在mySQL数据库中。祝你好运!
发布于 2014-02-24 13:56:12
试试这段代码,它会对你有帮助。
<?php
    mysql_connect("localhost", "root","") or die ("could not connect to the server");
    mysql_select_db("database1") or die ("that database could not be found");
    $file = $_FILES['image']['tmp_name'];
    $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);
    mysql_query("INSERT INTO table1 (id,image) VALUES ('1','{$image}')");        
?>https://stackoverflow.com/questions/16382672
复制相似问题