【发布时间】:2018-09-11 09:22:07
【问题描述】:
我需要创建一个插件来在管理菜单中添加一个页面。在这个页面中,我想选择一个文件并在上面做一些事情。 我创建了一个带有文件上传字段和提交按钮的简单自定义页面,但我无法管理提交按钮操作。如果有人可以给我一些想法...
感谢 Krishna,我在表单中添加了一个操作,但是当我单击提交按钮时,我可以在我的代码中看到第一条消息(“我已阅读文件...”),但看不到第二条消息(“文件已满”)。
有什么想法吗?
这是我的插件文件定义:fileuploadplugin.php:
<?php
/*
Plugin Name: File upload Plugin
Description: Plugin for uploading file
Version: 1.0
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/*
Action for menu definition
*/
add_action('admin_menu', 'fileuploadplugin_menu');
/*
Menu definition
*/
function fileuploadplugin_menu () {
add_menu_page('File upload plugin', 'File upload', 'manage_options', 'fileuploadlogin', 'file_upload_plugin_page' );
}
/*
Display a page with file input field and submit button
*/
function file_upload_plugin_page () {
if(isset($_POST['submit'])) {
// actions to do on the file : read it, manage it, and so on
echo "I read the file and do what I have to do !";
if(isset($_FILES['filetoupload'])) {
echo "File is filed, I can read it!";
}
}
?>
<div class="container">
<h1>Upload file</h1>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" enctype="multipart/form-data">
<input type='file' id='filetoupload' name='File to upload : '></input>
<?php submit_button('Upload file') ?>
</form>
</div>
<?php
}
?>
【问题讨论】:
-
我测试了你的代码,它打印出
"File is filed, I can read it!"