【发布时间】:2012-05-09 01:24:18
【问题描述】:
我正在学习如何上传带有插页的照片。到目前为止,我已将图像上传到“照片”文件夹。这样可行。我创建了一个名为“image”的 blob 字段。
我应该看到 blob 字段中的路径还是实际照片?困惑。
<?
$order = "INSERT INTO reg_add (connect_date,
reg,
first_name,
last_name,
image)
VALUES
('$_POST[connect_date]',
'{$_POST[reg]}nv',
'$_POST[first_name]',
'$_POST[last_name]',
'$_POST[image]')";
$new_image = 'photos/'.basename( $_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'], $new_image)) {
// The images was uploaded
} else{
header("location: reg_add_fail_IMAGE.php");
}
$sql = "INSERT INTO image (path) VALUES ('" . mysql_real_escape_string($path) . "')";
$result = mysql_query($order);
?>
表格:
<form id="form_register" method="POST" action="reg_add.php">
<input class="req-string bx short" type="text" name="connect_date" id="connect_date">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input class="req-string bx long caps" type="text" name="reg" id="reg">
<input class="req-string bx long" type="text" name="first_name">
<input class="bx long" type="text" name="last_name">
Choose a image to upload: <input name="image" type="file">
<input id="rbSubmit" class="rb2 rbSubmit" type="submit" value="submit">
</form>
【问题讨论】:
-
无论哪种方式都可以,本网站上有很多问题在权衡每种方法的优缺点。
-
在phpMyAdmin的浏览器中可以看到图片吗?或者我可以显示链接吗?
-
如果您想将链接放入数据库中(我会推荐),您必须将其添加到您的
INSERT语句中(即在@987654326 之前将列名添加到列表中@ 关键字和列表中VALUES关键字之后的相应位置的图像路径):字符串类型的列(例如VARCHAR)比BLOB更适合此目的。如果您想将图像本身存储在BLOB中(这是可能的,但我不推荐),那么您必须使用图像文件的内容作为要插入的值来执行类似的操作。跨度> -
顺便说一句,您的代码容易受到 SQL 注入的攻击。您真的应该使用准备好的语句,您将变量作为参数传递到其中,这些参数不会针对 SQL 进行评估。如果你不知道我在说什么,或者如何解决它,请阅读Bobby Tables 的故事。
-
我最终会使用准备好的语句。现在我正在尝试了解上传的基础知识。
标签: php mysql image upload insert