i got a little problem with my script. I would like to upload image into a PostgreSQL database. But it gives me an error (see below). Has someone found an easy way to solve this problem?
The code:
<?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.
";
}
else { echo "File size greater than 300kb!
"; }
echo "'$name'
";
$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 server
");
$query = "insert into image values ('$name', lo_import('$uploadfile'), 'now')";
$result = pg_query($query);
if($result)
{
echo "File is valid, and was successfully uploaded.
";
unlink($uploadfile);
}
else
{
echo "Filename already exists. Use another filename. Enter all the values.";
unlink($uploadfile);
}
pg_close($con);
?>
fails with:
Warning: move_uploaded_file(/home/gora.jpg): failed to open stream: No such file or directory
in C:\xampp\htdocs\postgretest\image.php on line 24
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php3A3.tmp' to '/home/gora.jpg'
in C:\xampp\htdocs\postgretest\image.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:\xampp\htdocs\postgretest\image.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:\xampp\htdocs\postgretest\image.php on line 48
The errors you show have nothing whatsoever to do with PostgreSQL. The move_uploaded_file
function is failing with the errors given above. The error suggests that the file is probably too large for your PHP configuration to process given its available memory - note in particular:
File size greater than 300kb! 'dfd'
Make sure you can upload the file correctly. Then work on inserting it into the database. While you're at it, put some error handling in your code.
You should enable exceptions, or check function return error codes and exit the script on an error. Don't just continue blindly. Once you've hit one error, the rest of the code isn't going to work, after all.