このサイトはPHPを独習する際に作成したスクリプトを基に、プログラム言語初心者のための入門講座として作成しました。PHPは初心者に親しみやすく、解りやすいプログラム言語です。これからプログラムを学ぶ人の参考になればと思います。
美しい画像掲示板
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja"><head>
<meta http-equiv="Content-type" content="text/html; charset=Shift-JIS">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="../style.css">
<title>美しい画像掲示板</title></head>
<body><div class="file2">
<form method="post" enctype="multipart/form-data" action="pictbbs.php">
<input type="file" name="data" size="30">
<input type="submit" name="enter" value="GO!"></form>
<?php
error_reporting(1);
$enter = $_POST['enter'];
$updir = "./img_folder/";
/*--------------------------------------------ファイルのアップロード*/
$filename = $_FILES['data']['name'];
if(file_exists($_FILES['data']['tmp_name'])){ /*ファイルの存在を確認 tempの状態で確認*/
$filesize = filesize($_FILES['data']['tmp_name']); /*ファイルサイズを取得 tempの状態で取得*/
if($filesize <= 100000){
if(ereg("(^[a-zA-Z0-9_-]+)¥.(jpg)|(^[a-zA-Z0-9_-]+)¥.(gif)",$filename)){ /*jpgかgifしかアップできない*/
move_uploaded_file($_FILES['data']['tmp_name'], $updir.$filename); /*ファイルをuploadフォルダに保存*/
$stamp = 2145798000-time(); /*2037年12月31日のタイムスタンプ*/
$new_name = $stamp.$filename; /*ファイル名につける数字*/
rename("$updir$filename", "$updir$new_name"); /*ファイルの名前を変更*/
echo "ファイル名 " . $filename . " アップロード成功!";
} else {
echo "jpg かgif のファイルしかアップできません。或いはハイフン以外の記号があります。";
}
} else {
echo "$filesize Byte ファイルサイズが大きすぎます。<br>100000 Byte 以下にしてください。";
}
} else {
echo "jpg か gif ファイルで 100kb 以内、ファイル名は英数と_-以外は使えません。53枚を越すと削除します。<br><br>";
}
/*--------------------------------------------ファイル名を配列に読み込む*/
if ($handle = opendir($updir)) {
$cnt=0;
while (false !== ($file = readdir($handle))) {
if(ereg("(^[a-zA-Z0-9_-]+)¥.(jpg)|(^[a-zA-Z0-9_-]+)¥.(gif)",$file)){ /*画像ファイルのみを読み込む*/
$filenames[$cnt] = $file;
}
$cnt++;
}
closedir($handle);
}
/*--------------------------------------------ファイルをソートすることでキーを付け直す*/
sort ($filenames);
/*--------------------------------------------一定の枚数で削除*/
$delete = 53; /*画像最大枚数(変更可能)*/
/*--------------------------------------------ファイルの削除*/
if($filenames[$delete] != null){
unlink("$updir$filenames[$delete]");
array_pop($filenames); /*配列の最後、つまり指定番号のファイル名も削除*/
}
/*--------------------------------------------画像の数を得る*/
$count = count($filenames);
/*--------------------------------------------ページ情報を得る*/
$page_num = $_GET['page_num'];
if($page_num == null or $enter == "GO!"){
$page_num = 1;
}
/*--------------------------------------------ここから表示のための計算*/
$col = 4; /*画像をヨコ何行表示させるか(変更可能)*/
$times = 0; /*ヨコ何行かをカウントする*/
$pgplus = $col*5*($page_num-1); /*2ページ目以降は20枚づつずれるのでその分の補正*/
/*--------------------------------------------指定行数だけテーブルを並べる*/
while($times < $col){
$f_cnt = 0+$times*5+$pgplus; /*ヨコの行数とページによってファイルの番号を調整する*/
/*--------------------------------------------配列数(画像の枚数)を超えたら終了*/
if($f_cnt >= $count){
break;
}
/*--------------------------------------------ファイルのデータを得る 2次元配列*/
while($f_cnt < (5+$times*5+$pgplus)){
if($filenames[$f_cnt] != null){
$f_data[$f_cnt] = getimagesize("$updir$filenames[$f_cnt]");
} else {
$f_data[$f_cnt] = array(100,100,); /*画像ファイルがない場合の処理*/
}
$f_cnt++;
}
/*--------------------------------------------ファイルのタテとヨコのデータを変数に代入*/
/*--------------------------------------------これは後の計算式を単純にするために代入している*/
$a_w = $f_data[0+$times*5+$pgplus][0];
$a_h = $f_data[0+$times*5+$pgplus][1];
$b_w = $f_data[1+$times*5+$pgplus][0];
$b_h = $f_data[1+$times*5+$pgplus][1];
$c_w = $f_data[2+$times*5+$pgplus][0];
$c_h = $f_data[2+$times*5+$pgplus][1];
$d_w = $f_data[3+$times*5+$pgplus][0];
$d_h = $f_data[3+$times*5+$pgplus][1];
$e_w = $f_data[4+$times*5+$pgplus][0];
$e_h = $f_data[4+$times*5+$pgplus][1];
$size_w = 700; /*テーブルの幅(変更可能)*/
/*--------------------------------------------画像の高さをそろえるための計算*/
$a_num = $size_w/($a_w+$b_w*$a_h/$b_h+$c_w*$a_h/$c_h+$d_w*$a_h/$d_h+$e_w*$a_h/$e_h);
$b_num = $a_h*$a_num/$b_h;
$c_num = $a_h*$a_num/$c_h;
$d_num = $a_h*$a_num/$d_h;
$e_num = $a_h*$a_num/$e_h;
/*--------------------------------------------表示用のヨコ数値を得る*/
$width[0] = round($a_w*$a_num,0);
$width[1] = round($b_w*$b_num,0);
$width[2] = round($c_w*$c_num,0);
$width[3] = round($d_w*$d_num,0);
$width[4] = round($e_w*$e_num,0);
/*--------------------------------------------テーブルと画像の表示*/
$tb_cnt = 0+$times*5+$pgplus;
$w_cnt = 0;
echo "<table border='0'><tr>";
while($tb_cnt < (5+$times*5+$pgplus)){
echo "<td valign='top' style='font-size:12px'>";
echo "<a href='$updir$filenames[$tb_cnt]' target='_blank'>";
if($tb_cnt < $count){
echo "<img src='$updir$filenames[$tb_cnt]' border='0' width='$width[$w_cnt]'></a>";
echo "<br>".$f_data[$tb_cnt][0]."*".$f_data[$tb_cnt][1]."</td>";
} else { /*画像がない場合*/
echo "<img src='image.jpg' border='0' width='$width[$w_cnt]'></a></td>";
}
$tb_cnt++;
$w_cnt++;
}
echo "</tr></table>";
$times++;
}
/*--------------------------------------------2ページ目以降へのリンク*/
echo "<br>";
$page = (int)(($count-1)/($col*5)+1); /*総画像数を1ページの画像数で割りページ数を出す*/
$page_cnt = 1;
while($page_cnt <= $page){
if($page_cnt == $page_num){
echo "<a href='pictbbs.php?page_num=$page_cnt'><b> No.".$page_cnt." ></b></a>";
} else {
echo "<a href='pictbbs.php?page_num=$page_cnt'> No.".$page_cnt." ></a>";
}
$page_cnt++;
}
?></div></body></html>