WEB/Server

PHP ๋‚ด์šฉ์ •๋ฆฌ - 3

kite707 2021. 1. 14.
<!doctype html>
<html>
    <body>
        <h1><a href="index.php">WEB</a></h1>
        <ol>
            <li><a href="index.php?id=HTML">HTML</a></li>
            <li><a href="index.php?id=CSS">CSS</a></li>
            <li><a href="index.php?id=JavaScript">JavaScript</a></li>
        </ol>
        <h2>
            <?php
            if(isset($_GET['id'])){
                echo $_GET['id'];
            }
            else{
                echo "Welcome!";
            }
            ?>
        </h2>
        <?php
        if(isset($_GET['id'])){
            echo file_get_contents("data/".$_GET['id']);
        }
        else{
            echo "Hello PHP";
        }
        
        ?>
    </body>
</html>

์œ„ ์ฝ”๋“œ๋Š” ์ €๋ฒˆ ์‹œ๊ฐ„์— ๋งŒ๋“ค์–ด ๋†“์€ ์ฝ”๋“œ์ด๋‹ค. ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์œ„ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ๊ฒƒ์ด๋‹ค.

scandirํ•จ์ˆ˜

์‚ฌ์šฉ๋ฐฉ๋ฒ•

scandir ( "ํด๋” ์œ„์น˜"  )

๋ฆฌํ„ดํ˜•์€ array์ด๋‹ค. 

์‚ฌ์šฉ ์˜ˆ์‹œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!doctype html>
<html>
    <body>
        <h1><a href="index.php">WEB</a></h1>
        <ol>
            <?php
                $str=scandir("./data");
                var_dump($str);
                }
            ?>
    

        </ol>
        <h2>
            <?php
            if(isset($_GET['id'])){
                echo $_GET['id'];
            }
            else{
                echo "Welcome!";
            }
            ?>
        </h2>
        <?php
        if(isset($_GET['id'])){
            echo file_get_contents("data/".$_GET['id']);
        }
        else{
            echo "Hello PHP";
        }
        
        ?>
    </body>
</html>

 

<!doctype html>
<html>
    <body>
        <h1><a href="index.php">WEB</a></h1>
        <ol>
            <?php
                $str=scandir("./data");
                $i=0;
                while($i<6){
                    echo"<li>$str[$i]</li>\n";
                    $i=$i+1;
                }
            ?>
        </ol>
        <h2>
            <?php
            if(isset($_GET['id'])){
                echo $_GET['id'];
            }
            else{
                echo "Welcome!";
            }
            ?>
        </h2>
        <?php
        if(isset($_GET['id'])){
            echo file_get_contents("data/".$_GET['id']);
        }
        else{
            echo "Hello PHP";
        }
        
        ?>
    </body>
</html>

 

์—ฌ๊ธฐ์„œ 1๋ฒˆ๊ณผ 2๋ฒˆ์˜ ..์„ ์—†์• ๊ธฐ ์œ„ํ•ด ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. 

<!doctype html>
<html>
    <body>
        <h1><a href="index.php">WEB</a></h1>
        <ol>
            <?php
                $str=scandir("./data");
                $i=0;
                while($i<count($str)){
                    if($str[$i]!='.'){
                        if($str[$i]!='..'){
                            echo"<li><a href=\"index.php?id=$str[$i]\">$str[$i]</a></li>\n";
                        }
                    }
                   $i++;
                }
            ?>
        </ol>
        <h2>
            <?php
            if(isset($_GET['id'])){
                echo $_GET['id'];
            }
            else{
                echo "Welcome!";
            }
            ?>
        </h2>
        <?php
        if(isset($_GET['id'])){
            echo file_get_contents("data/".$_GET['id']);
        }
        else{
            echo "Hello PHP";
        }
        
        ?>
    </body>
</html>

ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ

ํ•จ์ˆ˜์˜ ์„ ์–ธ๊ณผ ์ •์˜๋Š” ๋น„์Šทํ•˜๋‹ˆ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ์œ ์‚ฌํ•˜๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์œ„ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<?php
function print_title(){
  if(isset($_GET['id'])){
    echo $_GET['id'];
  } else {
    echo "Welcome";
  }
}
function print_description(){
  if(isset($_GET['id'])){
    echo file_get_contents("data/".$_GET['id']);
  } else {
    echo "Hello, PHP";
  }
}
function print_list(){
  $list = scandir('./data');
  $i = 0;
  while($i < count($list)){
    if($list[$i] != '.') {
      if($list[$i] != '..') {
        echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
      }
    }
    $i = $i + 1;
  }
}
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>
      <?php
      print_title();
      ?>
    </title>
  </head>
  <body>
    <h1><a href="index.php">WEB</a></h1>
    <ol>
      <?php
      print_list();
      ?>
    </ol>
    <h2>
      <?php
      print_title();
      ?>
    </h2>
    <?php
    print_description();
     ?>
  </body>
</html>

 

๋Œ“๊ธ€