0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[PHP] readfileでcsvをダウンロードさせると余計なものが挿入される

Last updated at Posted at 2022-10-19

何が起きたのか

webアプリケーション上でcsvダウンロードボタンを作った。
動いたものの、元のcsvファイルデータと一緒にPHPの警告や、htmlソースがデータ化されてしまっている。。。

column_1 column_2
1 tree
2 mono
3 ace
4 believe
<!DOCTYPE html>
<html lang="ja">

解決した

$filename = 'child.csv';
$csvFilename = '/tmp/child.csv';

header('Content-Type: application/stream');
header('Content-Disposition: attachment; filename='.$fileName); 
header('Content-Length: '.filesize($csvFileName)); 
readfile($csvFileName);
- unlink($fileName);
+ unlink($csvFileName);

unlinkの設定を修正したら、正常になりましたとさ。おしまい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?