使用标题下载部分文件

时间:2017-12-11 17:19:17

标签: php csv header

我正在尝试进行一些计算并下载结果,为此我使用了标题,但结果非常大,而且我遇到了内存问题。

以下是我的代码标头代码:

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

更多代码,我逐行改变了我的方法,即便如此,我也遇到了内存问题。这是生成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 个答案:

答案 0 :(得分:1)

你可以改变内存限制:ini_set();,如下所示:

tags$head(
    tags$a(class="logo", target="_blank",
           href="http://link.com/")
)

编辑: 我同意@apokryfos。如果您想要更详细的解决方案,我们需要更多代码。