2017-12-11 8 views
0

いくつかの計算を行い、結果をダウンロードしようとしています。このためにヘッダーを使用していますが、結果はかなり大きく、メモリの問題が発生しています。ヘッダーを使用してファイルを部分的にダウンロードする

ここ

は私のコードヘッダコードです:私が問題になってるところ

function download_send_headers($filename) { 
    // disable caching 
    $now = gmdate("D, d M Y H:i:s"); 
    header("Expires: Tue, 03 Jul 2020 06:00:00 GMT"); 
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); 
    header("Last-Modified: {$now} GMT"); 

    // force download 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 

    // disposition/encoding on response body 
    header("Content-Disposition: attachment;filename={$filename}"); 
    header("Content-Transfer-Encoding: binary"); 
} 

そして、ここでは、次のとおりです。

$export_model->download_send_headers('tainacan_csv.csv'); 
foreach($objects as $object) 
{ 
     $csv_data = $export_model->generate_csv_data($data, $object); 
     echo utf8_decode($export_model->array2csv($csv_data, $data['socialdb_delimiter_csv'])); 
} 

もっとコードを、私は行ごとに私のaproachを変更し、そうであっても私は記憶上の問題を抱えています。これは、CSVを生成するコードです。

public function generate_csv_data($data) { 

    $df = fopen("php://output", 'w'); 

    $propertyModel = new PropertyModel; 
    $objects = $this->get_collection_posts($data['collection_id']); 
    $facets = CollectionModel::get_facets($data['collection_id']); 

    //fputcsv($df, array_keys(reset($objects)), $data['socialdb_delimiter_csv']); 

    foreach ($objects as $object) { 
     $csv_data = []; 
     if ($object->ID == $data['collection_id']) { 
      continue; 
     } 

     /** ID * */ 
     if ($object->ID != "") { 
      $csv_data['ID'] = $object->ID; 
     } 

     /** Title * */ 
     if ($object->post_title != "") { 
      $value = $object->post_title; 
      if(mb_detect_encoding($value)==='UTF-8'){ 
       $value = utf8_decode($value); 
      } 
      $csv_data['title'] = $value; 
     } else { 
      $csv_data['title'] = ''; 
     } 

     /** Description * */ 
     if ($object->post_content != "") { 
      $value = $object->post_content; 
      if(mb_detect_encoding($value)==='UTF-8'){ 
       $value = utf8_decode($value); 
      } 
      $csv_data['description'] = $value; 
     } else { 
      $csv_data['description'] = ''; 
     } 

     /** Content * */ 
     if (get_post_meta($object->ID, 'socialdb_object_content', true) != "") { 
      $csv_data['content'] = utf8_decode(get_post_meta($object->ID, 'socialdb_object_content', true)); 
      if ($csv_data['content'] != '' && is_numeric($csv_data['content'])) { 
       $csv_data['content'] = wp_get_attachment_url($csv_data['content']); 
      } 
     } else { 
      $csv_data['content'] = ''; 
     } 

     /** Origin * */ 
     if (get_post_meta($object->ID, 'socialdb_object_from')) { 
      $csv_data['item_from'] = get_post_meta($object->ID, 'socialdb_object_from', true); 
     } 

     /** Type * */ 
     if (get_post_meta($object->ID, 'socialdb_object_dc_type')) { 
      $csv_data['item_type'] = get_post_meta($object->ID, 'socialdb_object_dc_type', true); 
     } 

     /** Source * */ 
     if (get_post_meta($object->ID, 'socialdb_object_dc_source')) { 
      $value = get_post_meta($object->ID, 'socialdb_object_dc_source', true); 
      if(mb_detect_encoding($value)==='UTF-8'){ 
       $value = utf8_decode($value); 
      } 
      $csv_data['item_source'] = $value; 
     } 

     /** URL * */ 
     if (get_post_meta($object->ID, 'socialdb_uri_imported')) { 
      $csv_data['permalink'] = get_post_meta($object->ID, 'socialdb_uri_imported', true); 
     } else { 
      $csv_data['permalink'] = get_the_permalink($data['collection_id']) . '?object_id=' . $object->ID; 
     } 

     /** Tags * */ 
     $tags = wp_get_object_terms($object->ID, 'socialdb_tag_type', array('fields' => 'names')); 
     if (!empty($tags)) { 
      $csv_data['tags'] = utf8_decode(implode('||', $tags)); 
     } else { 
      $csv_data['tags'] = ''; 
     } 

     /** Categories * */ 
     $categories_of_facet = array(); 
     $category_model = new CategoryModel; 
     $categories = wp_get_object_terms($object->ID, 'socialdb_category_type'); 
     if (is_array($categories)): 
      foreach ($categories as $category) { 
       $facet_id = $category_model->get_category_facet_parent($category->term_id, $data['collection_id']); 
       if (!isset($facet_id) || $facet_id == $category->term_id) { 
        continue; 
       } 
       $categories_of_facet[$facet_id][] = $this->get_hierarchy_names($category->term_id, $facet_id); 
      } 
     endif; 

     if ($facets) { 
      foreach ($facets as $facet) { 
       $term = get_term_by('id', $facet, 'socialdb_category_type'); 
       if (is_array($categories_of_facet[$facet])): 
        $csv_data[utf8_decode($term->name)] = utf8_decode(implode(', ', $categories_of_facet[$facet])); 
       else: 
        $csv_data[utf8_decode($term->name)] = ''; 
       endif; 
      } 
     } 

     /** Propriedades de Atributos * */ 
     $root_category = $this->get_category_root_of($data['collection_id']); 

     //$all_properties_id = get_term_meta($root_category, 'socialdb_category_property_id'); 
     $all_properties_id = array_unique($this->get_parent_properties($root_category, [], $root_category)); 
     if ($all_properties_id) { 
      foreach ($all_properties_id as $property_id) { 
       $property = get_term_by("id", $property_id, "socialdb_property_type"); 
       if (in_array($property->slug, $this->fixed_slugs)): 
        continue; 
       endif; 
       $type = $propertyModel->get_property_type($property_id); // pego o tipo da propriedade 
       if ($type == 'socialdb_property_data') { 
        $value = get_post_meta($object->ID, 'socialdb_property_' . $property_id, true); 
        if(mb_detect_encoding($value)==='UTF-8'){ 
         $value = utf8_decode($value); 
        } 
        $csv_data[utf8_decode($property->name)] = get_post_meta($object->ID, 'socialdb_property_' . $property_id, true); 
       } elseif ($type == 'socialdb_property_object') { 
        $property_result_meta_value = get_post_meta($object->ID, 'socialdb_property_' . $property_id); 
        if (is_array($property_result_meta_value) && $property_result_meta_value[0] != '') { 
         foreach ($property_result_meta_value as $property_meta_value) { 
          $array_property_name[] = get_post($property_meta_value)->post_title; 
         } 
         $csv_data[utf8_decode($property->name)] = utf8_decode(implode(', ', $array_property_name)); 
        } else { 
         $csv_data[utf8_decode($property->name)] = ''; 
        } 
       } 
      } 
      $array_property_name = []; 
     } 

     /** Arquivos * */ 
     $array_files = $this->list_files_to_export($object->ID); 
     if ($array_files) { 
      $csv_data['Files'] = implode(', ', $array_files); 
     } else { 
      $csv_data['Files'] = ''; 
     } 


     /**    * ************************** */ 
     //$csv[] = $csv_data; 

     fputcsv($df, $csv_data, $data['socialdb_delimiter_csv']); 
     unset($csv_data); 
    } 
    fclose($df); 
    //return $csv; 
} 
+1

おそらく 'generate_csv_data'は、あなたがCSVに変換する大きな配列を生成するものです。理想的には、 'generate_csv_data'が実際のCSVデータを生成し、代わりに行ごとにエコーする必要があります。 – apokryfos

+0

私はこのような行を1行ずつ試しましたが、それでもApacheはcosumeメモリを停止しません。消費されたメモリ580 MBで終了します –

+0

出力バッファリングが有効になっている場合は、無効にする必要があります。 – apokryfos

答えて

1

あなたはしてメモリ制限を変更することができます。

ini_set('memory_limit', '2048M'); 

編集:私は@apokryfosに同意 そうのようなini_set();。より詳細なソリューションが必要な場合は、より多くのコードが必要です。

+0

これは「それを投げる」ソリューションとして知られています。残念ながら誰もがそれを買う余裕はない。 (この文脈では、資金は有限の資源を象徴している) – apokryfos

+0

そして、私にとっては役に立たない –

関連する問題