[SOLVED]OCI_lob::savefile() fails - no error code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

[SOLVED]OCI_lob::savefile() fails - no error code

Post by vaughtg »

I've tried this two separate ways:

Code: Select all

    if ($lob->import($_FILES['lob_upload']['tmp_name'])) {
AND

Code: Select all

    if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
Neither is saving my Word document in my BLOB field in the database. The second format (officially an alias of the first format) did save a plain text file in the BLOB field, but every time I try to do this with my Word doc, it throws back a false result.

According to the official, online docs the results are
Returns TRUE on success or FALSE on failure.
So, NO explanation as to WHY it failed, just that it failed.

Anybody seen this before? Any ideas as to WHY it would be failing?

Full code of function:

Code: Select all

  public function loadBLOBFile($filePurpose, $fileType, $fileName){
    if(!isset($_SESSION['user'])) $_SESSION['user']=1;
    $sequence = "blobs_seq";
    $myblobid = $this->getNextId($sequence);
    $conn = $this->getConnection();
    // Delete any existing BLOB

    $query = 'DELETE FROM blobTbl WHERE FILEPURPOSE = :FILEPURPOSE '.
        'AND USERID = :USERID';
    $stid = oci_parse ($conn, $query);
    if(!$stid){
      $err = oci_error($stid);
      $this->setOciErrorMsg($err);
      return;
    }
    oci_bind_by_name($stid, ':FILEPURPOSE', $filePurpose);
    oci_bind_by_name($stid, ':USERID', $_SESSION['user']);
    $e = oci_execute($stid, OCI_COMMIT_ON_SUCCESS);
    if(!$e){
      $err = oci_error($e);
      $this->setOciErrorMsg($err);
      return;
    }
    oci_free_statement($stid);

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

    $lob = oci_new_descriptor($conn, OCI_DTYPE_LOB);
    $stid = oci_parse($conn, 'INSERT INTO blobTbl  (BLOBID, FILETYPE, '.
        'USERID, FILEPURPOSE, FILENAME, FILEDATA) VALUES (:MYBLOBID, '.
        ':FILETYPE, :USERID, :FILEPURPOSE, :FILENAME, EMPTY_BLOB()) '.
        'RETURNING FILEDATA INTO :BLOBDATA');
    if(!$stid){
      $err = oci_error($stid);
      $this->setOciErrorMsg($err);
      return;
    }
    oci_bind_by_name($stid, ':MYBLOBID', $myblobid);
    oci_bind_by_name($stid, ':FILEPURPOSE', $filePurpose);
    oci_bind_by_name($stid, ':FILETYPE', $fileType);
    oci_bind_by_name($stid, ':USERID', $_SESSION['user']);
    oci_bind_by_name($stid, ':FILENAME', $fileName);
    oci_bind_by_name($stid, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
    $success = oci_execute($stid, OCI_DEFAULT);
    if(!$success){
      $err = oci_error($stid);
      $this->setOciErrorMsg($err);
    }

    // The function $lob->import(...) reads from the uploaded file.
    if ($lob->import($_FILES['lob_upload']['tmp_name'])) {
      oci_commit($conn);
      $msg = "File upload was successful.\n";
    }else{
      $msg = "Couldn't upload file.\n";
    }
    $lob->free();
    oci_free_statement($stid);
    return $msg;
  }
Thanks in advance for any assistance you can offer.
:? :banghead: :? :banghead: :? :banghead: :|
Last edited by vaughtg on Fri Jul 15, 2011 4:28 pm, edited 6 times in total.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: File upload fails with no explanation

Post by twinedev »

Without the code of the functions to know what they are doing, not going to be able to tell you what they may have died on.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: File upload fails with no explanation

Post by vaughtg »

Sorry. I'd posted this code, but it was in another thread. Suffering from context-sickness, I guess.

Code: Select all

  public function loadBLOBFile($filePurpose, $fileType, $fileName){
    if(!isset($_SESSION['user'])) $_SESSION['user']=1;
    $sequence = "blobs_seq";
    $myblobid = $this->getNextId($sequence);
    $conn = $this->getConnection();
    // Delete any existing BLOB

    $query = 'DELETE FROM blobTable WHERE FILEPURPOSE = :FILEPURPOSE '.
        'AND USERID = :USERID';
    $stid = oci_parse ($conn, $query);
    if(!$stid){
      $err = oci_error($stid);
      $this->setOciErrorMsg($err);
      return;
    }
    oci_bind_by_name($stid, ':FILEPURPOSE', $filePurpose);
    oci_bind_by_name($stid, ':USERID', $_SESSION['user']);
    $e = oci_execute($stid, OCI_COMMIT_ON_SUCCESS);
    if(!$e){
      $err = oci_error($e);
      $this->setOciErrorMsg($err);
      return;
    }
    oci_free_statement($stid);

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

    $lob = oci_new_descriptor($conn, OCI_DTYPE_LOB);
    $stid = oci_parse($conn, 'INSERT INTO blobTable (BLOBID, FILETYPE, '.
        'USERID, FILEPURPOSE, FILENAME, FILEDATA) VALUES (:MYBLOBID, '.
        ':FILETYPE, :USERID, :FILEPURPOSE, :FILENAME, EMPTY_BLOB()) '.
        'RETURNING FILEDATA INTO :BLOBDATA');
    if(!$stid){
      $err = oci_error($stid);
      $this->setOciErrorMsg($err);
      return;
    }
    oci_bind_by_name($stid, ':MYBLOBID', $myblobid);
    oci_bind_by_name($stid, ':FILEPURPOSE', $filePurpose);
    oci_bind_by_name($stid, ':FILETYPE', $fileType);
    oci_bind_by_name($stid, ':USERID', $_SESSION['user']);
    oci_bind_by_name($stid, ':FILENAME', $fileName);
    oci_bind_by_name($stid, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
    $success = oci_execute($stid, OCI_DEFAULT);
    if(!$success){
      $err = oci_error($stid);
      $this->setOciErrorMsg($err);
    }

    // The function $lob->import(...) reads from the uploaded file.
    if ($lob->import($_FILES['lob_upload']['tmp_name'])) {
      oci_commit($conn);
      $msg = "File upload was successful.\n";
    }else{
      $msg = "Couldn't upload file.\n";
    }
    $lob->free();
    oci_free_statement($stid);
    return $msg;
  }
Wait - that the same code I posted above. The only thing here that isn't pretty much straight out of the PHP API is the getConnection() function. Beyond that, I'm passing in three strings that describe the file being uploaded.

Here's the getConnection() code:

Code: Select all

  public function getConnection(){
    /*
     * returns the current connection to the database or sets an error
     *  message that no connection exists
     */
    if(!DBConnection::$instantiated){
      $conn = null;
      try{
        $conn = oci_connect(ORA_USER, ORA_PWD, ORA_HOST);
        if(!$conn){
          $this->setOciErrorMsg(oci_error());
          $this->setErrorMsg("Could not connect to database", E_USER_ERROR);
          return;
        }
        DBConnection::$connection =& $conn;
        DBConnection::$instantiated = true;
      }catch(Exception $e){
        $this->setErrorMsg("- ERROR: constructing DBConnection <br />".
            $e->getMessage()."<br />");
        //die("Database connection failed. ".$e->getMessage());
      }
    }
    if(!DBConnection::$instantiated){
      $this->setErrorMsg("-Error getConnection:No connection to the database.");
    }
    return DBConnection::$connection;
  }
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: File upload fails with no explanation

Post by vaughtg »

Okay, I was running

Code: Select all

    // The function $lob->import(...) reads from the uploaded file.
    if ($lob->import($_FILES['lob_upload']['tmp_name'])) {
which was originally

Code: Select all

    // The function $lob->savefile(...) reads from the uploaded file.
    if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
Then I put in this

Code: Select all

    // The function $lob->import(...) reads from the uploaded file.
    if ($lob->save($_FILES['lob_upload']['tmp_name'])) {
And now it *seems* to be working.

I'll mark this solved IF I can get the data back out and display it.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: File upload fails with no explanation

Post by vaughtg »

Okay, THIS issue has NOT been resolved by using OCI_lob->save() vs import() or savefile()
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: File upload fails with no explanation

Post by vaughtg »

Okay, here I've got the test code (lifted from the web and barely modified)

Code: Select all

<?php
require_once '../classes/DBConnection.php';
// Sample form to upload and insert an image into an ORACLE BLOB
// column using PHP5's OCI8 API.
//
// Before running this script, execute these statements in SQL*Plus:
// drop table btab;
// create table btab (blobid number, blobdata blob);
//
// This example uploads an JPG file and inserts it into a BLOB
// column. The image is retrieved back from the column and displayed.
// Make sure there is no whitespace before "<?php" else the wrong HTTP
// header will be sent and the image won't display properly.
//
// Based on a sample originally found in
// http://www.php.net/manual/en/function.oci-new-descriptor.php

$myblobid = 1; // should really be a unique id e.g. a sequence number

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 = new DBConnection();
$conn = $conn->getConnection(); // returns the actual connection obtained from oci_connect()
// Delete any existing BLOB so the query at the bottom
// displays the new data

$query = 'DELETE FROM BTAB WHERE BLOBID = :MYBLOBID';
$stid = oci_parse ($conn, $query);
oci_bind_by_name($stid, ':MYBLOBID', $myblobid);
$e = oci_execute($stid, OCI_COMMIT_ON_SUCCESS);
if (!$e) {
  $err = oci_error();
  die;
}
oci_free_statement($stid);

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

$lob = oci_new_descriptor($conn, OCI_D_LOB);
$stid = oci_parse($conn, 'INSERT INTO BTAB (BLOBID, BLOBDATA) '
.'VALUES(:MYBLOBID, EMPTY_BLOB()) RETURNING BLOBDATA INTO :BLOBDATA');
oci_bind_by_name($stid, ':MYBLOBID', $myblobid);
oci_bind_by_name($stid, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
oci_execute($stid, 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);
}
else {
echo "Couldn't upload Blob\n";
}
$lob->free();
oci_free_statement($stid);

// Now query the uploaded BLOB and display it

$query = 'SELECT BLOBDATA FROM BTAB WHERE BLOBID = :MYBLOBID';

$stid = oci_parse ($conn, $query);
oci_bind_by_name($stid, ':MYBLOBID', $myblobid);
oci_execute($stid, OCI_DEFAULT);
$arr = oci_fetch_assoc($stid);
$result = $arr['BLOBDATA']->load();

// If any text (or whitespace!) is printed before this header is sent,
// the text won't be displayed and the image won't display properly.
// Comment out this line to see the text and debug such a problem.
header("Content-type: ".$_FILES['lob_upload']['type']);
echo $result;

oci_free_statement($stid);

oci_close($conn); // log off
}
?>
That works - wonderfully. And I compare that to what I've got in my test code, that doesn't work, and I'm confused about what I've messed up. (Uploading the exact same file into the same data type in the table.)

Code: Select all

<?php
session_start();
require_once '../classes/DBConnection.php';
if(isset($_GET['docType'])) $_SESSION['docType'] = $_GET['docType'];
if(isset($_SESSION['docType']) && ("resume" == $_SESSION['docType'] ||
		"schedule" == $_SESSION['docType'])){
  $filePurpose = $_SESSION['docType'];
}else{
  $filePurpose = "test";
}
if(isset($_FILES['uploadedfile'])){
  if(isset($filePurpose)){
    $msg = "";
    $needle = $_FILES["uploadedfile"]["type"];
    $haystack = array("text/plain", "application/pdf", "application/msword",
        "image/jpeg", "image/pjpeg");
    $fileSize = $_FILES["uploadedfile"]["size"];
    if(!in_array($needle, $haystack)){
      $msg .= "Invalid file type.";
    }elseif(0>=$fileSize || 2048000<=$fileSize){
      $msg .= "Invalid file size.";
    }else{
      $fileName = $_FILES['uploadedfile']['name'];
      $fileName = str_replace("#", "No.", $fileName);
      $fileName = str_replace("$", "Dollar", $fileName);
      $fileName = str_replace("%", "Percent", $fileName);
      $fileName = str_replace("&", "and", $fileName);
      $fileName = str_replace("^", "", $fileName);
      $fileName = str_replace("\\", "", $fileName);
      $fileName = str_replace("|", "", $fileName);
      $fileName = str_replace(";", "", $fileName);
      $fileName = str_replace(":", "", $fileName);
      $fileName = str_replace("*", "", $fileName);
      $fileName = str_replace("?", "", $fileName);
      $fileType=$_FILES["uploadedfile"]["type"];
      if ($_FILES["uploadedfile"]["error"] > 0){
        $msg .= "Return Code: ".$_FILES["uploadedfile"]["error"]."<br />";
      }else{
        $dbconn = new DBConnection();
        if(!isset($_SESSION['user'])) $_SESSION['user']=1;
        $sequence = "blobs_seq";
        $myblobid = $dbconn->getNextId($sequence);
        $conn = $dbconn->getConnection();
        // Delete any existing BLOB

        $query = 'DELETE FROM uploadedFiles WHERE FILEPURPOSE = :FILEPURPOSE '.
            'AND USERID = :USERID';
        $stid = oci_parse ($conn, $query);
        if(!$stid){
          $err = oci_error($stid);
          die;
        }
        oci_bind_by_name($stid, ':FILEPURPOSE', $filePurpose);
        oci_bind_by_name($stid, ':USERID', $_SESSION['user']);
        $e = oci_execute($stid, OCI_COMMIT_ON_SUCCESS);
        if(!$e){
          $err = oci_error($e);
          die;
        }
        oci_free_statement($stid);

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

        $lob = oci_new_descriptor($conn, OCI_D_LOB);
        $stid = oci_parse($conn, 'INSERT INTO uploadedFiles (BLOBID, FILETYPE, '.
            'USERID, FILEPURPOSE, FILENAME, FILESIZE, FILEDATA) VALUES '.
            '(:MYBLOBID, :FILETYPE, :USERID, :FILEPURPOSE, :FILENAME, '.
            ':FILESIZE, EMPTY_BLOB()) RETURNING FILEDATA INTO :BLOBDATA');
        if(!$stid){
          $err = oci_error($stid);
          $this->setOciErrorMsg($err);
          return;
        }
        oci_bind_by_name($stid, ':MYBLOBID', $myblobid);
        oci_bind_by_name($stid, ':FILEPURPOSE', $filePurpose);
        oci_bind_by_name($stid, ':FILETYPE', $fileType);
        oci_bind_by_name($stid, ':USERID', $_SESSION['user']);
        oci_bind_by_name($stid, ':FILENAME', $fileName);
        oci_bind_by_name($stid, ':FILESIZE', $fileSize);
        oci_bind_by_name($stid, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
        $success = oci_execute($stid, OCI_DEFAULT);
        if(!$success){
          $err = oci_error($stid);
          $this->setOciErrorMsg($err);
        }

        // The function $lob->import(...) reads from the uploaded file.
        if ($lob->savefile($_FILES['lob_upload']['tmp_name'])){ // ******** I set a breakpoint here *********
          oci_commit($conn);
          $msg = "File upload was successful.";
        }else{
          $err = oci_error($stid); // ***** flow steps to here *****
          $msg = "Couldn't upload file."; // ***** stepping to here and $err is false, so no explanation *****
        }
        $lob->free();
        oci_free_statement($stid);

        //$msg .= $conn->loadBLOBFile($docType, $fileType, $fileName, $fileSize);
      }
    }
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <script type="text/javascript">
      if("" != "<?php echo $msg;?>"){
        alert("<?php echo $msg;?>");
        window.close();
      }
    </script>
  </head>
  <body>
    <form enctype="multipart/form-data" action="uploader.php" method="POST">
      <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
      <?php if("resume"==$filePurpose){?>
      <h3>Uploading your resume</h3>
      Acceptable file formats are:<br />.doc, .txt, .pdf
      <?php }else if("schedule"==$filePurpose){?>
      <h3>Uploading your schedule</h3>
      Acceptable file formats are:<br />.jpg, .pdf
      <?php }?>
      <br /><br />Choose a file to upload:
      <input name="uploadedfile" type="file" /><br />
      <input type="submit" value="Upload File" />
    </form>
  </body>
</html>
Long-story-short, the first bit of code uploads a file and produces a BLOB field with identical length to the file size, the second bit of code uploads the file and I get BLOB field with length 0.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: OCI_lob::savefile() fails - no error code

Post by vaughtg »

SOLVED

$_FILES['lob_upload'] != $_FILES["uploadedfile"]
Post Reply