[英]create a sql table with image
I want to use php to create one table in sql which has images set content of the image and then I want fetch this table in html. 我想使用php在sql中创建一个表,该表具有图像设置图像的内容,然后以html形式获取此表。
someone knows how this work? 有人知道这是如何工作的吗? or someone has see the example on line?
或有人在线查看示例?
please help. 请帮忙。 thanks a lot.
非常感谢。
这是一篇文章,将告诉您所有有关在blob列中存储图像等内容的知识, http: //www.phpriot.com/articles/images-in-mysql
Ok, I understand that you want to upload an image file into a table in db, through your web app, using php, and then again you want to display that same image from the table in your web app(html page). 好的,我知道您想使用php通过Web应用程序将图像文件上传到db中的表中,然后再次希望从Web应用程序(html页)中的表中显示相同的图像。
I will show the example for MySQL, using simple PHP 5.3 我将使用简单的PHP 5.3展示MySQL的示例
Create a sample table, say "photo_table" : 创建一个示例表,说“ photo_table”:
CREATE TABLE photo_table (photo_name_col VARCHAR(30),photo_img_col LONGBLOB);
Now for uploading the image file through your php page use the following in your html code: 现在,要通过您的php页面上传图像文件,请在html代码中使用以下代码:
<form name="photoForm" method="post" ENCTYPE="multipart/form-data" action="../insertImage.php" >
<input type="text" name="uploadFileName">
<!--commenting as I was not able to publish this note<input type="file" name="uploadFile" />-->
<!--Some other input fields if you want-->
<input type="submit" value="Submit"/>
</form>
Now, once the form "photoForm" is submitted, the form values are passed to "insertImage.php", where we will have the following code to upload the photo and its name into a table in our MySQL db: 现在,提交表单“ photoForm”后,表单值将传递到“ insertImage.php”,在这里我们将具有以下代码将照片及其名称上传到我们的MySQL数据库中的表中:
<?php
$con=mysql_connect("ipAddressOfDB","username","password");
if(!$con){
die('Could not connect:'.mysql_error());
}
mysql_select_db("db_name",$con);
$image = $_FILES['uploadfile']['tmp_name'];
$imageName = $_POST['uploadFileName'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into photo_table values('$imageName','$content')";
mysql_query($sql,$con);
?>
For displaying the photo from table into your html page, I use the following approach. 为了将表格中的照片显示到您的html页面中,我使用以下方法。 Fetch the image from db and copy it to a temporary location in the server.
从db获取映像并将其复制到服务器中的临时位置。
<?php
$con=mysql_connect("ipAddressOfDB","username","password");
mysql_select_db("db_name",$con);
$sql="select * from photo_table where photo_name_col ='$uploadFileName'";
$result=mysql_query($sql,$con);
if($row=mysql_fetch_array($result)){
$fileName="generated/".$uploadFileName;
file_put_contents($fileName, $row['photo']);
}
echo "<img src='" . $fileName . "' />"; <!--this will display the image-->
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.