Forum Stats

  • 3,827,144 Users
  • 2,260,745 Discussions
  • 7,897,182 Comments

Discussions

how to load folder with images php with oracle

997232
997232 Member Posts: 50
edited May 3, 2013 7:30PM in PHP
Hi i want to upload from my php form folder with images what should i fix in my php code for 1 image?

<?php

define("ORA_CON_UN", "obrazy");
define("ORA_CON_PW", "miksas1");
define("ORA_CON_DB", "//localhost/orcl");

if (!isset($_FILES['lob_upload'])) {
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"
enctype="multipart/form-data">
Image filename: <input type="file" name="lob_upload">
<input type="submit" value="Upload">
</form>

<?php
}
else {

$conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);


// Insert the BLOB from PHP's tempory upload area

$lob = oci_new_descriptor($conn, OCI_D_LOB);
$stmt = oci_parse($conn, 'INSERT INTO FOTKI (FOTKAID, FOTKA) '
.'VALUES(:MYBLOBID, EMPTY_BLOB()) RETURNING FOTKA INTO :FOTKA');
oci_bind_by_name($stmt, ':MYBLOBID', $myblobid);
oci_bind_by_name($stmt, ':FOTKA', $lob, -1, OCI_B_BLOB);
oci_execute($stmt, OCI_DEFAULT);

// The function $lob->savefile(...) reads from the uploaded file.
// If the data was already in a PHP variable $myv, the
// $lob->save($myv) function could be used instead.
if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
oci_commit($conn);
echo '<p>Obrazek załadowano</p>';
}
else {
echo "Couldn't upload Blob\n";
}
$lob->free();
oci_free_statement($stmt);

oci_close($conn); // log off
}

?>

Answers

  • Christopher Jones-Oracle
    Christopher Jones-Oracle Member Posts: 1,648 Employee
    If I understand correctly you want to convert that script to upload
    multiple images?

    Once you know the filenames, you can use code like that below:
    <?php
    
    // drop table mybtab;
    // create table mybtab (blobid number, blobdata blob);
    
    $c = oci_connect('cj', 'secret', 'localhost/orcl');
    if (!$c) {
        $m = oci_error();
        trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
    }
    
    $sql = 'delete from mybtab';
    $s = oci_parse ($c, $sql);
    $e = oci_execute($s, OCI_COMMIT_ON_SUCCESS);
    
    $lob = oci_new_descriptor($c, OCI_D_LOB);
    $sql = 'insert into mybtab (blobid, blobdata) values(:myblobid, :blobdata)';
    $s = oci_parse($c, $sql);
    oci_bind_by_name($s, ':myblobid', $myblobid);
    oci_bind_by_name($s, ':blobdata', $lob, -1, OCI_B_BLOB);
    
    // Load first image
    $myblobid = 1;
    $myv = file_get_contents("/tmp/oraclelogo.jpg");
    $lob->writeTemporary($myv, OCI_TEMP_BLOB);
    oci_execute($s, OCI_NO_AUTO_COMMIT);
    
    // Load second image
    $myblobid = 2;
    $myv = file_get_contents("/tmp/unbreaklinux.jpg");
    $lob->writeTemporary($myv, OCI_TEMP_BLOB);
    oci_execute($s, OCI_NO_AUTO_COMMIT);
    
    oci_commit($c);
    $lob->close();
    
    ?>
    Note this only parses once.

    I used a temporary lob but you could convert your RETURNING INTO code
    to similarly insert multiple values. Also see
    http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html
This discussion has been closed.