1--TEST-- 2MySQL PDOStatement - inserting BLOB from stream 3--EXTENSIONS-- 4pdo_mysql 5--SKIPIF-- 6<?php 7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 8MySQLPDOTest::skip(); 9 10$tmp = MySQLPDOTest::getTempDir(); 11if (!$tmp) 12 die("skip Can't create temporary file"); 13 14$file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst'; 15$fp = fopen($file, 'w'); 16if (!$fp) 17 die("skip Can't create temporary file"); 18 19if (4 != fwrite($fp, 'test')) { 20 die("skip Can't create temporary file"); 21} 22fclose($fp); 23clearstatcache(); 24 25if (!file_exists($file)) 26 die("skip Can't create temporary file"); 27 28unlink($file); 29?> 30--FILE-- 31<?php 32 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 33 34 function blob_from_stream($offset, $db, $file, $blob) { 35 36 @unlink($file); 37 clearstatcache(); 38 if (file_exists($file)) { 39 printf("[%03d + 1] Cannot remove old test file\n", $offset); 40 return false; 41 } 42 43 $fp = fopen($file, 'w'); 44 if (!$fp || !fwrite($fp, $blob)) { 45 printf("[%03d + 2] Cannot create test file '%s'\n", $offset, $file); 46 return false; 47 } 48 49 fclose($fp); 50 clearstatcache(); 51 if (!file_exists($file)) { 52 printf("[%03d + 3] Failed to create test file '%s'\n", $offset, $file); 53 return false; 54 } 55 56 $db->exec('DROP TABLE IF EXISTS test'); 57 $sql = sprintf('CREATE TABLE test(id INT, label BLOB) ENGINE=%s', PDO_MYSQL_TEST_ENGINE); 58 $db->exec($sql); 59 60 if (!$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)')) { 61 printf("[%03d + 4] %s\n", $offset, var_export($db->errorInfo(), true)); 62 return false; 63 } 64 65 $fp = fopen($file, 'r'); 66 if (!$fp) { 67 printf("[%03d + 5] Cannot create test file '%s'\n", $offset, $file); 68 return false; 69 } 70 71 72 $id = 1; 73 $stmt->bindParam(1, $id); 74 if (true !== ($tmp = $stmt->bindParam(2, $fp, PDO::PARAM_LOB))) { 75 printf("[%03d + 6] Expecting true, got %s. %s\n", 76 $offset, 77 var_export($tmp, true), 78 var_export($db->errorInfo(), true)); 79 return false; 80 } 81 82 if (true !== $stmt->execute()) { 83 printf("[%03d + 7] Failed to INSERT data, %s\n", $offset, var_export($stmt->errorInfo(), true)); 84 return false; 85 } 86 87 $stmt2 = $db->query('SELECT id, label FROM test WHERE id = 1'); 88 $row = $stmt2->fetch(PDO::FETCH_ASSOC); 89 if ($row['label'] != $blob) { 90 printf("[%03d + 8] INSERT and/or SELECT has failed, dumping data.\n", $offset); 91 var_dump($row); 92 var_dump($blob); 93 return false; 94 } 95 96 // Lets test the chr(0) handling in case the streaming has failed: 97 // is the bug about chr(0) or the streaming... 98 $db->exec('DELETE FROM test'); 99 $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)'); 100 $stmt->bindParam(1, $id); 101 $stmt->bindParam(2, $blob); 102 if (true !== $stmt->execute()) 103 printf("[%03d + 9] %s\n", $offset, var_export($stmt->errorInfo(), true)); 104 105 $stmt2 = $db->query('SELECT id, label FROM test WHERE id = 1'); 106 $row = $stmt2->fetch(PDO::FETCH_ASSOC); 107 if ($row['label'] != $blob) { 108 printf("[%03d + 10] INSERT and/or SELECT has failed, dumping data.\n", $offset); 109 var_dump($row); 110 var_dump($blob); 111 return false; 112 } 113 114 return true; 115 } 116 117 $db = MySQLPDOTest::factory(); 118 $blob = 'I am a mighty BLOB!' . chr(0) . "I am a binary thingie!"; 119 $tmp = MySQLPDOTest::getTempDir(); 120 $file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst'; 121 122 try { 123 124 printf("Emulated PS...\n"); 125 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1); 126 blob_from_stream(10, $db, $file, $blob); 127 128 printf("Native PS...\n"); 129 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); 130 blob_from_stream(30, $db, $file, $blob); 131 132 } catch (PDOException $e) { 133 printf("[001] %s [%s] %s\n", 134 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 135 } 136 137 print "done!"; 138?> 139--CLEAN-- 140<?php 141require __DIR__ . '/mysql_pdo_test.inc'; 142$db = MySQLPDOTest::factory(); 143$db->exec('DROP TABLE IF EXISTS test'); 144@unlink(MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR . 'pdoblob.tst'); 145?> 146--EXPECT-- 147Emulated PS... 148Native PS... 149done! 150