0

I have a php script that goes like:-

<?php
    $res = pg_query($db, "SELECT picture FROM userpost WHERE postid='2';");
    $raw = pg_fetch_result($res, 'picture');
    echo pg_unescape_bytea($raw);
?>

where $db is my postgresql database.

I'm using it to display an image that I inserted in my database using the command

 insert into userpost(userid,postid,picture,content) values('244','2',bytea('/home/ankit/Downloads/Photos/0.jpg'),'This is post number 3');

What I get then in my browser is the address of the image, i.e, /home/ankit/Downloads/Photos/0.jpg and not the actual image. I can't understand what's the problem. Please help me figure it out. Thanks!

user11067275
  • 99
  • 3
  • 8
  • 2
    That is what you're storing in your `INSERT` statement: the path to the file. It sounds like you either need to store the *content* of the file, or echo the path within an `` tag. – rickdenhaan Feb 17 '19 at 13:02
  • @rickdenhaan I can't use the `` tag because the database need not be on my laptop. And how is `bytea` storing the address instead of the contents? – user11067275 Feb 17 '19 at 13:04
  • 1
    `bytea('/home/ankit/Downloads/Photos/0.jpg')` simply converts the string `/home/ankit/Downloads/Photos/0.jpg` to a bytestring. It doesn't know that you're passing it a file path, so it won't try to read that file. You'll need to do something like `bytea(file_get_contents('/home/ankit/Downloads/Photos/0.jpg'))` – rickdenhaan Feb 17 '19 at 13:06
  • You can't just pass a path to the database. The database doesn't even need to be on the same server as the image, which means that it wouldn't possibly be able to read the file. You need to read the data using PHP and then pass the contents of the file to that function. (like the comment above suggests). – M. Eriksson Feb 17 '19 at 13:07
  • @rickdenhaan I'll try. Thanks – user11067275 Feb 17 '19 at 13:09
  • @MagnusEriksson ya I know. Actually, I'm initially adding some photos in my database as a dummy data, that's why I needed the insert statements and all... – user11067275 Feb 17 '19 at 13:10

0 Answers0