【发布时间】:2012-01-30 20:45:01
【问题描述】:
** 已解决 **
我的代码有点问题
我必须使用 php 和 HTML 表单将图像存储到 mysql 数据库中。 我遵循了指南,但我并没有真正工作。它将文件上传到服务器......然后它被放弃......至少没有存储。
对于代码...文本和变量的语言是...荷兰语如果您对此部分有疑问,很乐意提供帮助。
目标表的数据库布局为:
Fotos
fotonr int(10) Index number
album varchar(20) Grouping catagory(not needed)
Lid int(4) Index number of the member that placed it
type varchar(20) To store what kind of image it is
image blob The image itself
为此我使用以下段(数据库链接不在文件中,之前已经构建)
<p>
<?php
$target_path= "images/";
echo "checking file upload!<br />";
if(isset($_FILES['file']))
{
echo"SET!!!<br />";
if(isset($_POST['album']))
{
$album=trim($_POST['album']);
if($album!="")
{
$album= stripslashes($album);
}
else $album="Niet Ingedeeld/";
}
else $album="Niet Ingedeeld/";
$myalbum=mysql_real_escape_string($album);
$target_path=$target_path.$album;
foreach($_FILES['file']['tmp_name'] as $key=>$afbeelding)
{
echo $_FILES['file']['name'][$key]."<br />";
if ($_FILES['file']['tmp_name'][$key]!=''){
$size = getimagesize($_FILES['file']['tmp_name'][$key]);
$width=$size[0];
$height=$size[1];
echo "groote: ".$width."x".$height."<br />";
if($width>800&&$height>600)
{
echo "Uw afbeelding is te groot!(maximaal 800x600)<br />";
}
else
{
$mynr= mysql_real_escape_string($_SESSION['nummer']);
/*$type=exif_imagetype($_FILES['file']['tmp_name'][$key]);*/
$type=$size[mime];
echo 'Het type was:'.$type.'<br /> ';
if($type=="image/gif" ||$type=="image/jpeg" ||$type=="image/bmp" ||$type=="image/png" ||$type=="image/gif" ){
$mytype =mysql_real_escape_string($type);
$tmpName = $_FILES['file']['tmp_name'][$key];
/*$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$mycontent = mysql_real_escape_string($content);*/
$content = file_get_contents($tmpName);
$data = unpack("H*hex", $content );
$mycontent = '0x'.$data['hex'];
$sql="INSERT INTO`nacverk`.`Foto` (`album` , `lid` ,`image`,`type` )VALUES ('$myalbum' ,'$mynr','$mycontent', '$mytype')";
$result=mysql_query($sql);
/*fclose($fp);*/
if(!$result)
{
echo "<h1>Bestand uploaden mislukt!<br /> ".mysql_error()."<br /></h1>";
}
else
{
echo "<h1>Bestand Succesvol geupload!<br /></h1>";
}
}
else{
echo "<h1> NOD32 detected a viral intrusion!<br /></h1>";
}
}
}}
}
mysql_query("OPTIMIZETABLE `Foto` ");
?>
</p><hr />
<h3> Upload hier uw Foto's!</h3>
<hr />
<p>
<form enctype="multipart/form-data" action="" method="post" name="foto-upload">
<input name="MAX_FILE_SIZE" value="10485760" type="hidden">
Uw afbeeldingen:<br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
<input name="file[]" type="file"><br />
Het album waar ze aan toegevoegd moeten worden:<br />
<input name="album" type="text" maxlength="20"
<?php if(isset($_GET['album']))echo ' value="'.$_GET['album'].'" '; ?>><br />
<input value="Submit" type="submit">
</form>
</p>
<hr />
直到需要上传到数据库的部分...
然后它触发 SQL 错误说:查询为空。
感谢您的宝贵时间!
【问题讨论】:
-
通常最好将图像文件存储在 file 系统而不是数据库中。
-
@Dagon:一般来说,但并非总是如此。
-
@Bill 很棒的评论,那 not 适用于什么?
-
@Dagon:最好将图像存储在数据库中,例如,如果您需要图像服从事务隔离或回滚,或者在删除相应行时自动消失,或者包含在数据库备份中。