Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class PDOStreamWrapper {
- private $pk;
- private static $dbh;
- private $fp;
- private $writeMode = false;
- public static function setPDO(PDO $pdo)
- {
- self::$dbh = $pdo;
- }
- public function stream_open($path, $mode, $options, $opened_path)
- {
- $this->pk = parse_url($path, PHP_URL_HOST);
- $stmt = self::$dbh->prepare('SELECT file_contents FROM lob_test WHERE pk = ?');
- $stmt->bindValue(1, $this->pk, PDO::PARAM_INT);
- $stmt->execute();
- $res = $stmt->fetchColumn();
- if (!$res) {
- trigger_error("No matching file in database.", E_USER_ERROR);
- // there's no such item in the database
- return false;
- }
- if ($mode{0} == 'w' || $mode == 'r+') {
- $this->writeMode = true;
- trigger_error("File is being opened in WRITE mode (Forcing fopen with 'r+').", E_USER_NOTICE);
- // If we are writing a file, then we should open up a temporary
- // stream which we will update the contents of the database row with
- // at stream flush time
- $this->fp = fopen("php://temp", 'r+');
- } elseif ($mode == 'r') {
- // if we're reading, then we expect there to be an existing row for the
- // data and so we should return the results from the PDO resultset.
- trigger_error("File is being opened in READ mode (Using PDO stream resource).", E_USER_NOTICE);
- $this->fp = $res;
- } else {
- trigger_error("Invalid mode: " . $mode, E_USER_ERROR);
- return false;
- }
- return true;
- }
- /**
- * Flushes stream resource.
- */
- public function stream_flush()
- {
- if ($this->writeMode) {
- fflush($this->fp); // this shouldn't have any effect for memory streams anyway
- $stmt = self::$dbh->prepare('SELECT pk FROM lob_test WHERE pk = ?');
- $stmt->bindValue(1, $this->pk, PDO::PARAM_INT);
- $stmt->execute();
- if ($stmt->fetchColumn()) {
- $sql = 'UPDATE lob_test SET file_contents = ? WHERE pk = ?';
- } else {
- $sql = 'INSERT INTO lob_test (file_contents, pk) VALUES (?, ?)';
- }
- $updateStmt = self::$dbh->prepare($sql);
- $updateStmt->bindValue(1, $this->fp, PDO::PARAM_LOB);
- $updateStmt->bindValue(2, $this->pk, PDO::PARAM_INT);
- $updateStmt->execute();
- }
- }
- public function stream_read($count)
- {
- return fread($this->fp, $count);
- }
- public function stream_write($data)
- {
- return fwrite($this->fp, $data);
- }
- public function stream_eof()
- {
- return feof($this->fp);
- }
- public function stream_tell()
- {
- return ftell($this->fp);
- }
- public function stream_seek($offset, $whence)
- {
- return fseek($this->fp, $offset, $whence);
- }
- public function stream_close()
- {
- return fclose($this->fp);
- }
- }
- // Establish connection
- $dbh = new PDO('pgsql:dbname=testdb user=test');
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // Setup stream wrapper
- PDOStreamWrapper::setPDO($dbh);
- stream_wrapper_register('pdotest', 'PDOStreamWrapper');
- // Attempt to create table
- try {
- $dbh->exec('CREATE TABLE lob_test( pk INTEGER NOT NULL, file_contents BYTEA, PRIMARY KEY (pk))');
- } catch (PDOException $x) {
- trigger_error($x->getMessage(), E_USER_WARNING);
- // Assume that table already exists; allow continue
- }
- $dbh->exec('DELETE FROM lob_test');
- // 1) Insert value!
- $tmpfp = fopen("php://temp", "r+");
- fwrite($tmpfp, "This is just a database test!");
- rewind($tmpfp);
- /// print "Debug dump of \$fp: "; debug_zval_dump($fp);
- $sql = "INSERT INTO lob_test (pk, file_contents) VALUES (?,?)";
- $stmt = $dbh->prepare($sql);
- $stmt->bindValue(1, 201, PDO::PARAM_INT);
- $stmt->bindValue(2, $tmpfp, PDO::PARAM_LOB);
- $stmt->execute();
- fclose($tmpfp);
- unset($stmt);
- $fp = fopen("pdotest://201", "r+");
- if (!$fp) {
- throw new Exception("Unable to open stream for write.");
- }
- fwrite($fp, "This is rewritten data!");
- fclose($fp);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement