当前位置:网站首页>PHP PDO ODBC将一个文件夹的文件装载到MySQL数据库BLOB列,并将BLOB列下载到另一个文件夹

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

2022-04-23 15:25:00 allway2

<?php

$hostname = "localhost";
$database = "db";
$username = "root";
$password = "root";

try {
    $pdo = new PDO("odbc:Driver={MySQL ODBC 3.51 Driver};host=$hostname;database=$database", $username, $password);
} catch (PDOException $e) {
    echo $e->getMessage();
}

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

foreach ($iterator as $entry) {
    $arrFiles[] = $entry->getFilename();
    echo ( $entry->getFilename());
    echo ( $entry->getPathname());

    $blob = fopen($entry->getPathname(), 'rb');
    $sql = "INSERT INTO gallery(name,image) VALUES(:name,:image)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':name', $entry->getFilename());
    $stmt->bindParam(':image', $blob, PDO::PARAM_LOB);
    $stmt->execute();
}
?>
<?php

$hostname = "localhost";
$database = "db";
$username = "root";
$password = "root";

try {
    $pdo = new PDO("odbc:Driver={MySQL ODBC 3.51 Driver};host=$hostname;database=$database", $username, $password);
} catch (PDOException $e) {
    echo $e->getMessage();
}

$sql = "SELECT name,image FROM gallery";
$stmt = $pdo->query($sql);
while ($row = $stmt->fetch()) {
    $filehandle = fopen("DownLoadFiles/" . $row[name], 'wb');
    fwrite($filehandle, $row[image]);
    fclose($filehandle);
}
?>

版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/allway2/article/details/124361888