Storing large object files into database is much interesting then saving path of files.
Large object data like images, document can be save into database by two simple method:
1> Save images in base encoded form that i had explained in my earlier article PHP Encoding with base64
2> Use BYTEA Data type in Postgresql.
“The BYTEA Data type allows storage of binary strings” you may say as RAW Bytes.
When you SELECT a Bytea type, PostgreSQL returns octal byte values prefixed with ‘\’ (e.g. \032). Users are supposed to convert back to binary format manually.
To Start with create column doc_image having data type “Bytea” in a table.
Now when you use file upload on your PHP projects, you will get file data in file super-global variable $_FILES
and you first have to move file to your server like i have move files to upload folder
move_uploaded_file($_FILES["file"]["tmp_name"], "/upload/".$_FILES["file"]["name"]); // get data of image from upload folder $dataString = file_get_contents("/upload/".$_FILES["file"]["name"]); $raw_data = pg_escape_bytea($dataString); //Store this string into your databse.
Use “pg_escape_bytea” function, it returns escaped string for insertion into a bytea field., and store this raw string ($raw_data) to database.
Now to retrieve that file data use “pg_unescape_bytea” function, It returns the unescaped string, possibly containing binary data.
and if you had used image file to upload then use “pg_unescape_bytea” function to convert into binary and display to browser as