我有一个简单的表单,添加了一个新帖子并设置了一个特色图片。我正在尝试使用以下内容清除表单:header("Location: $_SERVER[PHP_SELF]");
但在页面重新加载后,我收到“已发送标头”错误消息和“未定义索引”。
Notice: Undefined index: attach in /var/www/vhosts/smoige.com/jobs/wp-content/themes/twentyfourteen/page-templates/upload.php on line 20 Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/smoige.com/jobs/wp-includes/post-template.php:478) in /var/www/vhosts/smoige.com/jobs/wp-content/themes/twentyfourteen/page-templates/upload.php on line 53
<?php
/**
* Template Name: Submit
*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
get_header(); ?>
<div id="nav-submit-form" style="margin: 0 0 0 333px;">
<span class="nav-submit-form">
<?php
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if(isset($_POST['submit'])) {
$filename = $_FILES['attach']['name'];
$new_post = array(
'ID' => '',
'post_author' => $current_user->ID,
//'post_category' => array(3322),
'tags_input' => "",
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
$post = get_post($post_id);
$new_post = $post->ID;
if (!function_exists('wp_generate_attachment_metadata')) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $new_post );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($new_post,'_thumbnail_id',$attach_id);
}
header("Location: $_SERVER[PHP_SELF]");
}
?>
<form method="post" enctype="multipart/form-data" action="" id="upload-form">
<input type="text" name="post_title" value="Image Name" size="45" id="input-title"/>
<input type="file" name="attach" id="image" />
<input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
</form>
<?php } else {
echo 'Welcome, visitor!';
}
?>
</span>
</div><!-- #nav-submit-form -->
<?php get_sidebar(); get_footer(); ?>
答案 0 :(得分:2)
$filename = $_POST['attach'];
应该是
$filename = $_FILES['attach']['name'];
http://www.php.net//manual/en/features.file-upload.post-method.php
答案 1 :(得分:1)
标头已发送:
对于“已发送标头”错误,如果已将任何输入发送到浏览器,则无法修改标头(HTML标记,带有header()的内容,文件开头的空格)... < / p>
所以,让我们说你用echo输出一些东西('你好那里')。然后,尝试使用标题重定向用户('位置:...'),它将无法正常工作。
函数头必须修改HTTP头以重定向用户。
“echo”将“hello there”一词与HTTP标头一起发送给用户。如果服务器没有HTTP标头,那么任何东西都不能将服务器留给浏览器,因此PHP只添加了一个。
之后,您尝试使用标题('Location')修改它。那么PHP会告诉你“我很抱歉,但你要我修改标题,我已经把它发送到了页面......太糟糕了。”
上周我写了一篇关于它的文章:http://www.mogosselin.com/warning-cannot-modify-header-information-headers-already-sent-by/如果您需要更多信息。
查找已发送标题的问题
检查错误消息
已经发送的标头(输出开始于 /var/www/vhosts/smoige.com/jobs/wp-includes/的后的template.php:478 强>) 在 /var/www/vhosts/smoige.com/jobs/wp-content/themes/twentyfourteen/page-templates/upload.php 第53行
脚本post-template.php @ line 478中的某些内容尝试输出一些内容,但标题已经发送。
标题是由upload.php中的第53行发送的。
未定义的索引:
对于未定义的索引,这意味着您尝试在数组中使用的INDEX不存在。例如: $ _POST [ '名称']; NAME是INDEX。
如果您尝试这样做: $ var = $ _POST ['FDASFSDA']; 你会得到一个“未定义的索引”,因为$ _POST
中没有设置FDASFSDA答案 2 :(得分:0)
我使用了您的代码,这适用于在文件开头添加此行(可能您的路径不同):
include("../../../wp-load.php");
完整的代码:
<?php
include("../../../wp-load.php");
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if(isset($_POST['submit'])) {
//$filename = $_POST['attach'];
$filename = '';
if(isset($_FILES['attach'])) {
$filename = $_FILES['attach']['name'];
}
$new_post = array(
'ID' => '',
'post_author' => $current_user->ID,
//'post_category' => array(3322),
'tags_input' => "",
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
$post = get_post($post_id);
$new_post = $post->ID;
if (!function_exists('wp_generate_attachment_metadata')) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $new_post );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($new_post,'_thumbnail_id',$attach_id);
}
header("Location: $_SERVER[PHP_SELF]");
}
?>
<form method="post" enctype="multipart/form-data" action="" id="upload-form">
<input type="text" name="post_title" value="Image Name" size="45" id="input-title"/>
<input type="file" name="attach" id="image" />
<input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
</form>
<?php } else {
echo 'Welcome, visitor!';
}
?>
享受您的代码! :)