我创建了一个插件 import.php
<?php
function fileupload_process() {
ini_set('memory_limit', '64M');
set_time_limit(0);
$uploadfiles = $_FILES['uploadfiles'];
//echo 'ddd';
// exit;
if (is_array($uploadfiles)) {
//echo 'ram';
//print_r($uploadfiles);
echo $uploadfiles['name'];
// foreach ($uploadfiles as $key => $value) {
foreach ($uploadfiles['name'] as $key => $value) {
// look only for uploaded files
if ($uploadfiles['error'][$key] == 0) {
$filetmp = $uploadfiles['tmp_name'][$key];
if (($handle = fopen($filetmp, "r")) !== FALSE) {
$flag = true;
$songs = explode("\n",file_get_contents($filetmp));
$count = count( $songs );
unset($songs);
echo "Total item count: " . $count . "<BR />";
// typical entry: If You Have To Ask,Red Hot Chili Peppers,0:03:37, Rock & Alternative,1991,on
// using a generous 1000 length - will lowering this actually impact performance in terms of memory allocation?
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Skip the first entry in the csv containing colmn info
if($flag) {
$flag = false;
echo "<BR />";
$count--;
continue;
}
// insert the current post and relevant info into the database
$currently_processed = process_custom_post($data);
$count--;
}
echo "Done!";
fclose($handle);
}
unlink($filetmp); // delete the temp csv file
}
}
}
} // END: file_upload_process()
function process_custom_post($song) {
global $wpdb;
// Prepare and insert the custom post
$track = (array_key_exists(0, $song) && $song[0] != "" ? $song[0] : 'N/A');
$custom_post = array();
$custom_post['post_type'] = 'songs';
$custom_post['post_status'] = 'publish';
$custom_post['post_title'] = $track;
$post_id = wp_insert_post( $custom_post );
// Prepare and insert the custom post meta
$meta_keys = array();
$meta_keys['artist_name'] = (array_key_exists(1, $song) && $song[1] != "" ? $song[1] : 'N/A');
$meta_keys['song_length'] = (array_key_exists(2, $song) && $song[2] != "" ? $song[2] : 'N/A');
$meta_keys['song_genre'] = (array_key_exists(3, $song) && $song[3] != "" ? $song[3] : 'N/A');
$meta_keys['song_year'] = (array_key_exists(4, $song) && $song[4] != "" ? $song[4] : 'N/A');
$meta_keys['song_month'] = (array_key_exists(5, $song) && $song[5] != "" ? $song[5] : 'N/A');
$meta_keys['sample_playlist'] = (array_key_exists(6, $song) && $song[6] != "" ? $song[6] : '');
$custom_fields = array();
$place_holders = array();
$query_string = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value) VALUES ";
foreach($meta_keys as $key => $value) {
array_push($custom_fields, $post_id, $key, $value);
$place_holders[] = "('%d', '%s', '%s')";
}
$query_string .= implode(', ', $place_holders);
$wpdb->query( $wpdb->prepare("$query_string ", $custom_fields));
return true;
}// END: process_custom_post()
function import_page () {
//HTML for the import page + the file upload form
if (isset($_POST['uploadfile'])) {
fileupload_process();
}
}
在 main.php
中,我会自定义代码:
<?php
/*
Plugin Name: csv ram
Plugin URI: http://www.newdreamdatasystems.com
Description: csv ram
Version: 2.0
Author: RAM KUMAR
Author URI: http://www.newdreamdatasystems.com
*/
define('SAVEQUERIES', true);
define( 'MY_PLUGIN_ROOT' , dirname(__FILE__) );
include_once( MY_PLUGIN_ROOT . '/import.php');
$ram = import_page();
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'tools.php', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' );
}
function my_custom_submenu_page_callback() {
$html= <<<RAM
<form action="{$ram}" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="">
<label>upload file<input type="file" name="uploadfiles" id="uploadfiles" /></label>
<label><input type="submit" name="uploadfile" id="uploadfile" value="Submit" /></label>
</form>
RAM;
echo $html;
}
但我有一个错误:
警告:在第13行的C:\ xampp \ htdocs \ wordpress \ wp-content \ plugins \ try \ import.php中为foreach()提供的参数无效..?
如何解决这个问题?
答案 0 :(得分:0)
三个主要问题:
函数import_page()
应该在回调函数中运行:
function my_custom_submenu_page_callback() {
import_page();
/* etc */
}
操作<form action="">
应为空,因此会返回到相同的插件页/wp-admin/tools.php?page=my-custom-submenu-page
。
文件字段应允许多次上传,因此这将有效foreach ($uploadfiles as $key => $value)
并删除您看到的错误。
<input type="file" name="uploadfiles[]" id="uploadfiles" multiple />
更好地思考,您可能不需要multiple
属性,但随后调整逻辑的其余部分。
但在进行此调整后,仍有一堆通知和警告,例如Array to string conversion
,Undefined index: name
和fopen(): Filename cannot be empty
。
所有这一切都可以通过仔细调试轻松解决,这就是我发现上述3个问题的方法:
printf( '<pre>%s</pre>', print_r( $INSPECT_VAR, true ) );
以检查有问题的变量。