|
<?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 );
}
}
?>
| |