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.