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 + 通信の最新記事】
- fake sendmail for windows を使って、PHP でごく普通に( mb_send_mail で )メール送信
- PHP の cURL でファイルアップロード
- PHP : WEB でもコマンドラインでも HTTP でファイルをダウンロードする 『fget.php』
- PHP : cURL : Google Photo API の albums.list メソッドを使用して アルバム一覧データを取得する
- PHP + cURL で Google Photo API の アクセストークンを取得する
- PHP : cURL で FTP + SSL ダウンロード
- PHP の imap 関数で( Windows ローカル )、Yahoo メールのメールをさくらインターネットのメールへコピーする
- PHP の imap 関数( Windows のローカル )で、さくらインターネットのメールボックスの情報を取得する
- PHP : IMAP 関数 でまずメールのヘッダ部分を整理してログに出力する / ※ JSON 出力を追加
- ファイルを一つアップロード : FORM の target を IFRAME にして、PHP に JavaScript を書き出させて元のページにメッセージを表示させる
- Pear の Net_POP3 で、SSL(995) を使って簡単にメール受信
- PHP をコマンドプロンプトから実行して、WEB 上のデータを取得するいくつかの方法
- FormData を使用して $.ajax で送信すると、ファイルのアップロードとメール送信(ファイル添付)を一度に行えます
- ファイルのアップロードからそれを添付してメールを送るテンプレート(スケルトン)
- PHP : mb_send_mail を使用した自家用メール送信 API
- PHP で Windows標準のオブジェクト(CDO.Message)とGMail を使ってメール送信
- 連想配列のキー部分がプロパティと一致したらセットする( メール送信用クラス )
- PHP : リモートのファイルサイズを取得する
- ローカルの PHP を使ってコマンドプロンプトより簡単にバイナリファイルをアップロードする
- PHP の cURL で ftp サーバーにファイルをアップロードする(SSLなし)







