PHP PDO将一个文件夹的文件装载到ORACLE数据库BLOB列,并将BLOB列下载到另一个文件夹

<?php

//OR connect using the Oracle Instant Client
$dsn = 'oci:dbname=//192.168.50.128:1521/orcl';

$username = 'scott';
$password = 'tiger';

try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}



$arrFiles = array();
$iterator = new FilesystemIterator("InputFiles");

foreach ($iterator as $entry) {
    $arrFiles[] = $entry->getFilename();
    echo ( $entry->getFilename());
    echo ( $entry->getPathname());
    $name = $entry->getFilename();
    $blob = fopen($entry->getPathname(), 'rb');
    $sql = "INSERT INTO bindata(name,data) VALUES(?,EMPTY_BLOB()) RETURNING data INTO ? ";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(1, $name);
    $stmt->bindParam(2, $blob, PDO::PARAM_LOB);
    $pdo->beginTransaction();
    $stmt->execute();
    $pdo->commit();
}
?>
<?php

//OR connect using the Oracle Instant Client
$dsn = 'oci:dbname=//192.168.50.128:1521/orcl';

$username = 'scott';
$password = 'tiger';

$pdo = new PDO($dsn, $username, $password);

$sql = "SELECT name,data FROM bindata";
$stmt = $pdo->query($sql);
while ($row = $stmt->fetch()) {
    $filename = "DownLoadFiles/" . $row['NAME'];
    $content = stream_get_contents($row['DATA']);
    file_put_contents($filename, $content);
}
?>
CREATE TABLE "SCOTT"."BINDATA" (
    "ID"   NUMBER(10, 0),
    "NAME" VARCHAR2(512 BYTE),
    "DATA" BLOB
)
 
ALTER TABLE bindata ADD (
    CONSTRAINT bindata_pk PRIMARY KEY ( id )
);
 
CREATE SEQUENCE bindata_sequence;
 
CREATE OR REPLACE TRIGGER bindata_on_insert BEFORE
    INSERT ON bindata
    FOR EACH ROW
BEGIN
    SELECT
        bindata_sequence.NEXTVAL
    INTO :new.id
    FROM
        dual;
 
END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值