尝试将文件上传添加到WordPress,除了move_uploaded_file之外,目前一切都运行良好。
<input type="file" name="attachment" id="attachment">
上面表格中的字段
if (!move_uploaded_file($_FILES["attachment"]["tmp_name"],WP_CONTENT_DIR .'/uploads/cv/'.basename($_FILES['attachment']['name'])))
{
$return['error'] = true; $return['error_message'] = __('File could not be uploaded', 'btoa').'<br />';
}
尝试移动上传的文件时,它无效并显示错误。
我尝试使用is_uploaded_file,它也返回false,uploads文件夹是777,它试图保存在/ cv /中的文件夹也是777。
我不太确定这里出了什么问题,没有创建错误日志,我似乎无法让它工作。
除了移动上传的文件外,发送附件的工作也很完美。
我能做什么?
表格代码
<form action="<?php echo home_url() ?>" method="post" id="enquiry-form" enctype="multipart/form-data">
<p><small class="error" style="margin-top: 15px; display: none;"></small></p>
<p><input type="text" value="<?php _e('Your name', 'btoa'); ?>" name="name" id="enquiry-name" onfocus="if(jQuery(this).val() == '<?php _e('Your name', 'btoa'); ?>') { jQuery(this).val(''); }" onblur="if(jQuery(this).val() == '') { jQuery(this).val('<?php _e('Your name', 'btoa'); ?>'); }" /></p>
<p><input type="text" value="<?php _e('Email address', 'btoa'); ?>" name="email" id="enquiry-email" onfocus="if(jQuery(this).val() == '<?php _e('Email address', 'btoa'); ?>') { jQuery(this).val(''); }" onblur="if(jQuery(this).val() == '') { jQuery(this).val('<?php _e('Email address', 'btoa'); ?>'); }" /></p>
<p><label for="file">Upload your CV/Resume:</label></p>
<p style="margin-bottom:0;"><input type="file" name="attachment" id="attachment"></p>
<p><textarea name="message" id="enquiry-message" cols="" rows="4" onfocus="if(jQuery(this).val() == '<?php _e('Message', 'btoa'); ?>') { jQuery(this).val(''); }" onblur="if(jQuery(this).val() == '') { jQuery(this).val('<?php _e('Message', 'btoa'); ?>'); }"><?php _e('Message', 'btoa'); ?></textarea></p>
<p><input type="submit" class="button-primary" value="<?php _e('Send', 'btoa'); ?>" /></p>
<div class="clear"></div>
<!-- /.clear/ -->
<div class="error-message"></div>
<div class="thank-you hidden">
<p><?php _e('Thank you! Your message has been successfully sent.', 'btoa'); ?></p>
</div>
</form>
成功表格代码
//// IF EMAIL IS VALID
if($return['error'] == '') {
/// GETS OTHER VARS
$name = isset($_POST['email']) ? trim($_POST['name']) : '';
$the_message = isset($_POST['message']) ? trim($_POST['message']) : '';
$post_id = isset($_POST['post_id']) ? trim($_POST['post_id']) : '';
//// NOW WE GET OUR SPOT AND THE USER
$spot = get_post($post_id);
$user = get_user_by('id', $spot->post_author);
//// STARTS OUR RECIPIENT ARRAY
$to = $user->user_email;
//// HEADERS
$headers = 'From: '.get_bloginfo('name').' <'.get_option('admin_email').'>'."\r\n".
'Reply-To: '.$email;
$subject = sprintf2(__('%site_name Contact: %spot_name', 'btoa'), array('site_name' => get_bloginfo('name'), 'spot_name' => $spot->post_title));
if (!move_uploaded_file($_FILES["attachment"]["tmp_name"],WP_CONTENT_DIR .'/uploads/cv/'.basename($_FILES['attachment']['name'])))
{
$return['error'] = true; $return['error_message'] = __('File could not be uploaded', 'btoa').'<br />';
error_reporting(E_ERROR | E_WARNING | E_PARSE);
}
$attachments = array(WP_CONTENT_DIR ."/uploads/cv/".basename($_FILES['attachment']['name']));
add_filter( 'wp_mail_content_type', 'my_custom_email_content_type' );
function my_custom_email_content_type() {
return 'text/html';
}
$message = sprintf2(__("Dear %author,
%user (email: %email) has sent you a message via %site_name:
-----
%message", 'btoa'), array(
'author' => $user->display_name,
'user' => $name,
'email' => $email,
'site_name' => get_bloginfo('name'),
'attachment' => $attachments,
'message' => $the_message
));
//// SENDS OUT OUR EMAIL
if(!wp_mail($to, $subject, stripslashes($message), $headers, $attachments)) {
$return['error'] = true; $return['error_email'] = sprintf2(__('There was a problem sending your enquiry. Please email us directly at %site_email', 'btoa'), array('site_email' => get_option('admin_email')));
} else { $return['success'] = stripslashes($headers); }
}
echo json_encode($return);
exit;
}
在尝试修复此错误时忽略稍微混乱的代码。
还尝试在wp_config中添加/调整此项,但仍未在root或wp-content中创建错误日志
// Enable WP_DEBUG mode
define('WP_DEBUG', true);
// Enable Debug logging to the /wp-content/debug.log file
define('WP_DEBUG_LOG', true);
// Disable display of errors and warnings
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors',0);
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define('SCRIPT_DEBUG', true);
答案 0 :(得分:0)
您是否尝试过回显WP_CONTENT_DIR并看到它会得到什么?或者做var_dump(is_dir(WP_CONTENT_DIR .'/uploads/cv/'.basename($_FILES['attachment']['name']))
。同时在$ _FILES数组上执行print_r以查看它返回的实际错误号并将其与:http://us1.php.net//manual/en/features.file-upload.errors.php相关联可能会有所帮助。除非您理解,否则请告诉我各自的结果。