admin管理员组

文章数量:1122846

I'm trying to create an upload form from my admin page via a plugin.

I've cobbled together some code from a few sources, including this one: .html

I get a 500 error when I submit the form and don't understand why or how to debug further.

Is this the wrong way to do a file upload form?

My plugin has two files:

wp-content/plugins/test_upload/test_upload.php

<?php
   /*
   Plugin Name: test_upload
   Version: 1.0
   */

  add_action('admin_menu', 'test_plugin_setup_menu');

  function test_plugin_setup_menu(){
      add_menu_page( 
          'Test Upload', 'Test Upload', 'manage_options', 'test-upload', 'upload_page' 
      );
  }

  function upload_page(){
      ?>
      <h1>Test Upload Form</h1>  

      <form id="upload_form" action="/wp-content/plugins/test_upload/main.php" enctype="multipart/form-data" method="post" target="messages">
        <p><input name="upload" id="upload" type="file" accept="text/csv" /></p>
        <p><input id="btnSubmit" type="submit" value="Upload selected csv file" /></p>
        <iframe name="messages" id="messages"></iframe>
      </form>
  <?php
  }

wp-content/plugins/test_upload/main.php

<?php
//provides access to WP environment
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');  
 
  /* import
  you get the following information for each file:
  $_FILES['field_name']['name']
  $_FILES['field_name']['size']
  $_FILES['field_name']['type']
  $_FILES['field_name']['tmp_name']
 */
  
if($_FILES['upload']['name']) {
  if(!$_FILES['upload']['error']) {
    //validate the file
    $new_file_name = strtolower($_FILES['upload']['tmp_name']);
    //can't be larger than 300 KB 
    if($_FILES['upload']['size'] > (300000)) {
      //wp_die generates a visually appealing message element
      wp_die('Your file size is to large.');
    }
    else {
      //the file has passed the test
      //These files need to be included as dependencies when on the front end.
      require_once( ABSPATH . 'wp-admin/includes/image.php' );
      require_once( ABSPATH . 'wp-admin/includes/file.php' );
      require_once( ABSPATH . 'wp-admin/includes/media.php' );
       
      // Let WordPress handle the upload.
      // Remember, 'upload' is the name of our file input in our form above.
      $file_id = media_handle_upload( 'upload', 0 );
 
      if ( is_wp_error( $file_id ) ) {
         wp_die('Error loading file!');
      } else {
        wp_die('Your menu was successfully imported.');
      }
    }
  }
  else {
    //set that to be the returned message
    wp_die('Error: '.$_FILES['upload']['error']);
  }
} 
?>

I'm trying to create an upload form from my admin page via a plugin.

I've cobbled together some code from a few sources, including this one: https://www.htmlgoodies.com/beyond/cms/create-a-file-uploader-in-wordpress.html

I get a 500 error when I submit the form and don't understand why or how to debug further.

Is this the wrong way to do a file upload form?

My plugin has two files:

wp-content/plugins/test_upload/test_upload.php

<?php
   /*
   Plugin Name: test_upload
   Version: 1.0
   */

  add_action('admin_menu', 'test_plugin_setup_menu');

  function test_plugin_setup_menu(){
      add_menu_page( 
          'Test Upload', 'Test Upload', 'manage_options', 'test-upload', 'upload_page' 
      );
  }

  function upload_page(){
      ?>
      <h1>Test Upload Form</h1>  

      <form id="upload_form" action="/wp-content/plugins/test_upload/main.php" enctype="multipart/form-data" method="post" target="messages">
        <p><input name="upload" id="upload" type="file" accept="text/csv" /></p>
        <p><input id="btnSubmit" type="submit" value="Upload selected csv file" /></p>
        <iframe name="messages" id="messages"></iframe>
      </form>
  <?php
  }

wp-content/plugins/test_upload/main.php

<?php
//provides access to WP environment
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');  
 
  /* import
  you get the following information for each file:
  $_FILES['field_name']['name']
  $_FILES['field_name']['size']
  $_FILES['field_name']['type']
  $_FILES['field_name']['tmp_name']
 */
  
if($_FILES['upload']['name']) {
  if(!$_FILES['upload']['error']) {
    //validate the file
    $new_file_name = strtolower($_FILES['upload']['tmp_name']);
    //can't be larger than 300 KB 
    if($_FILES['upload']['size'] > (300000)) {
      //wp_die generates a visually appealing message element
      wp_die('Your file size is to large.');
    }
    else {
      //the file has passed the test
      //These files need to be included as dependencies when on the front end.
      require_once( ABSPATH . 'wp-admin/includes/image.php' );
      require_once( ABSPATH . 'wp-admin/includes/file.php' );
      require_once( ABSPATH . 'wp-admin/includes/media.php' );
       
      // Let WordPress handle the upload.
      // Remember, 'upload' is the name of our file input in our form above.
      $file_id = media_handle_upload( 'upload', 0 );
 
      if ( is_wp_error( $file_id ) ) {
         wp_die('Error loading file!');
      } else {
        wp_die('Your menu was successfully imported.');
      }
    }
  }
  else {
    //set that to be the returned message
    wp_die('Error: '.$_FILES['upload']['error']);
  }
} 
?>
Share Improve this question asked May 16, 2020 at 10:31 Jesse AldridgeJesse Aldridge 1316 bronze badges 2
  • You could turn on debug and output log file by referring to this post. Then read the log file and find out. – 西門 正 Code Guy - JingCodeGuy Commented May 16, 2020 at 11:22
  • Yeah I have debugging on. There are no errors other than the 500. – Jesse Aldridge Commented May 16, 2020 at 20:11
Add a comment  | 

1 Answer 1

Reset to default 0

Turns out the code is correct, the problem is the tutorial site I copy/pasted from was using weird, non standard space characters instead of normal spaces. I replaced the "fake spaces" with "normal spaces" and now everything works.

In order to debug this I needed to look at the Apache error log rather than the WordPress error log. That eventually led me here: https://stackoverflow.com/questions/11493751/unexpected-t-string-in-if-clause-with-spaces

本文标签: Trying to add admin file upload form plugin