コンボボックスによるファイルダウンロード( 要 JavaScript と PHP )

  HTML 側





  
<SCRIPT language="javascript" type="text/javascript">
function DownloadFile(id) {

	top.location = 
	"http://lightbox.on.coocan.jp/html/button_download.php?download_target="
	 + document.getElementById(id).value;

}
</SCRIPT>

<SELECT id="d1"> 
<OPTION value="WinOfSql102.lzh">SQLの窓 Build C++</OPTION>
<OPTION value="winofsql.png">SQLの窓ロゴ</OPTION>
</SELECT> 
<INPUT type=button value="ダウンロード" onClick='DownloadFile("d1");'> 
  

IE6 の場合、location は top を使用しないと、フレーム内の処理で2回目にエラーとなります

要するに自分のウインドウ使用すると、ページがぶっ壊れるという
仕様なのかバグなのか良く解らない、Microsoft らしい現象ですが。

IE7 ではなおってました。




  PHP 側 ( button_download.php )



  
<?
header( "Content-Type: application/octet-stream" );
header( "Content-disposition: attachment; filename={$_GET['download_target']}" );

$path = '../download/' . $_GET['download_target'];

$size = filesize( $path );

header( "Content-Length: $size" );

$fp = fopen( $path, 'rb' );

if ( $fp ) {

	while( TRUE ) {
		if ( feof( $fp ) ) {
			break;
		}
		$ret = fread( $fp, 1024 );
		print $ret;
	}

	fclose( $fp );
}

?>
  



  fget.php

他のサーバーからもダウンロードできるようにしました。
コマンドラインでも使用できます。


  
<?php
// *******************************************************************
// 【fget.php】
// http:// で他のサーバのファイルを読み込んでダウンロードします
// 【利用方法】
// WEB : fget.php?target=URLエンコードされたURL
// CMD : php.exe fget.php URL
// *******************************************************************

// *******************************************************************
// Windows 用の処理
// *******************************************************************
if ( strtoupper( substr( php_uname("s"), 0, 7 )) == 'WINDOWS' ) {
	if ( !extension_loaded( "curl" ) ) { 
		dl("php_curl.dll"); 
	}
}

// *******************************************************************
// コマンドラインでも使えるように
// *******************************************************************
if (substr(php_sapi_name(), 0, 3) == 'cgi') {
	$remoteFile = $_GET['target'];
}
else {
	$remoteFile = $argv[1];
}
$fileName = explode("/",$remoteFile);
$fileCount = count($fileName);

// *******************************************************************
// PHP オンラインマニュアルの投稿データより
// *******************************************************************
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
	echo 'cURL failed';
	exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
	$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
	$contentLength = (int)$matches[1];
}

// *******************************************************************
// WEB 経由ダウンロード
// *******************************************************************
if (substr(php_sapi_name(), 0, 3) == 'cgi') {
	header( "Content-Type: application/octet-stream" );
	header( "Content-disposition: attachment; filename={$fileName[$fileCount-1]}" );

	$path = $remoteFile;

	header( "Content-Length: $contentLength" );

	$fp = fopen( $path, 'rb' );

	if ( $fp ) {

		while( TRUE ) {
			if ( feof( $fp ) ) {
				break;
			}
			$ret = fread( $fp, 1024 );
			print $ret;
		}

		fclose( $fp );
	}
}
// *******************************************************************
// コマンドライン
// *******************************************************************
else {
	$ihandle = fopen($remoteFile, "r");
	$ohandle = fopen($fileName[$fileCount-1], "w");
	if ( $ihandle ) {

		while( TRUE ) {
			if ( feof( $ihandle ) ) {
				break;
			}
			$ret = fread( $ihandle, 1024 );
			fwrite( $ohandle,  $ret );
		}

		fclose( $ohandle );
		fclose( $ihandle );
	}
}

?>
  



  fget.php を簡単に使える JavaScript ライブラリ

下側の createDownloadComboJsonly は、png や jpg や swf はダウンロードできません。
表示してしまいます。しかし、PHP を使わずに通常ファイルならばダウンロード可能です。

  
function createDownloadCombo(){
	var i,url,name,str;

str="";
str+="<div> \n";
str+="<select>  \n";

	for( i = 0; i < (window.createDownload.length)/2; i++ ) {
		url = window.createDownload[i*2];
		name = window.createDownload[i*2+1];
str+="<option value=\""+url+"\">"+name+"</option> \n";

	}

str+="</select>  \n";
str+="<input type=button value=\"ダウンロード\" onClick=' \n";
str+="	var a = this.parentNode; \n";
str+="	a = encodeURIComponent(a.getElementsByTagName(\"SELECT\")[0].value); \n";
str+="	top.location = \"http://lightbox.on.coocan.jp/fget.php?target=\"+a; \n";
str+="	' \n";
str+="> \n";
str+="</div> \n";

	document.write(str);

}

function createDownloadComboJsonly(){
	var i,url,name,str;

str="";
str+="<div> \n";
str+="<select>  \n";

	for( i = 0; i < (window.createDownload.length)/2; i++ ) {
		url = window.createDownload[i*2];
		name = window.createDownload[i*2+1];
str+="<option value=\""+url+"\">"+name+"</option> \n";

	}

str+="</select>  \n";
str+="<input type=button value=\"ダウンロード\" onClick=' \n";
str+="	var a = this.parentNode; \n";
str+="	a = a.getElementsByTagName(\"SELECT\")[0].value; \n";
str+="	top.location = a; \n";
str+="	' \n";
str+="> \n";
str+="</div> \n";

	document.write(str);

}
  


使い方

  
<script
	type="text/javascript" charset="shift_jis"
	src="http://lightbox.on.coocan.jp/createDownloadCombo.js">
</script>

<script type="text/javascript">
window.createDownload = [
"http://lightbox.on.coocan.jp/download/WinOfSql102.lzh","SQLの窓 Build C++"
,
"http://winofsql.jp/download/hanbaib.lzh","簡易販売管理用DBデータ"
,
"http://lightbox.cocolog-nifty.com/photos/image_another/roadtrip_11_bg_021404.jpeg","画像もダウンロード"
]
createDownloadCombo();
</script>
  












  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ