2011-07-31 113 views
0

我想通過使用php的web窗體插入到mysql數據庫的小圖像。但它不起作用。怎麼了?試圖插入圖像到blob字段

形式:圖像

<form action="http://www.yeahthatrocks.com/update2.php" method="post" enctype="multipart/form-data"> 
Game Name: <input name="game_name" type="text" size="25" maxlength="255" /><br></br> 
Release Date: <input name="release_date" type="text" size="25" /><p></p> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
Cover Image: <input type="file" name="cover" id="cover"><br><br> 
<input name="upload" type="submit" id="upload" value="upload" /> 
</form> 

插入查詢:

$fileName = $_FILES['cover']['name']; 
$tmpName = $_FILES['cover']['tmp_name']; 
$fileSize = $_FILES['cover']['size']; 
$fileType = $_FILES['cover']['type']; 

$fp = fopen($tmpName, 'r'); 
$content = fread($fp, filesize($tmpName)); 
$content = addslashes($content); 
fclose($fp); 

if(!get_magic_quotes_gpc()) 
{ 
$fileName = addslashes($fileName); 
} 

$query = "INSERT INTO games set name='".$fileName."', size='".$fileSize."', type='".$fileType."', content='".$content."'"; 
mysql_query($query) ; 
+0

我是半個傾斜問你,如果你的電腦開啓時:)你得到一些錯誤訊息? – aib

+0

是的電腦在...大聲笑。沒有錯誤信息,只是沒有文件上傳。 –

回答

0

這裏有一個簡單的例子,一直爲我工作:

# create table blobs(id int(11) NOT NULL auto_increment, myblob longblob, PRIMARY KEY (id)); 


$host  = "localhost"; 
$username = "blobtest"; 
$password = "blobtest"; 
$database = "blobtest"; 

$link_id = @mysql_connect($host, $username, $password); 
@mysql_select_db($database); 

$blob = file_get_contents('stripe.png'); 
$bdata = addslashes($blob); 

$qry = "INSERT INTO blobs VALUES(NULL, '$bdata')"; 
mysql_query($qry);