We are in the midst of a data migration , and the project needs from one particular table BLOB data saved to files, and sent to the vendor, due to their structure
I am able to achieve this one at a time, but needed to create a batch process, as there are 50,000 rows/potential files.
my current code is
$sql = "SELECT a.guid AS file_name, a.attachment AS file_blob"
." FROM attachment a";
$squery = oci_parse($link, $sql);
oci_execute($squery);
while ($row = oci_fetch_array($squery, OCI_ASSOC | OCI_RETURN_LOBS)) {
header('Content-type: octet-stream;');
header('Content-disposition: attachment;filename='.$row['FILE_NAME']);
print $row['FILE_BLOB'];
}
oci_free_statement($squery);
I do understand that I would rather be saving the data rather than streaming it to the browser, I just am not wrapping my head around how to achieve this, should i be looking at php file I/O fputs()?
End result is I would like the process to batch write binary files based on the query to a folder
EDIT
Thank you for your direction and help, based on that I ended up coming up with two separate ways to achieve this
first i did the following getcwd() to verify where the server was pointing to, and did an absolute path if needed
$basedir = '/path/to/host/www/blobdoc/';
set permissions on the blobdoc folder then used either of the following scripts in the while loop
$filename='';
$filename=$basedir.$row['FILE_NAME'];
file_put_contents( $filename, $row['FILE_BLOB']);
$filename='';
$filename=$basedir.$row['FILE_NAME'];
$File = @fopen( $filename, 'w' );
if( $File ) {
if( FALSE === fwrite( $File, $row['FILE_BLOB'] ))
return FALSE;
fclose( $File );
return TRUE;
}
Instead of:
print $row['FILE_BLOB'];Use something like:
file_put_contents( $filename, $row['FILE_BLOB']); //save locallyYou need to write the blob to a file.
If you want to force a download of that file then you need to make use of the correct headers in combinarion with readfile, like so: