Welcome Guest, Not a member yet? Register   Sign In
postgres bytea fields
#1

[eluser]Andy Chapman[/eluser]
Hi all, not sure if I'm doing something wrong but I'm having trouble maintaining file integrity when writing to / reading from postgres bytea fields.

I have a page where a file is uploaded and stored in a postgres byte a field.

When writing the file to the database, I'm using ActiveRecord like so:

Code:
$data = file_get_contents($_FILES["resource_file"]["tmp_name"]);
   if(!$data)
      show_error("Sorry, could't read the uploaded file!");
                
   $update_data["filedata"] = pg_escape_bytea($data);

Then when I'm trying to restore the file, I'm doing this:

Code:
// Output the appropriate header  and content disposition.
    header("Content-type:$m_type");
    header("Content-Disposition:attachment;filename='" . $file_name . "'");

    // Output the file.
    print pg_unescape_bytea($obj_resource->filedata);

The file comes out bigger than it should be and corrupted. I've tried not using the pg_escape_bytea methods but then the file data isn't inserted into the database properly at all.

Any ideas?
#2

[eluser]Gerben van Eck[/eluser]
Is the data OK immediately after uploading? (have you tested echo $data to validate this). The CI functions (as far as I scanned through them) don't seem to do a lot to the actual data.
#3

[eluser]Andy Chapman[/eluser]
It's hard to tell if it's CI's fault or not, but when I viewed the data in the database directly, it looked like the data was being escape twice, and the file size on download was twice the original size.

I've reverted to using text fields and base64 encoding for now. This isn't ideal, as it will come with a performance hit, but it wont matter for this site as it's for an intranet.




Theme © iAndrew 2016 - Forum software by © MyBB