1--TEST-- 2MySQL PDOStatement - inserting BLOB from stream 3--EXTENSIONS-- 4pdo_mysql 5--SKIPIF-- 6<?php 7require_once __DIR__ . '/inc/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__ . '/inc/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 $sql = sprintf('CREATE TABLE test_stmt_blobfromstream(id INT, label BLOB) ENGINE=%s', PDO_MYSQL_TEST_ENGINE); 57 $db->exec($sql); 58 59 if (!$stmt = $db->prepare('INSERT INTO test_stmt_blobfromstream(id, label) VALUES (?, ?)')) { 60 printf("[%03d + 4] %s\n", $offset, var_export($db->errorInfo(), true)); 61 return false; 62 } 63 64 $fp = fopen($file, 'r'); 65 if (!$fp) { 66 printf("[%03d + 5] Cannot create test file '%s'\n", $offset, $file); 67 return false; 68 } 69 70 71 $id = 1; 72 $stmt->bindParam(1, $id); 73 if (true !== ($tmp = $stmt->bindParam(2, $fp, PDO::PARAM_LOB))) { 74 printf("[%03d + 6] Expecting true, got %s. %s\n", 75 $offset, 76 var_export($tmp, true), 77 var_export($db->errorInfo(), true)); 78 return false; 79 } 80 81 if (true !== $stmt->execute()) { 82 printf("[%03d + 7] Failed to INSERT data, %s\n", $offset, var_export($stmt->errorInfo(), true)); 83 return false; 84 } 85 86 $stmt2 = $db->query('SELECT id, label FROM test_stmt_blobfromstream WHERE id = 1'); 87 $row = $stmt2->fetch(PDO::FETCH_ASSOC); 88 if ($row['label'] != $blob) { 89 printf("[%03d + 8] INSERT and/or SELECT has failed, dumping data.\n", $offset); 90 var_dump($row); 91 var_dump($blob); 92 return false; 93 } 94 95 // Lets test the chr(0) handling in case the streaming has failed: 96 // is the bug about chr(0) or the streaming... 97 $db->exec('DELETE FROM test_stmt_blobfromstream'); 98 $stmt = $db->prepare('INSERT INTO test_stmt_blobfromstream(id, label) VALUES (?, ?)'); 99 $stmt->bindParam(1, $id); 100 $stmt->bindParam(2, $blob); 101 if (true !== $stmt->execute()) 102 printf("[%03d + 9] %s\n", $offset, var_export($stmt->errorInfo(), true)); 103 104 $stmt2 = $db->query('SELECT id, label FROM test_stmt_blobfromstream WHERE id = 1'); 105 $row = $stmt2->fetch(PDO::FETCH_ASSOC); 106 if ($row['label'] != $blob) { 107 printf("[%03d + 10] INSERT and/or SELECT has failed, dumping data.\n", $offset); 108 var_dump($row); 109 var_dump($blob); 110 return false; 111 } 112 113 return true; 114 } 115 116 $db = MySQLPDOTest::factory(); 117 $blob = 'I am a mighty BLOB!' . chr(0) . "I am a binary thingie!"; 118 $tmp = MySQLPDOTest::getTempDir(); 119 $file = $tmp . DIRECTORY_SEPARATOR . 'pdoblob.tst'; 120 121 try { 122 123 printf("Emulated PS...\n"); 124 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1); 125 blob_from_stream(10, $db, $file, $blob); 126 127 $db->exec('DROP TABLE IF EXISTS test_stmt_blobfromstream'); 128 129 printf("Native PS...\n"); 130 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); 131 blob_from_stream(30, $db, $file, $blob); 132 133 } catch (PDOException $e) { 134 printf("[001] %s [%s] %s\n", 135 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 136 } 137 138 print "done!"; 139?> 140--CLEAN-- 141<?php 142require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 143$db = MySQLPDOTest::factory(); 144$db->exec('DROP TABLE IF EXISTS test_stmt_blobfromstream'); 145@unlink(MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR . 'pdoblob.tst'); 146?> 147--EXPECT-- 148Emulated PS... 149Native PS... 150done! 151