Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Foreach$文件创建post如何_Php_Wordpress - Fatal编程技术网

Php Foreach$文件创建post如何

Php Foreach$文件创建post如何,php,wordpress,Php,Wordpress,我知道这可能非常简单,我忽略了这里的一些东西 我在我们网站的前端有一个表单,用户可以通过表单上传图片,然后在我们的商店里创建产品。我们正在使用Wordpress和Woocommerce,但我觉得这个问题足够笼统,可以在这里提问 目前,上传一张图片时一切正常,当用户上传多张图片时,我该如何处理 以我改变的形式 到 ,我认为用这样的方式包装下面的内容会有用: if ($_FILES) { foreach ($_FILES as $file => $array) { // do n

我知道这可能非常简单,我忽略了这里的一些东西

我在我们网站的前端有一个表单,用户可以通过表单上传图片,然后在我们的商店里创建产品。我们正在使用Wordpress和Woocommerce,但我觉得这个问题足够笼统,可以在这里提问

目前,上传一张图片时一切正常,当用户上传多张图片时,我该如何处理

以我改变的形式
,我认为用这样的方式包装下面的内容会有用:

if ($_FILES) {
  foreach ($_FILES as $file => $array) {
    // do necessary code
  }
}
但事实并非如此

以下是我创建帖子的内容。我不知道为什么在foreach$文件中包装它不起作用

$current_user = wp_get_current_user();

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {


        $postTitle = trim($_POST['postTitle']);


    if($post_id)
    {
        wp_redirect(home_url());
        exit;
    }

    //random sku
$skuu = rand();

$new_post = array(
'post_title' => $skuu,
'post_content' => '', //add anything here like link to vendor or whatever
'post_status' => 'publish',
'post_type' => 'product'
);


//insert and update post
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, '_sku', $skuu );

if ($_FILES) {
  foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$post_id);
  }
}

//find available attributes
$avail_attributes = Array(
    'high-resolution',
    'medium-resolution',
    'low-resolution'
);

//set terms (variations and attributes)
wp_set_object_terms ($post_id, 'variable', 'product_type');
wp_set_object_terms( $post_id, $avail_attributes, 'pa_resolution' );


//Categories
wp_set_object_terms( $post_id, array(esc_attr(strip_tags($_POST['postCat'])), esc_attr(strip_tags($_POST['raceCat']))), 'product_cat' );

// set variable data
$thedata = Array('pa_resolution'=>Array(
'name'=>'pa_resolution',
'value'=>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));

update_post_meta( $post_id,'_product_attributes',$thedata);
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
//update_post_meta( $post_id, '_product_vendors_commission', '25');

//insert variations post_type
$i=1;
while ($i<=3) {
$my_post = array(
      'post_title'    => 'Variation #' . $i . ' of ' . esc_attr(strip_tags($_POST['postTitle'])),
      'post_name'     => 'product-' . $post_id . '-variation-' . $i,
      'post_status'   => 'publish',
      'post_parent'   => $post_id,
      'post_type'     => 'product_variation',
      'guid'          =>  home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $i,
      'post_author'   => $vendor_data->term_id
    );

    // Insert the post into the database
    wp_insert_post( $my_post );

    $variable_id = $post_id + 2;
    $variable_two = $variable_id + 1;
    $variable_three = $variable_two + 1;

    //get user prices
    global $current_user;
    $low_price = get_user_meta($current_user->ID, '_low_price', true);
    $medium_price = get_user_meta($current_user->ID, '_medium_price', true);
    $high_price = get_user_meta($current_user->ID, '_high_price', true);

    //downloadable file paths
    $download_id = get_post_thumbnail_id( $post_id );
    $lowRes_src  = wp_get_attachment_image_src( $download_id, 'low-resolution' );
    $medRes_src  = wp_get_attachment_image_src( $download_id, 'medium-resolution' );
    $highRes_src  = wp_get_attachment_image_src( $download_id, 'high-resolution' );

    $low_downloadable_images = array();
    $medium_downloadable_images = array();
    $high_downloadable_images = array();

    foreach ($lowRes_src as $lowRes_url) {
        $lowRes_url = $lowRes_src[0];
        $low_downloadable_images[md5( $lowRes_url )] = $lowRes_url;
    }

    foreach ($medRes_src as $medRes_url) {
        $medRes_url = $medRes_src[0];
        $medium_downloadable_images[md5( $medRes_url )] = $medRes_url;
    }

    foreach ($highRes_src as $highRes_url) {
        $highRes_url = $highRes_src[0];
        $high_downloadable_images[md5( $highRes_url )] = $highRes_url;
    }

    //echo $rand;

    update_post_meta( $variable_id, 'attribute_pa_resolution', 'high-resolution');
    update_post_meta( $variable_id, '_price', $high_price );
    update_post_meta( $variable_id, '_regular_price', $high_price);
    update_post_meta( $variable_id, '_virtual', 'yes');
    update_post_meta( $variable_id, '_downloadable', 'yes');
    update_post_meta( $variable_id, '_file_paths', $high_downloadable_images);

    update_post_meta( $variable_two, 'attribute_pa_resolution', 'medium-resolution');
    update_post_meta( $variable_two, '_price', $medium_price );
    update_post_meta( $variable_two, '_regular_price', $medium_price);
    update_post_meta( $variable_two, '_virtual', 'yes');
    update_post_meta( $variable_two, '_downloadable', 'yes');
    update_post_meta( $variable_two, '_file_paths', $medium_downloadable_images);

    update_post_meta( $variable_three, 'attribute_pa_resolution', 'low-resolution');
    update_post_meta( $variable_three, '_price', $low_price );
    update_post_meta( $variable_three, '_regular_price', $low_price);
    update_post_meta( $variable_three, '_virtual', 'yes');
    update_post_meta( $variable_three, '_downloadable', 'yes');
    update_post_meta( $variable_three, '_file_paths', $low_downloadable_images);

    $i++;
    }


}

//attach vendor to product
$vendor = get_user_vendor();
if( isset( $vendor->slug ) && strlen( $vendor->slug ) > 0 ) {
    wp_set_object_terms( $post_id, $vendor->slug, 'shop_vendor', false );
}
$current_user=wp_get_current_user();
如果(isset($发布['submitted'])和&isset($发布['POST\u nonce\u field'])和&wp\u验证当前($发布['POST\u nonce\u field'],'POST\u nonce')){
$postTitle=trim($_POST['postTitle']);
如果($post_id)
{
wp_重定向(home_url());
出口
}
//随机单品
$skuu=rand();
$new\u post=数组(
“post_title”=>$skuu,
'发布内容'=>'',//在此处添加任何内容,如指向供应商的链接或其他内容
“发布状态”=>“发布”,
“post_类型”=>“产品”
);
//插入并更新帖子
$post\u id=wp\u insert\u post($new\u post);
更新发布元数据($post\u id,'u sku',$sku);
如果($\u文件){
foreach($\作为$file=>$array的文件){
$newupload=插入附件($file,$post\u id);
}
}
//查找可用属性
$avail\u attributes=数组(
“高分辨率”,
“中等分辨率”,
“低分辨率”
);
//设置术语(变体和属性)
wp_set_object_术语($post_id,'variable','product_type');
wp_set_object_terms($post_id,$avail_attributes,'pa_resolution');
//类别
wp_set_object_terms($post_id,数组(esc_attr(strip_标签($post['poscat')))、esc_attr(strip_标签($post['raceCat')))、product_cat');
//设置变量数据
$thedata=Array('pa_resolution'=>Array(
'name'=>'pa_分辨率',
'值'=>'',
'是否可见'=>'1',
'is_variation'=>'1',
'is_分类法'=>'1'
));
更新发布元($post\u id、“'u product\u attributes'、$thedata);
更新发布元($post\u id、'u可见性、'visible');
更新发布元数据($post\u id、'u stock\u status、'instock');
//更新发布meta($post\u id、'u product\u vendors\u commission、'25');
//插入后U类型的变体
$i=1;
而($i‘Variation#’。$i。'of'.esc_attr(strip_标签($_POST['postTitle])),
“post_name”=>“product-”.$post_id.-variation-”.$i,
“发布状态”=>“发布”,
“post\u parent”=>$post\u id,
“后类型”=>“产品变化”,
'guid'=>home\u url()。/?product\u variation=product-'.$post\u id'.-variation-'.$i,
“发布作者”=>$vendor\u data->term\u id
);
//将帖子插入数据库
wp_insert_post($my_post);
$variable_id=$post_id+2;
$variable\u two=$variable\u id+1;
$variable_three=$variable_three+1;
//获取用户价格
全局$当前用户;
$low_price=get_user_meta($current_user->ID,'u low_price',true);
$medium\u price=get\u user\u meta($current\u user->ID,'u medium\u price',true);
$high\u price=get\u user\u meta($current\u user->ID,'u high\u price',true);
//可下载文件路径
$download\u id=get\u post\u缩略图\u id($post\u id);
$lowRes_src=wp_get_attachment_image_src($download_id,'low resolution');
$medRes_src=wp_get_attachment_image_src($download_id,'medium resolution');
$highRes_src=wp_get_attachment_image_src($download_id,'high-resolution');
$low_可下载_图像=数组();
$medium_downloadable_images=array();
$high_可下载_图像=数组();
foreach($lowRes\u src作为$lowRes\u url){
$lowRes_url=$lowRes_src[0];
$low_可下载_图像[md5($lowRes_url)]=$lowRes_url;
}
foreach($medRes\U src作为$medRes\U url){
$medRes_url=$medRes_src[0];
$medium_可下载图片[md5($medRes_url)]=$medRes_url;
}
foreach($highRes\u src作为$highRes\u url){
$highRes_url=$highRes_src[0];
$high_可下载_图像[md5($highRes_url)]=$highRes_url;
}
//埃科$兰特;
更新后元($variable_id,'attribute_pa_resolution','high-resolution');
更新后元($variable\u id,''u price',$high\u price);
更新后元($variable\u id,''u regular\u price',$high\u price);
更新后元($variable\u id、'u virtual、'yes');
更新发布元数据($variable\u id,''u downloadable','yes');
更新发布元($variable\u id,''u file\u path',$high\u downloadable\u image);
更新_post_meta($variable_two,'attribute_pa_resolution','medium resolution');
更新后元($variable\u two,$price',$medium\u price);
更新后元($variable\u two,$regular\u price,$medium\u price);
更新后置元($variable'u two','u virtual','yes');
更新后元($variable'u two','u downloadable','yes');
更新发布meta($variable\u two,'u file\u path',$medium\u downloadable\u image);
更新后元($variable_three,'attribute_pa_resolution','low resolution');
更新后元($variable\u three,$price',$low\u price);
更新后元($variable\u three,$regular\u price,$low\u price);
更新后元($variable三,''u virtual','yes');
更新后元($variable三,''u downloadable','yes');
更新后置元($variable_three,'u file_path',$low_downloadable_images);
$i++;
}
}
//将供应商附加到产品
$vendor=get_user_vendor();
如果(isset($vendor->slug)和&strlen($vendor->slug)>0){
wp_set_object_terms($post_id,$vendor->slug,'shop_vendor',false);
}

简言之,我只需要一种方法,上面的代码创建每个上传的文件后,而不是像现在这样只上传一个文件。。我没有主意了

明白了,所以我把输入文件的名称从缩略图改为缩略图[]。然后我将上面的粘贴包在一个函数中,并在t处添加了这个部分
if ( $_FILES ) {
    $files = $_FILES['thumbnail'];
        foreach ($files['name'] as $key => $value) {
          if ($files['name'][$key]) {
          $file = array(
          'name' => $files['name'][$key],
          'type' => $files['type'][$key],
          'tmp_name' => $files['tmp_name'][$key],
          'error' => $files['error'][$key],
          'size' => $files['size'][$key]
          );

              $_FILES = array("" => $file);

              foreach ($_FILES as $file => $array) {
                      //call my function
                  create_var_product($file, $post_id);
              }
          }
    }
}