我如何使用PHP在MySQL数据库中插入图像? [英] How can I insert an image in a MySQL database using PHP?
问题描述
< FORM action =testimage1.phpmethod =post>
< div style =font:bold 10px arial,serif; >产品名称*< / div>
< input type =textname =myuserNamemaxlength =50/>< br />
< div style =font:bold 10px arial,serif; >上传照片< / div>
< input name =uploadimagetype =file/>< / br>
< div style =font:bold 10px arial,serif;>产品描述:< / div> < input type =textname =productvalue =>< / br>
< input id =submittype =submitvalue =submit/>< br />
< / form>
和test1.php
require_once( dbconnect.inc.php); //用于数据库连接
$ db_name =thinstrokes;
$ tbl_name =product;
$ db_selected = mysql_select_db($ db_name)或死(无法选择数据库);
//连接服务器并选择databse。
//从表单发送的用户名和密码
$ myusername = $ _ POST ['myusername'];
$ myproduct = $ _ POST ['product'];
$ filename = $ _ POST ['uploadimage'];
$ imgData = file_get_contents($ filename);
$ size = getimagesize($ filename);
$ sql =INSERT INTO product
(productname,image_id,image_type,image,image_size,image_name,productdesc)
VALUES
('$ myusername','11',' {$ size ['mime']}','{$ imgData}','{$ size [3]}',
'{$ _FILES ['userfile'] ['name']}', $ productdesc');
$ result = mysql_query($ sql)或死(上传/ *错误);
出现错误是: -
file_get_contents(DSC02945.JPG)[function.file-get-contents]:未能打开流:C:\xampp\htdocs\thinstrokes中没有这样的文件或目录原来的site\testimage1.php在线22
警告:getimagesize(DSC02945.JPG)[function.getimagesize]:无法打开流:在C:\xampp\htdocs\\中没有这样的文件或目录\\ th>笔记本原始网站\testimage1.php上线23
我怎么改正它??? ???
<在你的表单声明中需要enctype = multipart / form-data。解决方案
并通过$ _FILES变量而不是$ _POST变量来访问文件。像:
< form action =testimage1.phpmethod =postenctype =multipart / form-data >
< input name =uploadimagetype =file/>
< / form>
<?php
$ filename = $ _FILES ['uploadimage'] ['tmp_name'];
?>
I am using this form:
<FORM action="testimage1.php" method="post">
<div style="font:bold 10px arial,serif;" >Product Name*</div>
<input type="text" name="myuserName" maxlength="50" /><br />
<div style="font:bold 10px arial,serif;" >Upload a photo</div>
<input name="uploadimage" type="file" /></br>
<div style="font:bold 10px arial,serif;">Product Description:</div> <input type="text" name="product" value=""></br>
<input id="submit" type="submit" value="submit" /><br />
</form>
and in test1.php
require_once("dbconnect.inc.php"); //for database connection
$db_name="thinstrokes";
$tbl_name="product";
$db_selected=mysql_select_db("$db_name")or die("cannot select DB");
// Connect to server and select databse.
// username and password sent from form
$myusername=$_POST['myusername'];
$myproduct=$_POST['product'];
$filename=$_POST['uploadimage'];
$imgData = file_get_contents($filename);
$size = getimagesize($filename);
$sql = "INSERT INTO product
(productname, image_id , image_type ,image, image_size, image_name,productdesc)
VALUES
('$myusername','11', '{$size['mime']}', '{$imgData}', '{$size[3]}',
'{$_FILES['userfile']['name']}','$productdesc')";
$result=mysql_query($sql) or die("error in uploading/*");
and getting errors are:-
file_get_contents(DSC02945.JPG) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\thinstrokes original site\testimage1.php on line 22
Warning: getimagesize(DSC02945.JPG) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\thinstrokes original site\testimage1.php on line 23
how can i correct it..???
You need enctype=multipart/form-data in your form declaration. And access the file through the $_FILES variable instead of the $_POST variable. Like:
<form action="testimage1.php" method="post" enctype="multipart/form-data">
<input name="uploadimage" type="file" />
</form>
<?php
$filename = $_FILES['uploadimage']['tmp_name'];
?>
这篇关于我如何使用PHP在MySQL数据库中插入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!