开发者

PHP limited by filesize saving a blob in oracle

I'm using a php script to save a binary stored in the server's temp directory into oracle as a blob. It works fine for filesizes < 7MB, any larger than that and it just doesn't save. The php post and file upload limit is 32MB. The script stops completely at lob->savefile开发者_运维技巧($tempFile). The general code is below:

$stid = oci_parse($conn, "UPDATE FILE_UPLOAD SET file_blob = EMPTY_BLOB(), status = 'S', temp_file = '{$tempFile}' WHERE FILE_UPLOAD_ID = :file_id RETURNING file_blob INTO :file_blob");
oci_bind_by_name($stid, ':file_blob', $lob, -1, OCI_B_BLOB);
oci_bind_by_name($stid, ':file_id', $fileID);
oci_execute($stid, OCI_DEFAULT);

if ($lob->savefile($tempFile)) { // this is where it stops
    oci_commit($conn);
}
else {
    logAction("Status", "Couldn't upload Blob"); // doesn't get here
}

Any advice would be appreciated.

Regards,

Angus


You can enable tracing for your session and see the trace file. Perhaps it will contain error which is cause of the problem.

EXECUTE DBMS_SESSION.SESSION_TRACE_ENABLE(waits => TRUE, binds => FALSE);

The following script returns the path to the trace file that the current session writes. It returns the path whether or not tracing is enabled.

select 
  u_dump.value   || '/'     || 
  db_name.value  || '_ora_' || 
  v$process.spid || 
  nvl2(v$process.traceid,  '_' || v$process.traceid, null ) 
  || '.trc'  "Trace File"
from 
             v$parameter u_dump 
  cross join v$parameter db_name
  cross join v$process 
        join v$session 
          on v$process.addr = v$session.paddr
where 
 u_dump.name   = 'user_dump_dest' and 
 db_name.name  = 'db_name'        and
 v$session.audsid=sys_context('userenv','sessionid');


Well the problem has been resolved after much debugging, etc. The answer lays herein:

where I was accessing the tempfile directly, i.e.

$tempFileN= $_FILES['Filedata']['tmp_name']

and saving that, I replaced it with:

<br/>
$contents = file_get_contents($_FILES['Filedata']['tmp_name']);<br/>
<br/>

and where I was saving the blob using the savefile method, I changed it to:

<br/>
$lob->save($contents)<br/>
<br/>

It works now.


This example is used in an REST JSON interface. This code saves also big images.

<?php
// Database parameters
$oci_user = 'YOUR_DB_USER';
$oci_pw = 'YOUR_DB_PASSWORD';
$oci_db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 000.000.000.000)(PORT = 1521)))(CONNECT_DATA=(SID=XXX)))"; 

// Get data from JSON imput
$data = json_decode(file_get_contents("php://input"),true);

// Now you can do some checks on date etc.
$img = $data['IMAGE'];
$jfid = $data['OBJECT_ID'];
$jfdate = $data['DATE'];

// Let's beginn with the blob upload
// We have 3 fiels in our table: OBJECT_ID,DATE,BLOBIMAGE
// First you fill your BLOB with an 'Empty' one and assign in PL/SQL style :img
$sql = "INSERT INTO ZAEHLER.METERAPP_BILD (OBJECT_ID,DATE,BLOBIMAGE)
VALUES (".$jfid.",to_date('".$jfdate."','DD/MM/YYYY'),empty_blob())
RETURNING BLOBIMAGE INTO :img";

$result = oci_parse($conn, $sql);
$blob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($result, ":img", $blob, -1, OCI_B_BLOB);
oci_execute($result, OCI_NO_AUTO_COMMIT);

// Now let's check if we could connect to database or if we have to output something => 500
if(!$result){
    $err = oci_error();
    header('HTTP/1.0 500 Internal Server Error');
    header('Content-Type: application/json');
    $out = array('code' => '500', 'response' => '500 Internal Server Error / SQL connection problem', 'error sql' => $err[message]);
    echo json_encode($out);
}
// Can we same the image ($img) or not => 406
// This step saves the image to the db
if(!$blob->save($img)) {
    oci_rollback($conn);
    header('HTTP/1.0 406 Not Acceptable');
    header('Content-Type: application/json');
    $out = array('code' => '406', 'response' => '406 Not Acceptable / Wrong image type or JSON error');
    echo json_encode($out);
}
// If both was ok, we're going to commit and output an OK => 200
else {
    oci_commit($conn);
    header('HTTP/1.0 200 OK');
    header('Content-Type: application/json');
    $out = array('code' => '200', 'response' => '200 OK', 'response advanced' => 'Image saved', 'object_id' => $jfid, 'file date' => $jfdate);
    echo json_encode($out);
}

// Clean up
oci_free_statement($result);
$blob->free();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜