SQLの窓

2015年06月22日


PHP : http 経由でファイルのサイズを取得する file_size 関数と ファイルサイズを付加した get_imagesize 関数

getimagesize は、ファイルのサイズが取得されません。あまり大きいサイズは事前に排除したいので、ファイルサイズを付加すれば便利だろうと思い、まず file_size 関数を作成しました。



※ getimagesize( get_imagesize ) は、画像以外だとエラーになります。
<?php
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );

if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {

	if ( $_POST["send"] == "テスト送信" ) {
		$result = @getimagesize( $_POST["url"] );
		if ( $result !== false ) {
			$view = print_r( $result, true );

			$header = get_headers( $_POST["url"] );
			$view .= print_r( $header, true );

			$size = implode( "\n", $header );
			$view .= $size;

			if (preg_match('/Content-Length: (\d+)/', $size, $matches)) {
				$contentLength = (int)$matches[1];
				$view .= "\n\niamge size : {$contentLength}";
			}

		}
		else {
			$view = "{$_POST["url"]} を読み込めません";
		}
	}

	if ( $_POST["send"] == "get_imagesize" ) {
		$result = get_imagesize( $_POST["url"] );
		if ( $result !== false ) {
			$view = print_r( $result, true );
		}
		else {
			$view = "{$_POST["url"]} を読み込めません";
		}
	}

	if ( $_POST["send"] == "fle_size" ) {
		$result = fle_size( $_POST["url"] );
		if ( $result !== false ) {
			$view = "filesize : $result";
		}
		else {
			$view = "{$_POST["url"]} を読み込めません";
		}
	}
}

function get_imagesize($url) {

	$result = @getimagesize($url);
	if ( $result !== false ) {
		$view = print_r( $result, true );

		$size = fle_size($url);
		if ( $size !== false ) {
			$result["size"] = $size;
		}
		return $result;
	}
	else {
		return false;
	}

}

function fle_size($url) {

	$header = @get_headers($url) ;
	if ( $header !== false ) {
		$size = implode( "\n", $header );
		if (preg_match('/Content-Length: (\d+)/', $size, $matches)) {
			$contentLength = (int)$matches[1];
			return $contentLength;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}

}

?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
* {
	font-size: 16px;
	font-family: "Hiragino Kaku Gothic ProN", "メイリオ", Meiryo, "MS Pゴシック", sans-serif;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function(){

});
</script>
</head>
<body>

<form method="post" target="_self">
	URL => <input type="text" name="url" style="width:800px;" value="<?= $_POST["url"] ?>">
	<br>

	<input type="submit" name="send" value="テスト送信">
	<input type="submit" name="send" value="get_imagesize">
	<input type="submit" name="send" value="fle_size">
</form>
<pre>
<?= $view ?>
</pre>
</body>
</html>

※ ボタン画像



【PHP + 通信の最新記事】
posted by lightbox at 2015-06-22 14:26 | PHP + 通信 | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり