LoginSignup
10
9

More than 5 years have passed since last update.

PHPの複数ファイルダウンロード

Posted at
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="SJIS" />
<title>HTML 5 complete</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

</head>
<body>
<form name="download1" target="target1" action="/test/download1.php"></form>
<form name="download2" target="target2" action="/test/download2.php"></form>
<input type="button" value="ダウンロード" onclick="download1.submit();download2.submit();">

<iframe name="target1" width="0" height="0"></iframe>
<iframe name="target2" width="0" height="0"></iframe>
</body>
</html>
download1.php
<?php

 // 作成するファイル名の指定
  $file_name = 'file1.txt';

  // ファイルの存在確認
  if( !file_exists($file_name) ){
    // ファイル作成
    touch( $file_name );
  }else{
    // すでにファイルが存在する為エラーとする
    echo('Warning - ファイルが存在しています。 file name:['.$file_name.']');
    exit();
  }

  // ファイルのパーティションの変更
  chmod( $file_name, 0666 );


// ダウンロードさせるファイル名
$tmp_file1 = $file_name;
// ヘッダ
header("Content-Type: application/octet-stream");
// ダイアログボックスに表示するファイル名
header("Content-Disposition: attachment; filename=$tmp_file1");
readfile($tmp_file1);

//ファイル削除
unlink("D:\\xampp\\htdocs\\test\\".$file_name);
exit;
?>

download2.php
<?php

 // 作成するファイル名の指定
  $file_name = 'file2.txt';

  // ファイルの存在確認
  if( !file_exists($file_name) ){
    // ファイル作成
    touch( $file_name );
  }else{
    // すでにファイルが存在する為エラーとする
    echo('Warning - ファイルが存在しています。 file name:['.$file_name.']');
    exit();
  }

  // ファイルのパーティションの変更
  chmod( $file_name, 0666 );


// ダウンロードさせるファイル名
$tmp_file = $file_name;
// ヘッダ
header("Content-Type: application/octet-stream");

// ダイアログボックスに表示するファイル名
header("Content-Disposition: attachment; filename=$tmp_file");
// 対象ファイルを出力する。
readfile($tmp_file);

//ファイル削除
unlink("D:\\xampp\\htdocs\\test\\".$file_name);
exit;
?>

10
9
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
10
9