dqbn76906 2017-12-11 17:19
浏览 27

使用标题部分下载文件

I'm trying to make some calculations and download the result, for this I'm using headers but the result is pretty big and I'm getting memory issues.

Here is my code headers code:

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");
}

And here is where I'm getting a problem:

$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']));
}

More code, I changed my aproach to line by line and even so I'm getting memory issues. This the code that generate de 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条回答 默认 最新

  • douningle7944 2017-12-11 17:28
    关注

    You can change the memory limit with: ini_set(); like so:

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

    Edit: I agree on @apokryfos. If you want an more detailed solution, we need more code.

    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程