$ _FILES在使用wordpress自定义字段图像上载时为空,但是在核心php网站中有效

Sye*_*Ali 4 php wordpress file-upload

<form enctype="multipart/form-data" method="post" action="uploader.php">
        <input type="file" name="pic" /><br />
        <input type="submit" value="Upload File" />
    </form>

$file_title = $_FILES["pic"]["name"];
echo "$file_title";
Run Code Online (Sandbox Code Playgroud)

在wordpress functions.php文件中;自定义字段方法是:

function credits_meta() {
  global $post;

 $custom = get_post_custom($post->ID);
 $designers = $custom["designers"][0];
 $developers = $custom["developers"][0];
 $producers = $custom["producers"][0];
 ?>

<form method="POST" enctype="multipart/form-data">
 <p><label>Designed By:</label><br />
 <textarea cols="50" rows="5" name="designers"><?php echo $designers; ?></textarea></p>
 <p><label>Built By:</label><br />
 <textarea cols="50" rows="5" name="developers"><?php echo $developers;  ?> </textarea></p>
 <p><label>Upload Image :</label><br />
 <input type="file" name="myPhoto" size="25"/></p>
 </form>

  <?php
  }
function save_details(){
 global $post;

 $target_path = get_bloginfo('template_directory')."/images/";
 $file_title = $_FILES["myPhoto"]["name"];

$new_file_title = "wp_".$file_title;

update_post_meta($post->ID, "year_completed", $_POST["year_completed"]);
update_post_meta($post->ID, "designers", $_POST["designers"]);
update_post_meta($post->ID, "developers", $_POST["developers"]);
update_post_meta($post->ID, "producers", $new_file_title);
}
Run Code Online (Sandbox Code Playgroud)

当我使用核心php尝试上述代码时,它工作正常,但是,当我尝试在wordpress自定义字段图像上传中执行相同操作时:$ _FILESalwasy给出空值。

它给出了图像的名称,如果我使用 $_POST["pic"];

我试图用print_r,var_dumb甚至在wordpress functions.php文件中对此进行检查:

  add_action('init', 'myfunction');
  function myfunction(){
    if($_FILES){

      die("something");
    }
  }
Run Code Online (Sandbox Code Playgroud)

它仍然给空了。我要上传的文件大小为153kb。

我的php.ini文件:

file_uploads = on;
upload_max_filesize = 2M
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

Sye*_*Ali 5

在functions.php文件中添加以下挂钩可以解决该问题。

add_action( 'post_edit_form_tag' , 'post_edit_form_tag' );

function post_edit_form_tag( ) {
   echo ' enctype="multipart/form-data"';
}
Run Code Online (Sandbox Code Playgroud)