View content from uploaded file in editor

Okay.. I’m really losing it here.

I’m just gonna extract it all so it might be more clear.

In my functions.php:

function jj_readcsv($filename, $header=false) {
$handle = fopen($filename, "r");
echo '<table style="width:100%;">';
//display header row if true
if ($header) {
$csvcontents = fgetcsv($handle);
echo '<tr>';
foreach ($csvcontents as $headercolumn) {
    echo "<th>$headercolumn</th>";
}
echo '</tr>';
}
// displaying contents
while ($csvcontents = fgetcsv($handle)) {
echo '<tr>';
foreach ($csvcontents as $column) {
    echo "<td>$column</td>";
}
echo '</tr>';
}
echo '</table>';
fclose($handle);
}

This is the function that read’s the uploaded file from the database and outputs it in a nice table.

Here is the custom metabox where the file is being uploaded to the server.

<?php 
function add_meta_box_csv() {

// Define the custom attachment for pages
add_meta_box(
    'meta_box_csv',
    'Upload csv',
    'meta_box_csv',
    'page',
    'normal'
);

} // end add_meta_box_csv
add_action('add_meta_boxes', 'add_meta_box_csv');

function meta_box_csv() {
$file = get_post_meta(get_the_ID(), 'meta_box_csv', true);
wp_nonce_field(plugin_basename(__FILE__), 'meta_box_csv_nonce');

$html="<p class="description">";
$html .= 'Selecteer een .csv bestand.';
$html .= '</p>';
echo $html;  
printf( '<p><input type="file" name="meta_box_csv" value="'.$file['file'].'" size="15" /></p>' );


$content = jj_readcsv($file['file'],true);
//$editor_id = 'mycustomeditor';

//wp_editor( $content, $editor_id );
//$csv = array_map('str_getcsv', file('data.csv'));




} // end meta_box_csv

function save_custom_meta_data($id) {

/* --- security verification --- */
if(!wp_verify_nonce($_POST['meta_box_csv_nonce'], plugin_basename(__FILE__))) {
  return $id;
} // end if

if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  return $id;
} // end if

if('page' == $_POST['post_type']) {
  if(!current_user_can('edit_page', $id)) {
    return $id;
  } // end if
} else {
    if(!current_user_can('edit_page', $id)) {
        return $id;
    } // end if
} // end if
/* - end security verification - */

// Make sure the file array isn't empty
if(!empty($_FILES['meta_box_csv']['name'])) {

    // Setup the array of supported file types. In this case, it's just PDF.
    $supported_types = array('text/csv');
    //$supported_types = array('text/comma-separated-values');

    // Get the file type of the upload
    $arr_file_type = wp_check_filetype(basename($_FILES['meta_box_csv']['name']));
    $uploaded_type = $arr_file_type['type'];

    // Check if the type is supported. If not, throw an error.
    if(in_array($uploaded_type, $supported_types)) {

        // Use the WordPress API to upload the file
        $upload = wp_upload_bits($_FILES['meta_box_csv']['name'], null, file_get_contents($_FILES['meta_box_csv']['tmp_name']));

        if(isset($upload['error']) && $upload['error'] != 0) {
            wp_die('Er is een fout ontstaan bij het uploaden: ' . $upload['error']);
        } else {
            add_post_meta($id, 'meta_box_csv', $upload);
            update_post_meta($id, 'meta_box_csv', $upload);    
        } // end if/else

    } else {
        wp_die("Dit is geen .csv bestand");
    } // end if/else

  } // end if

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');


function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
?>

I can output the content of the uploades csv in my metabox.
The problem is that I want to output the content of the csv in a wp_editor box, but to do that I need to have a string with data.

Is there a way where I can store the output data of function jj_readcsv() in a string variable so I can use it to output?

And considering what @TomJNowell said: I can’t treat it like a plain text file.

I hope anyone understands my pain and can help me out!

Much appreciated.
M.

-EDIT-
Okay got it working.
Thnx to: https://stackoverflow.com/users/803518/jlevett
With his sollution on:https://stackoverflow.com/a/6675199/3115317
He gave me this:

ob_start();
jj_readcsv($file['file'],true);
$link = ob_get_contents();
ob_end_clean();
$editor_id = 'my_uploaded_csv';

wp_editor( $link, $editor_id );

And works like a charm.

Leave a Comment

tech