Reputation: 51
I'm tring to save any files in Cassandra ( CQL3), using YACassandraPDO
enter code here
$tmpName = $_FILES['content']['tmp_name'];
$fp = fopen($tmpName, 'rb');
$content = fread($fp, filesize($tmpName));
$stmt = $cass_db->prepare ("INSERT INTO $table ( id, ctype, fname, content ) VALUES ( now(), :ctype, :fname, textAsBlob('$content')) ;");
$stmt->bindValue (':ctype', $_FILES['content']['type']);
$stmt->bindValue (':fname', $_FILES['content']['name']);
$stmt->execute ();
if it's plain/text everything Ok But i can't save any binary files, I tried not to use textAsBlob, - can't save any types of files As a result PHP Fatal error: Uncaught exception 'PDOException' with message 'CQLSTATE[HY000] [2] Input length = 1'
Upvotes: 1
Views: 1097
Reputation: 604
You can write blob contents to cassandra using Blob class
$fileContent = file_get_contents($_FILES['content']['tmp_name']);
$blob = new \Cassandra\Blob($fileContent);
$content = $blob->bytes();
$stmt = new Cassandra\SimpleStatement("INSERT INTO $table ( id, ctype, fname, content ) VALUES ( ?,?,?,?");
$result = $session->execute($statement, new Cassandra\ExecutionOptions(array(
'arguments' => array($id, $ctype, $fname,$content)
)));
Upvotes: 0
Reputation: 291
p.s. why you use: pack("H*",$data[0]['content']); instead more simple hex2bin($data[0]['content']) ?
Upvotes: 0
Reputation: 11
My decisions own:
To Write
$content = file_get_contents($_FILES['content']['tmp_name']);
$content = bin2hex($content);
$stmt = $cass_db->prepare ("INSERT INTO $table ( id, ctype, fname, content ) VALUES ( now (), :ctype, :fname, asciiAsBlob('$content') );");
$stmt->bindValue (':ctype', $_FILES['content']['type']);
$stmt->bindValue (':fname', $_FILES['content']['name']);
$stmt->execute ();
To Read
$stmt = $cass_db->prepare ("SELECT blobAsascii(content) as content, ctype, fname FROM $table WHERE id = $id;");
$stmt->execute ();
$data = $stmt->fetchAll();
$fp = fopen('/tmp/'.$data[0]['fname'], 'w');
$content = pack("H*",$data[0]['content']);
fwrite($fp, $content);
fclose($fp);
Upvotes: 1
Reputation: 61
To my knowledge YACassandraPDO not stable. Please check this PHP library http://evseevnn.github.io/php-cassandra-binary/
It is written by me, and how to work with it is similar to the PDO wrapper. Perhaps it will suit you.
Upvotes: 0