Чтобы создать и загрузить текстовый файл на PHP, вы можете использовать различные методы. Вот несколько примеров с фрагментами кода:
Метод 1: использование file_put_contents() и header()
<?php
$content = "This is the content of the text file.";
$file = 'example.txt';
// Save the content to the file
file_put_contents($file, $content);
// Set the appropriate headers for download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
// Read the file and output it to the browser
readfile($file);
Метод 2: использование fopen(), fwrite() и fclose()
<?php
$content = "This is the content of the text file.";
$file = 'example.txt';
// Open the file for writing
$handle = fopen($file, 'w');
// Write the content to the file
fwrite($handle, $content);
// Close the file
fclose($handle);
// Set the appropriate headers for download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
// Read the file and output it to the browser
readfile($file);
Метод 3: использование file_get_contents() и file_put_contents()
<?php
$content = "This is the content of the text file.";
$file = 'example.txt';
// Save the content to the file
file_put_contents($file, $content);
// Set the appropriate headers for download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
// Read the file and output it to the browser
readfile($file);
Это всего лишь несколько примеров того, как можно создать и загрузить текстовый файл на PHP. Каждый метод использует разные функции для достижения одного и того же результата. Выберите тот, который лучше всего соответствует вашим требованиям и стилю кодирования.