简体   繁体   English

将图像上传到数据库的正确方法

[英]correct way to upload image to database

i know some of you are going to say that this isnt the correct way but im on a tight deadline to finish an application and as of now i cant go back and modify the code to store the images in a directory. 我知道你们中有些人会说这不是正确的方法,但是即时通讯在紧迫的期限内完成了一个应用程序,到目前为止,我无法返回并修改代码以将图像存储在目录中。

now that thats cleared 现在那已经清除

the question i had is i inserted an image into the database by typing this. 我的问题是我通过输入此图像将图像插入数据库。

(dont mind the class security call, all that is doing is a few checks if the data is valid) (不要介意类安全性调用,只需要检查一下数据是否有效即可)

$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);
$imgsize = $security->secure($_FILES['imgschool']['size']);
$imgtype = $security->secure($_FILES['imgschool']['type']);
$school = $security->secure($_POST['school']);


//begin upload
if($imgsize > 0) {
$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = addslashes($content);

//code to add all this to database
}

the variable $content is the image and all its getting is the addslashes. 变量$ content是图像,其所有获取内容都是加号。 i remember someone once mentioning to do it with something called base64 but i can barely recall how it was written. 我记得有人曾经提到要使用base64做它,但是我几乎记不起它是怎么写的。

this is how i am calling the image from the database 这就是我从数据库中调用图像的方式

aside from all the queries and whatnot this is the main part that is calling the image 除了所有查询之外,这是调用图像的主要部分

header("Content-length: ".$imgsize);
header("Content-type: ".$imgtype);
header("Content-Disposition: attachment; filename=".$imgname);
print $row['img'];

the problem i am having is that instead of the image showing. 我遇到的问题是,而不是图像显示。 the url is only showing, so in this case i only see this 该网址仅显示,因此在这种情况下,我只会看到此

http://localhost/admin/school-catalog.php?page=gallery&id=4 http://localhost/admin/school-catalog.php?page = gallery&id = 4

when opening the page to view the image with the correct params set in the url. 当打开页面以使用url中设置的正确参数查看图像时。


for those that wanted to see the query that is being done to save the image and so forth i copied the whole section 对于那些想查看正在执行的保存图像的查询,等等,我复制了整个部分

//save image to db
if(isset($_POST['btnupload'])) {

$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);
$imgsize = $security->secure($_FILES['imgschool']['size']);
$imgtype = $security->secure($_FILES['imgschool']['type']);
$school = $security->secure($_POST['school']);


//begin upload
if($imgsize > 0) {
$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = base64_encode($content);
}

$save = mysql_query("insert into tbl_schoolgallery(id,hash,img,imgtype,imgsize) values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());
header("Location: school-catalog.php?page=school_gallery");

}


//call image from db
$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {

$imgtypeget = explode("/", $row['imgtype']);

$imgname = "img.".$imgtypeget[1];
$imgtype = $row['imgtype'];
$imgsize = $row['imgsize'];

header("Content-length: ".$imgsize);
header("Content-type: ".$imgtype);
print base64_decode($row['img']);

print $row['img'];
}

Using addslashes is extremely incorrect. 使用addslashes是非常不正确。 Depending on whether your column is a TEXT field or a BLOB field, you should use Base64 or mysql_real_escape_string . 根据您的列是TEXT字段还是BLOB字段,应使用Base64或mysql_real_escape_string

Using Base64 isn't that hard; 使用Base64并不难; you may as well use that way. 您最好使用这种方式。 Just replace addslashes with base64_encode and echo the image with base64_decode . 只需更换addslashesbase64_encode和回声图像base64_decode

There's a bit easier way to write the whole thing, for that matter: 就此而言,有一种更简单的方法可以编写整个内容:

// begin upload
if ($imgsize > 0)
{
  $content = file_get_content($tmpname);
  $content = base64_encode($content);
}

And then to output you really only need to do 然后要输出,您真的只需要做

header("Content-type: ".$imgtype);
echo base64_decode($img);

If the column is a BLOB, however, you can directly use mysql_real_escape_string : 但是,如果该列是BLOB,则可以直接使用mysql_real_escape_string

// begin upload
if ($imgsize > 0)
{
  $content = file_get_content($tmpname);
  $content = mysql_real_escape_string($content);
}

And then: 接着:

header("Content-type: ".$imgtype);
echo $img;

Although judging from your current symptoms, I'm guessing you also have a bug relating to how your image is being stored and recalled from the database, and I'd need to see that part of the code where you make the queries to insert and read from the database before I could help you fix that part. 尽管从您当前的症状来看,但我猜您还存在一个与如何存储和从数据库调用图像有关的错误,并且我需要查看代码的那部分,以便您在其中插入和查询查询。从数据库中读取数据,然后我才能帮助您修复该部分。


Your current code seems mostly fine. 您当前的代码似乎还不错。 A few issues: 一些问题:

print base64_decode($row['img']);

print $row['img'];

You probably meant to get rid of the second row. 您可能打算摆脱第二行。 Also, you should use echo instead of print ; 另外,您应该使用echo而不是print everyone uses it, it can be slighty faster sometimes, and print doesn't really have any benefit other than returning a value: 每个人都使用它,有时它可能会稍微快一点,并且除了返回值外, print实际上没有任何好处:

echo base64_decode($row['img']);

$security->secure() appears to be some sort of sanitization function. $security->secure()似乎是某种清理功能。 Just use mysql_real_escape_string() - that's the one you're supposed to use. 只需使用mysql_real_escape_string() -这就是您应该使用的那个。 Except $imgsize ; 除了$imgsize ; you might want to use intval() on that one since you know it's supposed to be an integer. 您可能要在该对象上使用intval() ,因为您知道它应该是整数。

Also here: 也在这里:

$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error());

You name the table tbl_schoolgallery a few rows above that. 您将表tbl_schoolgallery命名tbl_schoolgallery上方几行。 I assume $tbl == 'tbl_schoolgallery' , but for consistency, you should either use $tbl in both places or tbl_schoolgallery in both places. 我假设$tbl == 'tbl_schoolgallery' ,但是为了保持一致,您应该在两个地方都使用$tbl或在两个地方都使用tbl_schoolgallery

Also, replace that while with an if - your code would cause trouble if it ever loops more than once, anyway. 此外,更换whileif -你的代码会造成麻烦,如果它曾经循环不止一次,反正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM