当前位置:网站首页>PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder

PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder

2022-04-23 15:26: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://yzsam.com/2022/04/202204231525131193.html