I have a problem with unserializing serialized data.
The data is serialized and saved in the database.
This data contains a uploaded .csv url that I want to give back to fgetcsv.
fgetcsv expects a array and now a string is given, so I need to unserialize the data but this gives me errors.
I found this online http://davidwalsh.name/php-serialize-unserialize-issues but this doesn't seem to work. So I hope somebody can tell me where I go wrong:
Here is the error:
Notice: unserialize() [function.unserialize]: Error at offset 0 of 1 bytes in /xxx/public_html/multi-csv-upload.php on line 163
I found that this means that there are certain characters in the serialized data that makes the file corrupt after unserialization (",',:,;)
Line 163:
jj_readcsv(unserialize ($value[0]),true);` // this reads the url of the uploaded csv and tries to open it.
Here is the code that makes the data serialized:
update_post_meta($post_id, 'mcu_csv', serialize($mcu_csv));
This is WordPress
Here is the output of:
echo '<pre>';
print_r(unserialize($value));
echo '</pre>';
Array
(
[0] => http://www.domain.country/xxx/uploads/2014/09/test5.csv
)
The way I see it there shouldn't be anything wrong here.
Anybody some idea's how I can unserialize this so I can use it? Here is what I did 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'));
}
You are attempting to access the first element of the serialized string:
jj_readcsv(unserialize ($value[0]),true);
As strings are essentially arrays of chars, you are trying to unserialize the 1st char of the serialized string.
You need to unserialize 1st then access the array element:
//php 5.4+
jj_readcsv(unserialize ($value)[0],true);
//php < 5.4
$unserialized = unserialize ($value);
jj_readcsv($unserialized[0],true);
Alternatively, if there is only ever one element, dont store an array in the 1st place, just save the url string, which doesnt need to be serialized:
//save
update_post_meta($post_id, 'mcu_csv', $mcu_csv[0]);
//access
jj_readcsv($value, true);