基礎から解る!PHP入門講座 プログラム入門に最適な言語、PHPを学ぼう

基礎から解る!PHP入門講座

プログラム入門に最適な言語、PHPを学ぼう      

    アップロード 9−4

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang="ja"><head>
    <meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <link rel="stylesheet" href="../style.css">
    <title>ファイルアップローダー4</title></head>
    <body><div class="file2">
    <form method="post" enctype="multipart/form-data" action="09_04_fileup.php">
    <input type="file" name="photo" size="30">
    <input type="submit" value="表示!"></form>

    <?php
    error_reporting(1);

    $updir = "./upload3/";      /*ファイルを保存するフォルダ*/
    $filename = $_FILES['photo']['name'];
    if(file_exists($_FILES['photo']['tmp_name'])){    /*ファイルの存在を確認 tempの状態で確認*/
      $filesize = filesize($_FILES['photo']['tmp_name']);    /*ファイルサイズを取得 tempの状態で取得*/
      list($width,$height)=getimagesize($_FILES['photo']['tmp_name']);  /*ファイルのサイズを変数に入れます。*/

      if($filesize <= 100000){
        if(ereg("(^[a-zA-Z0-9_-]+)¥.(jpg)|(^[a-zA-Z0-9_-]+)¥.(JPG)",$filename)){    /*jpgかgifしかアップできない*/
          $hiritu = $width/$height;    /*タテ・ヨコの比率を計算します*/
          if($hiritu > 1.2 and $hiritu < 1.6){    /*1.4前後の比率ならOKです。*/
            move_uploaded_file($_FILES['photo']['tmp_name'], $updir.$filename);  /*uploadフォルダに保存*/
              $stamp = 2145798000-time();  /*2037年12月31日のタイムスタンプから今のスタンプを引きます。*/
              $new_name = $stamp.".jpg";    /*ファイル名につける数字*/
                rename("$updir$filename", "$updir$new_name");      /*ファイルの名前を変更*/
                $new_width = 100;    /*作成するサムネイルのヨコ幅です*/
                $new_height = ($new_width / $width)*$height;  /*タテは計算でだします*/
                $src=imagecreatefromjpeg("$updir$new_name");  /*画像からイメージを作成*/
                $dst=imagecreatetruecolor($new_width,$new_height);  /*サムネイルのサイズで画像の準備*/
                imagecopyresampled($dst,$src,0,0,0,0,$new_width,$new_height,$width,$height);/*縮小して描きこみ*/
                imagejpeg($dst,"$updir$new_name");  //先に保存した大きい画像に上書します
          }else{
            echo "タテ:ヨコ 3:4程度の横長画像だけです。<br>";
          }
         $s_mess = "ファイル名 " . $filename . " アップロード成功!<br><br>";
        } else {
          $s_mess = "jpg ファイルしかアップできません。或いはハイフン以外の記号があります。";
        }
      } else {
        $s_mess = "$filesize Byte ファイルサイズが大きすぎます。<br>100KB 以下にしてください。";
      }
    } else {
    $s_mess = "jpg ファイルで 100KB 以内、ファイル名は英数と_-以外は使えません。画像は自動的にヨコ100pxに縮小されます。<br><br>";
    }
    echo "ファイルをアップした順にテーブルに並べていきます。50枚でお終い。<br>";
    echo "jpg ファイルで 100kb 以内、ファイル名は英数と_-以外は使えません。横長の画像だけです。<br>";
    /*-------------------------------------------------------------------------------*/
    if ($handle = opendir($updir)) {
      $cnt=0;
      while (false !== ($file = readdir($handle))) {
        if(ereg("(^[a-zA-Z0-9_-]+)¥.(jpg)|(^[a-zA-Z0-9_-]+)¥.(JPG)",$file)){      /*画像ファイルのみを読み込む*/
            $filenames[$cnt] = $file;
        }
      $cnt++;
      }
    closedir($handle);
    }
    /*-----------------------------------------ファイルをソートすることでキーを付け直す--------*/
    sort ($filenames);
    /*---------------------------------------------------50枚目の画像を削除します-------------*/
    if($filenames[50] != null){
      unlink("$updir$filenames[50]");
      array_pop($filenames);      /*配列の最後、つまり指定番号のファイル名も削除*/
    }
    /*---------------------------------------------------ここから表示-------------------------*/

      echo "<br><br><table border='0'>";
      $cnt = 0;
      while (isset($filenames[$cnt])) {
        if ($file != "." and $file != "..") {
          if($cnt % 5 == 4){
            echo "<td><img src='$updir/$filenames[$cnt]'></td></tr>";
          } else if($cnt % 5 == 0){  
            echo "<tr><td><img src='$updir/$filenames[$cnt]'></td>";
          } else {
            echo "<td><img src='$updir/$filenames[$cnt]'></td>";
          }
        $cnt++;
        }
      }
    echo "</table>";
    ?>

    </div></body></html>
    2023/9
    SUN MON TUE WED THU FRI SAT
              1 2
    3 4 5 6 7 8 9
    10 11 12 13 14 15 16
    17 18 19 20 21 22 23
    24 25 26 27 28 29 30


     フォクすけ©2006 Mozilla Japan



    制作:基礎から解るPHP入門講座 代表:FREEZE

    Copyright © 2005/2/28 flzPHP.All Rights Riserved