使用PHP将Image上传到MYSQL DB

时间:2012-01-25 22:56:55

标签: php mysql database

还有一个我坚持的PHP问题,所以这就是我想要做的和我的代码:

第一步 - 尝试将名称,图像和ID上传到SQL表。该表名为second,有4个字段。我在另一个名为admin的表中使用其中一个字段作为FK,而FK是id。

什么有效? * 当我点击提交按钮时,图片的链接会上传,但实际上我的文件夹中没有图像。当我尝试上传表单时,我想使用用户的ID并将其放入新表中,以便我可以引用该字段来显示数据。我觉得这是一个很难解释 *

的场景

在此处编辑 - 发现Id字段,favname字段没有记录任何信息,链接没有正确上传到文件夹。

任何帮助都会非常感激:到此为止。

SecondPic.php

<?php
include "common.php";
$secondid = $_GET['id'];
DBConnect();



$Link = mysql_connect($Host, $User, $Password);

//This is the directory where images will be saved 
 $target = "second/"; 
 $target = $target . basename( $_FILES['photo1']['name']); 


$favname = $_POST["name1"];
$pic2=($_FILES['photo1']['name']); 
$id = $_POST["$formID"];



$Query ="INSERT into $Table_2 values ('0', '$id', '$favname', '$pic2')";

if (mysql_db_query ($DBName, $Query, $Link)){
print ("A record was created <br><a href=index.php> return to index </a>\n");

 // Connects to your Database 
 //mysql_connect("localhost", "jonathon_admin", "hello123") or die(mysql_error()) ; 
 //mysql_select_db("jonathon_admin1") or die(mysql_error()) ; 


 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 


} else {

print (" - Your Record was not created");   
}

mysql_close($Link);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

SecondUpload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="secondPic.php">
  <p>
    <label for="name1">Fav Location Name: </label>
  <input type="text" name="fav1" id="fav1" />
  </p>
  <p>
  <label for="photo1">Fav Location Photo: </label>
 <input type="file" name="photo1"><br> 
  </p>
  <p>
  <label for="formID">ID: <? echo $rows['id']; ?> </label>
  <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>



</body>
</html>

3 个答案:

答案 0 :(得分:1)

之前使用过这个,我最大的建议是不要在数据库中存储图像(或任何二进制数据)。它导致许多问题,将来会引起问题。相反,我建议在本地存储文件并保持对象的相对路径。

答案 1 :(得分:1)

检查这些关于MYSQL存储图像的线程是否为BLOB。

To Do or Not to Do: Store Images in a Database

Insert Blobs in MySql databases with php

一般建议是:不要将图像存储在MySQL中,因为增长难以管理,性能可能会降低。此外,动态使用您的图像需要您将其转换回来,但需要支付额外费用。没有比你自己的服务器操作系统更好,更便宜的文件处理程序。

答案 2 :(得分:0)

Aftr看完你的帖子,标题误导了我(可能是上面的那个人)认为你想将图像数据直接存储到数据库中。

这不是你想要做的。

在我们继续之前,以下是我的问题。

$target = "second/"

这个脚本是“SecondPic.php”在基础文件夹中吗?和同一个基本文件夹中的文件夹“second”?

通常最好在move_uploaded_file()中给出一个绝对路径,所以$ target应该是这样的:

$target = dirname(__FILE__) . DIRECTORY_SEPARATOR . $_FILES['photo1']['name'];

其次,最好在保存到数据库之前移动文件,因为移动文件的错误概率更高。

第三,有任何错误吗?在日志或页面输出?