Uploading binary files to postgreSQL from PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twinklyblue
    New Member
    • Jun 2007
    • 37

    Uploading binary files to postgreSQL from PHP

    Hi team,

    Its me again. I am still in my quest in studying php.

    I just want to ask if you can upload binary files (ex. jpg, doc files) to the postgreSQL server through PHP? If so, how must this be done? Just a link on any tutorial will do. Thanks!
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    You can use file_get_conten ts to read the contents of the uploaded file into a string. Then addslashes and insert into your query.

    Alternatively, you can store your files elsewhere on the server and then save the path in the database.

    Comment

    • twinklyblue
      New Member
      • Jun 2007
      • 37

      #3
      Originally posted by pbmods
      You can use file_get_conten ts to read the contents of the uploaded file into a string. Then addslashes and insert into your query.

      Alternatively, you can store your files elsewhere on the server and then save the path in the database.
      Hi pbmods,

      Thanks for your reply. I have one more question. What is the difference between addslashes and pg_escape_bytea ? Are they 2 different things?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by twinklyblue
        I have one more question. What is the difference between addslashes and pg_escape_bytea ? Are they 2 different things?
        Hrm.... Looks like pg_escape_bytea is designed for PostgreSQL :)

        Comment

        • twinklyblue
          New Member
          • Jun 2007
          • 37

          #5
          Originally posted by pbmods
          Hrm.... Looks like pg_escape_bytea is designed for PostgreSQL :)
          Ohh.. hmm.. so which should I use? I tried to work this code out but failed. I patterned it over the example with a few minor changes :(

          <?php
          $data = file_get_conten ts($_FILES['form_data']['tmp_name']);
          $escaped = pg_escape_bytea ($data);
          $result = pg_prepare($con nection, "ins_pic",'INSE RT INTO tbl_photo (id,data) VALUES ($1,$2)');
          $result = pg_execute ($connection,"i ns_pic",array(' 1','$escaped')) ;
          ?>

          This is the page where I was trying to load the file from:
          <form method="post" action="process .php" enctype="multip art/form-data">
          <input type="hidden" name="MAX_FILE_ SIZE" value="1000000" >
          File to upload/store in database:<br>
          <input type="file" name="form_data " size="40">
          <input type="hidden" name="task" value ="uplImg"/>
          <input type="hidden" name="task" value ="fileUpload "/>
          <p><input type="submit" name="submit" value="submit">
          </form>

          Many thanks for your replies

          Comment

          • twinklyblue
            New Member
            • Jun 2007
            • 37

            #6
            Hi Again,

            I managed to make it work. I cant remember how but I found out the file is already in the db :))

            Now I have to figure out how to retrive it.. he..he

            Comment

            Working...