gpt4 book ai didi

php - $_FILES 结帐时为空

转载 作者:可可西里 更新时间:2023-10-31 22:45:29 25 4
gpt4 key购买 nike

我在 woocommerce 的结帐表单中添加了一些新字段。它读取完成的 php 文件;

    <form name="checkout" method="post" class="checkout woocommerce-checkout processing" action="http://localhost:100/wordpress/checkout/" enctype="multipart/form-data" style="position: relative; zoom: 1;">
<div id="pagePreview">
<input type="file" name="CheckoutImageUpload">
<div class="BBtextInputFrontend">
<input class="BBTextBoxFront" placeholder="placeholder">
<input class="BBInitialValue BBData" type="text" name="BBInitialValue[]">
</div>
<div class="BBtextInputFrontend">
<input class="BBTextBoxFront" placeholder="placeholder">
<input class="BBInitialValue BBData" type="text" name="BBInitialValue[]">
</div>
</div>

<!-- the rest is the default woocommerce billing inputs -->

<div class="col2-set" id="customer_details">
<div class="col-1">
<div class="woocommerce-billing-fields">
<h3>Billing Details</h3>

问题是输入

<input type="file" name="CheckoutImageUpload">

从不返回 $_FILES 数组中的值。事实上,$_FILES 数组总是返回一个空数组。我可以毫无问题地通过 $_POST 获取其他值。但不是文件。将插件全新安装在另一台单独的计算机上会产生完全相同的结果。

我目前正在使用这段代码来查找值:

function add_image($order_id) {
//if they DID upload a file...

if ($_FILES['CheckoutImageUpload']['name']) {
?>Y<?php
die();
}
else {
?>N<?php
die();
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'add_image', 100, 1);

有人可以帮忙吗?我觉得我快疯了

我在下面添加了提到的完整代码。您在上面看到的是在保留重要部分的同时对其进行了缩短。

  <?php
/*
@package BBPlugin
@wordpress_plugin
Plugin Name: Brave books book preview plugin
Plugin URI: null
Description: Allows the user to single out words to be replaced for a preview in a book.
Author: Goodship
Version: 0.0.2
Author URI: www.Goodship.co.za
*/
// If this file is called directly, abort execution.
if ( ! defined( 'WPINC' ) ) {
die;
}
ini_set('error_reporting', E_ALL);
// This will attach the file needed for the class which defines
// meta boxes, their tabs, views and partial content.
require_once plugin_dir_path( __FILE__ ) . 'admin/class-BBPlugin.php';


/**
The class that represents the meta box that will display
the navigation tabs and each of the fields for the meta box.
*/
require_once plugin_dir_path( __FILE__ ) . 'admin/class-BBPlugin-meta-box.php';


/*
Execute the plugin.

Everything for this particular plugin will be done so from within
the Author_Commentary/admin subpackage. This means that there is no reason to setup
any hooks until we're in the context of the Author_Commentary_Admin class.
@since 0.0.1
*/


/*
This will create an instance of the BBPlugin_Admin class
from the class file mentioned previously as soon as the plugin is activated,
After accepting the plugin name and version parameters.
*/


add_shortcode("BB", "BraveBooksShortCode");
function BraveBooksShortCode( $atts, $content = null , $checkout) {
$inputDiv = '<div class="BBtextInputFrontend">
<input class="BBTextBoxFront" type="text" placeholder="'.$content.'" />
<input class="BBInitialValue BBData" type="text" name="BBInitialValue[]" />
</div>';
return $inputDiv;
}







function Run_BBPlugin() {
$BBPlugin = new BBPlugin_Admin('BB-Plugin', '0.0.1');
$BBPlugin->initialize_hooks();
}

Run_BBPlugin();
wp_register_style( 'postStyles', '/'.'wp-content/plugins/BBPluginv2/admin/assets/css/BBClasses.css' );

wp_enqueue_style('postStyles');

wp_enqueue_script( 'jquery' );

function load_my_script(){
wp_register_script(
'functions',
'/wp-content/plugins/BBPluginv2/admin/assets/js/functions.js' ,
array( 'jquery' )
);
wp_enqueue_script( 'functions' );
}
add_action('wp_enqueue_scripts', 'load_my_script');






function woo_redirect_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
add_filter ('woocommerce_add_to_cart_redirect', 'woo_redirect_to_checkout');

function check_if_cart_has_product( $valid, $product_id, $quantity ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,0);
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

function change_add_to_cart_loop( $product ) {
global $product; // this may not be necessary as it should have pulled the object in already
return '<a href="' . esc_url( $product->get_permalink( $product->id ) ) . '">READ MORE</a>';
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_add_to_cart_loop' );


function woo_custom_cart_button_text() {
return __( 'Buy this book', 'woocommerce' );
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +

function wc_remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );

function wc_add_to_cart_message_filter($message, $product_id = null) {
$message = sprintf( 'Please remember to enter your details before purchase.');
return $message;
}
add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );








// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order , $order_id){
global $woocommerce, $post;?>

<div class="order_data_column">
<h4><?php _e( 'Words used' ); ?></h4>
<?php
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
echo '<p>' .json_encode(get_post_meta($product_id, 'BBPlugin-Pages', true) ). '</p>';
echo '<p>' .json_encode(get_post_meta($post->ID, 'your_key', true) ). '</p>';
}

$pageJSONData = json_encode(get_post_meta($product_id, 'BBPlugin-Pages', true));
$wordsJSONData = json_encode(get_post_meta($post->ID, 'your_key', true));
?>
<script type='text/javascript'>
var pageArray = <?php echo $pageJSONData ?>;
var wordsArray = <?php echo $wordsJSONData ?>;
</script>
<a href="javascript:restructureInput(pageArray, wordsArray)">Create PDF</a>
</div>

<?php
}

add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );


/*
** Getting an image to upload
*/
function add_image($order_id, $posted) {
$sanitized_input_data = array();
$inputsData = $_POST['BBInitialValue'];
$filesData = $_FILES['CheckoutImageUpload'];
$testLog = fopen("testicle.txt","w") or exit ("Unable to open file!");
fwrite ($testLog , "added files: " . $_FILES['CheckoutImageUpload']['name']);



foreach ( $inputsData as $inputsBoxNumber => $inputBoxData ) {
$inputArray = explode( "|", $inputBoxData );
if ( ! empty( $inputBoxData ) ) {

$BBData = array(
'shortcode' => $inputArray[0],
'word_used' => $inputArray[1]
);
fwrite ($testLog , "found files: " . $inputArray[0]);
$sanitized_input_data[ $inputsBoxNumber ] = $BBData;
}
}
fclose ($testLog);

update_post_meta( $order_id, 'your_key', $sanitized_input_data);

//if they DID upload a file...

if ($_FILES['CheckoutImageUpload']['name']) {
//if no errors...
if (!$_FILES['CheckoutImageUpload']['error'] ) {
$valid_file = true;
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['CheckoutImageUpload']['tmp_name'] ); //rename file
if ($_FILES['CheckoutImageUpload']['size'] > ( 1024000 ) ){ //can't be larger than 1 MB
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
echo $message;
die();
}

//if the file has passed the test
if ( $valid_file ) {
//move it to where we want it to be
//copy( $_FILES['CheckoutImageUpload']['tmp_name'], plugin_dir_path( __FILE__ ) . 'admin' );
$message = 'Congratulations! Your file was accepted.';
echo $message;


$BBdirectory = wp_upload_dir();
$BBdirectory = $BBdirectory['path'] .'/'. $order_id .'/';
if (!file_exists($BBdirectory)) {
mkdir($BBdirectory, 0777, true);
if (move_uploaded_file($_FILES['CheckoutImageUpload']['tmp_name'], $BBdirectory . $_FILES["CheckoutImageUpload"]['name'])) {
echo "Uploaded";
die();
} else {
echo "File was not uploaded";
die();
}
}
}
} //if there is an error...
else {
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: ' . $_FILES['CheckoutImageUpload']['error'];
echo $message;
}
}
else {
}
}


add_action( 'woocommerce_checkout_update_order_meta', 'add_image', 99, 2);
//add_action( 'woocommerce_checkout_update_order_meta', 'add_image');
/*
function platoon_add_order_meta( $order_id, $posted ) {
$sanitized_input_data = array();
$inputsData = $_POST['BBInitialValue'];
foreach ( $inputsData as $inputsBoxNumber => $inputBoxData ) {
$inputArray = explode( "|", $inputBoxData );
if ( ! empty( $inputBoxData ) ) {

$BBData = array(
'shortcode' => $inputArray[0],
'word_used' => $inputArray[1]
);

$sanitized_input_data[ $inputsBoxNumber ] = $BBData;
}
}

update_post_meta( $order_id, 'your_key', $sanitized_input_data);
}
add_action( 'woocommerce_checkout_update_order_meta', 'platoon_add_order_meta', 99, 2 );
*/

function add_checkout_notice() {


global $woocommerce;
$items = $woocommerce->cart->get_cart();
$item = end($items)['data']->post->ID;

$pages = get_post_meta( $item, 'BBPlugin-Pages', true );
echo '<div id="pagePreview">';
echo '<input type="file" name="CheckoutImageUpload" />';

foreach ( $pages as $pageNumber=>$pageData ) {

if ($pageData["page_type"] == "text_only"){
$designedData = $pageData["text"];
$designedData = do_shortcode ( $designedData, false );
echo $designedData;
}
else if ($pageData["page_type"] == "2up"){
$designedData = $pageData["text"];
$designedData = do_shortcode ( $designedData, false );
echo $designedData;
}
}
echo '</div>';
?>
<script>
function Test(){
<?php
/*
$testLog = fopen("testicle.txt","w") or exit ("Unable to open file!");
fwrite ($testLog , "added files: " . $_FILES['CheckoutImageUpload'] . $_POST['BBInitialValue']);
fclose ($testLog);
*/
?>
}
</script>

<a onclick="Test()" class="btn">Call PHP Function</a>
<?php
}
add_action( 'woocommerce_checkout_before_customer_details', 'add_checkout_notice');

/*
** end of image upload
*/

?>

我还包含了下面用于调试的代码,它也没有返回任何内容,因此它不是该操作独有的。

?>
<script>
function Test(){
<?php
$testLog = fopen("testicle.txt","w") or exit ("Unable to open file!");
fwrite ($testLog , "added files: " . $_FILES);
fclose ($testLog);
?>
}
</script>

<a onclick="Test()" class="btn">Call PHP Function</a>
<?php

最佳答案

"@Fred -ii- I used that link you added to get all the errors and I received this error: [Thu Mar 31 12:23:09.121930 2016] [:error] [pid 11208:tid 1248] [client 127.0.0.1:51335] PHP Notice: Undefined index: CheckoutImageUpload in Z:\Work\J00028 - Brave books plugin\Wordpress stack\apps\wordpress\htdocs\wp-content\plugins\BBPluginv2\BBPlugin.php on line 290, referer: http://localhost:100/wordpress/product/a-book/ Does this help? – Captain Dando"

您的文件的名称属性是 name="checkoutupload" 但您在整个代码中使用了 $_FILES['CheckoutImageUpload']

因此,为了避免将所有 $_FILES['CheckoutImageUpload'] 更改为命名属性,只需将文件名属性更改为 name="CheckoutImageUpload".

还要确保您要上传到的文件夹具有正确的路径,并且具有适当的写入权限。

关于php - $_FILES 结帐时为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36287005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com