Reputation: 327
Here is a plugin I'm working on. It's task is to parse a CSV file and displaying it as an HTML table on a wordpress page where it's shortcode is present.
When I activate the plugin and visit a page that contains it's shortcode, the page will not load, my browsers (FireFox, Chrome) attempt to load the page and cannot, I don't even get a WSoD; I also check my server's stats when this happens and notice that CPU usage is at 85% which is very abnormal.
Once the plugin is deactivated, CPU usage returns to normal (usually 0-14%) and the page containing the plugin's shortcode will load (but obviously won't load the data requested by the shortcode).
I have absolutely no idea what is causing this.
I have even tried using examples I've found of known working scripts that perform a similar task, and have experienced the same results every time.
The CSV file it's trying to open was generated by Excel 2003, and the CSV file is in the plugin folder.
Here are the contents of the CSV file:
Make, Model, Origin, Color, Miles, Options
Chevy, Tahoe, Delaware, Black, 10000, LX
Ford, F150, Texas, Red, 5000, S
Chevy, Corvette, Utah, Red, 12000, SE
Mazda, Miata, Florida, Blue, 90000, LX
<?php
/*
Plugin Name: CSV 3
Plugin URI:
Description: Displays CSV files as HTML tables
Version: 1.0
Author: Kreation
Author URI:
License:
*/
add_shortcode( "csv", "open_csv_file");
function open_csv_file() {
$file = file_get_contents( plugin_dir_path( __FILE__ ) . 'Book1.csv' );
$handle = fopen("$file", "r");
$data = fgetcsv($handle, 1000, ",");
$color = $data[3];
$options = $data[5];
echo('<table>');
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$color = $data[3];
$options = $data[5];
//generate HTML
echo('<tr data-color="' . $color . '" data-options="' . $options . '">');
foreach ($data as $index=>$val) {
echo('<td>');
echo htmlentities($val, ENT_QUOTES);
echo('</td>');
}
echo('</tr>');
}
echo("</table>");
fclose($handle);
}
?>
Upvotes: 0
Views: 80
Reputation: 4074
Make sure the file is present in the location, try using the plugin_dir_path for the exact directory location. Secondly on every refresh it is extracting all the data in the file, which i guess is not what you want. So you should try to save it in a database and time cache it.
Upvotes: 1