【问题标题】:insert image into mssql using php使用php将图像插入mssql
【发布时间】:2013-03-16 02:12:09
【问题描述】:

我想将图像插入到 mssql 中,为用户提供选择图像并插入数据库的选项。所以我使用了以下代码

<html>
<head>
<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
mmysql_connect("localhost","sample","welcome");
mysql_select_db("samples");

if (empty($_POST["frm"]))   
{

}
else
{
$filename=trim($_REQUEST['slcfile']);
$datastring = file_get_contents($filename);
$data         = unpack("H*hex", $datastring);
echo trim($_REQUEST['slcfile']);
mssql_query("insert into imageinserter values ( 0x".$data['hex'].",'.$filename.')"); 
}

}
?>
</head>
<body>
<form name="frm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
select image :<input type="file" name="slcfile" /> <br>
<input type="submit" name="slcfile" value="addimage"/> 
</form>
</body>
</html>

但它在 file_get_contents 处说错误错误,说没有找到这样的文件或资源。 请帮忙解决问题

提前致谢

【问题讨论】:

    标签: php html sql-server image blob


    【解决方案1】:

    在您的表单中使用multipart/form-data,如下所示:

    <form enctype="multipart/form-data" name="frm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    

    然后通过它的临时名称读取上传的文件 例如:$_FILES['uploadedfile']['tmp_name']

    【讨论】:

    • 我需要添加 multipart/form-data 吗?
    • 我是 php 和 html 的新手。所以请告诉我我需要在表单中赋予什么属性。
    • 在表单标签中添加 enctype="multipart/form-data"
    • 抱歉@krishnakumar 正如Use that enctype所说的那样
    • 你能告诉我要传递的参数是什么来代替 uploadedfile
    【解决方案2】:

    你需要允许

       allow_url_fopen
    

    在您的 php.ini 配置文件中。一些主机出于安全考虑不允许这样做

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多