Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Descarga de matriz a archivo CSV en Laravel 5.1

Tengo una matriz y estoy tratando de descargarla como archivo csv, mi matriz se ve así

 Array( [0] => Array ( [Date] => 2017-01-02 [Name] => Asia consumer MC Alpha bmk - risk [Level] => .333000 ) [1] => Array ( [Date] => 2017-01-03 [Name] => Asia consumer MC Alpha bmk - risk [Level] => .333000 ))

mi código para convertir la matriz a archivo es

 public function DownloadFile($filetype,$Array_data,$content){ $LevelArray = array('Date','Name','Level'); $selected_array = $LevelArray; if($filetype =='csv'){ $Filename ='Level.csv'; header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename='.$Filename.''); // create a file pointer connected to the output stream $output = fopen('php://output', 'w'); fputcsv($output, $selected_array); foreach ($Array_data as $row){ fputcsv($output, $row); } fclose($output); }}

en respuesta, obtengo el resultado de la respuesta, pero esa respuesta no se convierte en un tipo de archivo, aunque los encabezados apuntan para obtener el archivo csv.

 Date,Name,Level 2017-01-02,"Asia consumer MC Alpha bmk - risk",.333000 2017-01-03,"Asia consumer MC Alpha bmk - risk",.333000 2017-01-04,"Asia consumer MC Alpha bmk - risk",.333000

He intentado usar el método de respuesta de laravel y muchos otros tampoco parecen funcionar, sus encabezados no pueden obligar al navegador a descargar el archivo.

Los encabezados de RESPUESTA se ven así

 Connection:close Content-Disposition:attachment; filename=Benchmark-Level.csv Content-type:text/csv;charset=UTF-8 Host:localhost:8000 X-Powered-By:PHP/5.6.25
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¿Tu segundo encabezado está dividido en 2 líneas? Tiene que ser una sola línea sin saltos de línea.

 header('Content-Disposition: attachment; filename='.$Filename.'');

Editar

Este código está funcionando para mí. Agregué un encabezado más para forzar la descarga, pero funciona en Chrome con o sin ese encabezado adicional. ¿Podría intentar ejecutarlo en diferentes navegadores? Mi opinión es que hay configuraciones del navegador que impiden la descarga.

 <?php $selected_array = array('header:col1','header:col2', 'header:col3'); $Array_data = array( array('row1:col1','row1:col2', 'row1:col3'), array('row2:col1','row2:col2', 'row2:col3'), array('row3:col1','row3:col2', 'row3:col3'), ); $Filename ='Level.csv'; header('Content-Type: text/csv; charset=utf-8'); Header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename='.$Filename.''); // create a file pointer connected to the output stream $output = fopen('php://output', 'w'); fputcsv($output, $selected_array); foreach ($Array_data as $row){ fputcsv($output, $row); } fclose($output); ?>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!