2013-04-06 119 views
1

我遇到麻煩使用Jquery文件上傳引導皮膚(https://github.com/blueimp/jQuery-File-Upload)。該程序本身工作得很好,即使在github(https://github.com/blueimp/jQuery-File-Upload/wiki/Working-with-databases)上描述的數據庫也是功能性的。jquery文件上傳到base64數據庫

但我真正需要的是將圖片以base64格式保存在數據庫中。我似乎無法得到它。所以在add_img函數中,我需要以某種方式獲取base64中的圖片。

// Standard file uploader code 
function add_img($img_name) 
{ 
    $add_to_db = $this->query("INSERT INTO pics (pic_name, pic) VALUES ('".$img_name."','picture in base64 here')"); 
    return $add_to_db; 
} 

回答

1

使用以下代碼將圖像轉換爲base64並將其存儲到數據庫。

$imagedata = file_get_contents("filepath/filename.jpg"); 
$base64data = base64_encode($imagedata); 

編輯:

$file->size = $file_size;//from current code 

$imagedata = file_get_contents($file_path); 
$base64data = base64_encode($imagedata); 
$this->add_img($base64data); 

希望這會爲你工作

+0

這部分我確實媒體鏈接發現。我只是不知道這些數據在文件中的可用位置。因爲在某些時候它必須製作實際的圖片(imagedata),但我看不到在哪裏。 – Matt 2013-04-06 17:11:50

+0

$ imagedata = file_get_contents($ file_path); – 2013-04-06 17:57:12

+0

我看到你在那裏做了什麼。它的工作原理,謝謝! – Matt 2013-04-06 19:13:13