PHP unserialize serialized data

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 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>';
echo '</pre>';

Array ( [0] => )

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>';
    echo '</pre>';

    jj_readcsv(unserialize ($value[0]),true);
    $link = ob_get_contents();
    $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 .="</script>";
    echo $script;

function enqueue_scripts($hook)
    if ('post.php' != $hook && 'post-edit.php' != $hook && 'post-new.php' != $hook)
    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);

    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:

    update_post_meta($post_id, 'mcu_csv', $mcu_csv[0]);
    jj_readcsv($value, true);