【问题标题】:Some BLOB cannot be saved to SQL but some can有些 BLOB 不能保存到 SQL,但有些可以
【发布时间】:2016-02-13 11:44:29
【问题描述】:

我正在尝试通过在 SQL 中使用 BLOB 类型将一些图像保存到 SQL,但是当一些图像(我的图像是 base64 格式并已解码为 BLOB)无法保存到 SQL 但另一个可以时,我遇到了麻烦。MY SQL TABLE

saveFile = function () {
	//window.open(app.stage.toDataURL());
    var dataUrl = app.stage.toDataURL();
	//var name= jQuery("#name").val();
    jQuery.ajax({
        type: "POST",
        url: "saveFile.php",
        data: { image : dataUrl },
        success: function(data){
           alert(data);
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
//echo DIRECTORY_SEPARATOR;
//echo $_SERVER[ 'DOCUMENT_ROOT' ];
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] . DS . 'joomla' );

require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );

$mainframe =& JFactory::getApplication('site');

$db = JFactory::getDBO();

$input = new JInput;
$post = $input->getArray($_POST);
$user = JFactory::getUser();

//echo $user->id;   

if(isset($_POST['image'])){
    $id = $user->id;
    $url = $post['image'];

    //echo $url;
    $image = base64_decode($url);

    echo $image;

    $query = $db->getQuery(true);

    $columns = array('id_user', 'image'); // add more table columns here
    $values = array($db->quote($id), $db->quote($image)); // add more values here

    $query
        ->insert($db->quoteName('image_table2'))
        ->columns($db->quoteName($columns))
        ->values(implode(',', $values));

    $db->setQuery($query);

    //$db->query(); // Use for Joomla 2.5
    $db->execute(); // Use for Joomla 3.x
} ?>

【问题讨论】:

    标签: php sql image blob


    【解决方案1】:

    是的,经过 2 小时的研究,我发现 base64_decode 存在“非常长”的 base64 代码问题,因此解决方案是

    $image = ""; for ($i=0; $i < ceil(strlen($url)/256); $i++) $image = $image . base64_decode(substr(str_replace(' ', '+', $url),$i*256,256));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 2017-01-27
      • 2016-05-16
      相关资源
      最近更新 更多