Incorporating images with Oracle to create a PHP web site.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K. A.

    Incorporating images with Oracle to create a PHP web site.

    I'm trying to write a web site for storing staff ID's together with
    their photos using a PHP form. A user of the site should be able to
    enter his ID, name, and select a file from a directory as his personal
    photo.

    Step 1. Created the table in SQL.
    SQL> create table file_test (
    StaffID number(5) NOT NULL,
    StaffName VARCHAR2(30),
    Staff_pic bFile);

    Step 2. Register a directory alias with Oracle.
    SQL> CREATE DIRECTORY Staff_Images_Di r AS 'C:\Staff_Image s';

    Step 3. Insert some BFile names using PHP.
    <?php
    // etc.

    // Build an INSERT for the BFILE names
    $sql = "INSERT INTO file_test ( StaffID, StaffName, Staff_pic )
    VALUES ( '12345', 'Scott Tiger',
    BFILENAME('Staf f_Images_dir',' 12345.jpg') )";

    $stmt = oci_parse($conn , $sql);

    // Open the directory
    $dir = 'C:\Staff_Image s';
    $dh = opendir($dir)
    or die("Unable to open $dir");

    // Loop through the contents of the directory
    while (false !== ( $entry = readdir($dh) ) ) {

    // Match only files with the extension .jpg, .gif or .png
    if ( is_file($dir.'/'.$entry) &&
    preg_match('/\.(jpg|gif|png) $/',$entry) ) {

    // Bind the filename of the statement
    oci_bind_by_nam e($stmt, ":filename" , $entry);

    // Execute the statement
    if ( oci_execute($st mt) ) {
    print "$entry added\n";
    }
    }

    }
    ?>

    However, the above PHP code did not work, so I wrote the following
    lines straight into SQL and I got 1 row created.

    SQL> insert into file_test (StaffID, staff_name, staff_pic)
    values ('12345', 'Scott Tiger', BFILENAME('Staf f_images_dir',
    '12345.jpg'));

    My questions.
    1. How do I create a PHP code to let the user enter his details (ID and
    name) in a form and select a file image (as his personal photo) from a
    particular directory and save everything in the Database.
    2. How do I go back then (as an admin) and view staff details and
    pictures based on a SELECTed staff ID?

    Any help will be highly appreciated.

  • Mladen Gogala

    #2
    Re: Incorporating images with Oracle to create a PHP web site.

    On Sat, 06 May 2006 23:01:53 -0700, K. A. wrote:
    [color=blue]
    > However, the above PHP code did not work,[/color]

    What was the problem? Did you get any errors? I'll probably be able
    to help you out, once you give me all the necessary information.
    [color=blue]
    > so I wrote the following
    > lines straight into SQL and I got 1 row created.
    >
    > SQL> insert into file_test (StaffID, staff_name, staff_pic)
    > values ('12345', 'Scott Tiger', BFILENAME('Staf f_images_dir',
    > '12345.jpg'));[/color]

    Did that work?
    [color=blue]
    >
    > My questions.
    > 1. How do I create a PHP code to let the user enter his details (ID and
    > name) in a form and select a file image (as his personal photo) from a
    > particular directory and save everything in the Database.
    > 2. How do I go back then (as an admin) and view staff details and
    > pictures based on a SELECTed staff ID?[/color]

    Here is a little ADOdb snippet that I wrote to solve precisely the
    same problem. Uploaded the file to a CLOB field, which makes it possible
    for me to have web server and Oracle instance on two different machines.

    <html>
    <head>
    <title>Upload File (ADOdb)</title>
    </head>

    <body>
    <?php
    require_once('a dodb/adodb.inc.php') ;
    require_once('a dodb/adodb-exceptions.inc. php');
    session_start() ;
    $DSN=$_SESSION['DSN'];
    $db=NewADOConne ction("oci8");

    $INS="insert into poetry(file_des c,file_data)
    values (:pdesc,:cont)" ;
    $clob=file_get_ contents($_FILE S['file']['tmp_name']);

    try {
    $db->Connect($DSN['database'],
    $DSN['username'],
    $DSN['password']);
    $db->BeginTrans() ;
    $sth=$db->PrepareSP($INS );
    $db->InParameter($s th,$_POST['desc'],'pdesc');
    $db->InParameter($s th,$clob,'cont' ,-1,OCI_B_CLOB);
    $db->Execute($sth );
    }
    catch (Exception $e) {
    $db->RollbackTrans( );
    die($e->getTraceAsStri ng());
    }
    $db->CommitTrans( );
    ?>

    <center>
    <h2>File <?=$_FILES['file']['name']?>
    uploaded successfully!</h2>
    </center>
    </body>
    </html>

    I wholeheartedly advise you to use ADOdb as it is logical, reliable,
    easy to use, covers different drivers and works with PHP5, which gives
    you the ability to utilize exceptions.

    --


    Comment

    • Christopher.Jones@oracle.com

      #3
      Re: Incorporating images with Oracle to create a PHP web site.

      "K. A." <ka5880@hotmail .com> writes:
      [color=blue]
      > I'm trying to write a web site for storing staff ID's together with
      > their photos using a PHP form. A user of the site should be able to
      > enter his ID, name, and select a file from a directory as his personal
      > photo.[/color]

      Harry Fuecks has an article on using LOBS and BFILE with Oracle at:


      --
      Christopher Jones, Oracle Corporation, Australia.
      Email: Christopher.Jon es@oracle.com
      Blog: http://blogs.oracle.com/opal/

      Comment

      Working...