Postgresql php insert image



我的脚本有一个小问题。我想上传图像到PostgreSQL数据库。但它给了我一个错误(见下文)。有人找到一个简单的方法来解决这个问题吗?

代码:

<?php
$uploaddir = '/home/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$name = $_POST['name'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{    echo "File is valid, and was successfully uploaded.n";
}
else   {   echo "File size greater than 300kb!nn";   }
echo "'$name'n";
$host = "localhost"; 
$user = "postgres"; 
$pass = "qwe123"; 
$db = "baza"; 
$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
    or die ("Could not connect to servern"); 
$query = "insert into image values ('$name', lo_import('$uploadfile'), 'now')";
$result = pg_query($query);
if($result)
{
    echo "File is valid, and was successfully uploaded.n";
    unlink($uploadfile);
}
else
{
    echo "Filename already exists. Use another filename. Enter all the values.";
    unlink($uploadfile);
}
pg_close($con);
?>

失败:

 Warning: move_uploaded_file(/home/gora.jpg): failed to open stream: No such file or directory 
   in C:xampphtdocspostgretestimage.php on line 24
 Warning: move_uploaded_file(): Unable to move 'C:xampptmpphp3A3.tmp' to '/home/gora.jpg'
   in C:xampphtdocspostgretestimage.php on line 24
 File size greater than 300kb! 'dfd' 
 Warning: pg_query(): Query failed: Error: Can not open the file server "/home/gora.jpg": No such file or directory 
   in C:xampphtdocspostgretestimage.php on line 38
 Filename already exists. Use another filename. Enter all the values.
 Warning: unlink(/home/gora.jpg): No such file or directory 
   in C:xampphtdocspostgretestimage.php on line 48

显示的错误与PostgreSQL没有任何关系。move_uploaded_file函数由于上述错误而失败。该错误表明该文件可能太大,PHP配置无法处理给定的可用内存-特别注意:

 File size greater than 300kb! 'dfd' 

确保您可以正确上传文件。然后将其插入数据库。同时,在你的代码中加入一些错误处理

您应该启用异常,或者检查函数返回错误码并在出现错误时退出脚本。不要盲目地继续。一旦遇到了一个错误,其余的代码就无法工作了。

最新更新