Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i Create and download CSV in PHP / ~#root -i
  • RU
  • UA
  • EN
  • Create an online store
  • Documentation
  • Blog
    • Unix ОS
    • Php
    • MySQL
    • JavaScript
    • Package Managers
    • Docker
    • Seo
  • Auxiliary services
    • Short Links
    • Exchange views YouTube
  • Sign in
  • Create Account
  • Home
  • Php
  • Create and download CSV in PHP

Create and download CSV in PHP

An example of creating a CSV with a line separator ";"

Разделитель CSV

The code:

// 1
$fp = fopen('./fileName.csv', 'w');
if ($fp === false) {
// 2
exit('Can not create file (No Rights) ./fileName.csv');
}

// 3
$models = [
[
'name' => 'Visa',
'type' => 'Card',
'balance' => '10.0'
],
[
'name' => 'Master',
'type' => 'Card',
'balance' => '0'
]
];

// 4
fwrite($fp, '"Имя";"Тип";"Баланс"' . "\n");
foreach ($models as $v) {
fwrite($fp, '"' . $v['name'] . '";"'
. $v['type'] . '";"'
. $v['balance'] . '"'
. "\n");
}

// 5
fseek($fp, 0);
fclose($fp);

// 6
$data = file_get_contents('./fileName.csv');

// 7
header('Content-Type: text/csv'); // 7.1
header('Content-Disposition: attachment; filename="fileName.csv"'); // 7.2
header("Content-Transfer-Encoding: binary"); // 7.3
header('Expires: 0'); // 7.4
header('Pragma: no-cache'); // 7.5
header("Content-Length: ".strlen($data)); // 7.6

// 8
exit($data);

Note :

For the header to fire, there must be no BOM and no output of information before the code fires.

1 - Create a file fileName.csv in the root of the site for writing.

2 - We warn about an error if there are no rights to write.

3 - An array of values that we will process

4 - Writing to a file. '"Name ";" Type";"Balance"' . "\n" (";" - line separator, "\n" - line break).

5 - Return the pointer to the beginning of the file and close the file (save).

6 - Read the file again.

7 - We give headers for the ability to download the file.

7.1 - Parameter defining file type.

7.2 - Pointer to file attachment.

7.3 - In what form to transfer (in this case, in binary).

7.4, 7.5 - Pointer not to cache the file and constantly give the current one.

7.6 - The size of the content part of the file.

8 - We display information that, in conjunction with the headers, will make it possible to download it to a CSV file.

Result:

Результат CSV

25 April 22
118
0

Comments

Name
E-mail
Rating
Review

Other articles from the category

19 June 2019

ImageCMS Templates 4.10-4.12.1

Latest templates from ImageCMS 4.12, authorization is required to download.

26 January 2019

Long-term storage of the basket in the online store

Consider options for popular options for storing goods in a shopping cart in an online store. Let's outline the pros and cons of such storage. Consider options for long-term storage of the basket.

17 January 2019

License key for ImageCMS 4.9-4.12.1 Pro and Pre

Creating a key for imageCMS 4.9-4.12.1 Pro and Pre. You must be logged in to receive it.

07 December 2018

$_SERVER[DOCUMENT_ROOT] in CLI or Cron

When running the script from the console, there is no DOCUMENT_ROOT in $_SERVER. Let's try to get around this in our own ways, catch the file directory.

31 October 2018

Analysis of loaded parts of Php code with xhprof

After developing any tool in PHP, the question becomes how resourceful the created code is and what elements it still affects. Let's take a look at xhprof installation, configuration, and how to work with xhprof.

18 July 2018

Grouping conditions in an ORM Propel query (condition, combine)

Let's analyze the possibility of combining conditions in a query by groups in Propel. Consider an example of filtering by fields using ->condition() and ->combine().

14 June 2018

Authorization 1C on the site PHP_AUTH_USER on CentOS apache

Faced the problem of authorization 1s on the site. For some reason, the server did not accept the PHP_AUTH_USER and PHP_AUTH_PW parameters from it. Let's take a closer look at the solution and bypassing these parameters in $_SERVER .

21 May 2018

Joining an undeclared table in Propel or Join Custom Table Propel

The purpose of the article was to join (join) a table not declared in the schema (schema.xml) in propel2. Apparently a rare case or simply not enough documentation for this Propel ORM.

Categories

  • Unix OS
  • Php
  • MySQL
  • JavaScript
  • Package Managers
  • Docker
  • Seo

Latest comments

Добрый день, Сергей. Я на более новых версиях блют...
root-i
23.02.23
Пробовал на transmart колонке. Ничего из перечисле...
Сергей
20.02.23
HenryMit, может быть
root-i
07.02.23
Неофрейдизм — это… Определение, принципы, представ...
HenryMit
07.02.23

I share information in which I needed help and spent a lot of time figuring it out. If the information helped at least one person, then this site was not created in vain.

Thank you for the continuation of the site:
Contacts

Telegram Viber Mail

Search for articles

  • Sign in
  • Create Account

Powered by chmod -R