PHP反序列化序列化数据
问题描述:
我有反序列化序列化数据的问题。PHP反序列化序列化数据
数据被序列化并保存在数据库中。
此数据包含我想要返回给fgetcsv的上传的.csv网址。
fgetcsv需要一个数组,现在给出了一个字符串,所以我需要反序列化数据,但是这给了我错误。
我在网上找到了http://davidwalsh.name/php-serialize-unserialize-issues,但这似乎不起作用。所以我希望有人能告诉我在哪里出错:
以下是错误:
Notice: unserialize() [function.unserialize]: Error at offset 0 of 1 bytes in /xxx/public_html/multi-csv-upload.php on line 163
我发现,这意味着在序列化的数据,使文件损坏的反序列化(",',:,;)
后某些字符
163线:
jj_readcsv(unserialize ($value[0]),true);` // this reads the url of the uploaded csv and tries to open it.
下面是使数据序列化的代码:
update_post_meta($post_id, 'mcu_csv', serialize($mcu_csv));
这是WordPress的
下面是输出的:
echo '<pre>';
print_r(unserialize($value));
echo '</pre>';
Array ( [0] => http://www.domain.country/xxx/uploads/2014/09/test5.csv )
我认为不应该有什么不对来这里的路上。
任何人有一些想法,我可以反序列化这个,所以我可以使用它? 这里是我做的SOFAR ...
public function render_meta_box_content($post)
{
// Add an nonce field so we can check for it later.
wp_nonce_field('mcu_inner_custom_box', 'mcu_inner_custom_box_nonce');
// Use get_post_meta to retrieve an existing value from the database.
$value = get_post_meta($post->ID, 'mcu_images', true);
echo '<pre>';
print_r(unserialize($value));
echo '</pre>';
ob_start();
jj_readcsv(unserialize ($value[0]),true);
$link = ob_get_contents();
ob_end_clean();
$editor_id = 'my_uploaded_csv';
wp_editor($link, $editor_id);
$metabox_content = '<div id="mcu_images"></div><input type="button" onClick="addRow()" value="Voeg CSV toe" class="button" />';
echo $metabox_content;
$images = unserialize($value);
$script = "<script>
itemsCount= 0;";
if (!empty($images))
{
foreach ($images as $image)
{
$script.="addRow('{$image}');";
}
}
$script .="</script>";
echo $script;
}
function enqueue_scripts($hook)
{
if ('post.php' != $hook && 'post-edit.php' != $hook && 'post-new.php' != $hook)
return;
wp_enqueue_script('mcu_script', plugin_dir_url(__FILE__) . 'mcu_script.js', array('jquery'));
}
答
您试图访问序列化的字符串的第一个元素:
jj_readcsv(unserialize ($value[0]),true);
由于字符串是基本字符数组,你试图反序列化序列化字符串的第一个字符。
你需要反序列化月1日再访问数组元素:
//php 5.4+
jj_readcsv(unserialize ($value)[0],true);
//php < 5.4
$unserialized = unserialize ($value);
jj_readcsv($unserialized[0],true);
另外,如果只有过一个元素,不要存放在第一名的数组,只需保存URL字符串,其中犯规需求要序列化:
//save
update_post_meta($post_id, 'mcu_csv', $mcu_csv[0]);
//access
jj_readcsv($value, true);
谢谢。解释和它的工作。仅供参考,该数组将填充更多csv URL到文件。 – Interactive 2014-09-24 11:32:26
@Interactive很高兴我能帮到你。是的,我希望有一个使用数组的原因,但为了以防万一,添加了后面的代码。 – Steve 2014-09-24 11:36:01