[DUPLICATE]File uploads, but BLOB has 0 length

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

[DUPLICATE]File uploads, but BLOB has 0 length

Post by vaughtg »

TURNS OUT THIS IS A RED HERRING, THE SOLUTION TO THE THREAD BELOW WAS NOT A SOLUTION. ACTUAL ISSUE LINKED BELOW IS STILL AN UNRESOLVED ISSUE.
viewtopic.php?f=1&t=130457

Okay, I'm still trying to troubleshoot this piece of code. Some of you may have seen it before, as I'm working through the issues that I'm having with it.

The current issue is that I go through the code, getting good results from every operation, and when I go to check the size of the file in the BLOB column in Oracle, it's got a size of zero (0).

Code: Select all

  public function loadBLOBFile($filePurpose, $fileType, $fileName, $fileSize){
    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, 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->save($_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;
  }
This is what I find from SQL+
SQL> select dbms_lob.getlength(filedata)
2 from blob_tbl;

DBMS_LOB.GETLENGTH(FILEDATA)
----------------------------
672
0
The first is a text file I uploaded during testing, the second is a PDF I tried uploading once I got the code converted into my app class.

:banghead: :banghead: :banghead:
Last edited by vaughtg on Fri Jul 15, 2011 2:08 pm, edited 2 times in total.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: File uploads, but BLOB has 0 length

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();
// 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'])){
          oci_commit($conn);
          $msg = "File upload was successful.";
        }else{
          $err = oci_error($stid);
          $msg = "Couldn't upload file.";
        }
        $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: File uploads, but BLOB has 0 length

Post by vaughtg »

And I've found that OCI_lob::save() is for saving a string into a LOB field, while savefile() (an alias of import()) is for saving files into a LOB field. Now, back to why I get a failure (result of "false") on my savefile() operation.
Post Reply