[PHP]진짜 간단-허접- 앨범 만들기

송지석의 이미지

과연 이게 필요하실 분이 있을런지 의문입니다만

php를 사용한 앨범 만들기입니다.

디카로 사진을 찍어도 게시판에 하나씩 업로드하는 게 너무 귀찮아서요. -_- 그냥 계정에 ftp로 올려버리고 DB도 안쓰고 허접한 앨범을 만들어봤습니다.

그냥 쓴 방법은 PHP 4.3.0 부터 지원되는 glob이라는 함수를 이용한 겁니다.
이 함수는 현재 디렉토리에서 파일명들을 뽑아내는 함수인데요..
이 함수로 jpg나 bmp 파일을 찾아서 순서대로, 페이지별로 IMG태그에 물려주는 겁니다. 디자인 같은 것.. 없습니다. 테이블? 안사용합니다. 그냥 이미지 태그 써주고 <BR>로 줄바꿔주고 끝입니다. 파일명이 나오는 순서는 알파벳 순서 같은데.. 음..거기까지는 신경 안썼습니다.

index.php

<html>
<head>
<title> Album </title>
</head>
<body>

<?php
/***********************Album***********************/
/********it's crude.. -_-
       Daniel J. Song
        2003.7.26
       There's no license of this.
*********/
@extract($HTTP_GET_VARS);       // page와 files_per_page 변수 가져옵니다.
if (!$page || $page<1)
        $page=1;
if (!$files_per_page || $files_per_page<1)
        $files_per_page=5;      //페이지당 5개 그림.
$i=0;
foreach (glob("*[jJ][Pp][Gg]") as $filename) {  //JPG 파일을 뽑아냅니다.
        if ($i >= ($page-1)*$files_per_page && $i < $page*$files_per_page)
                echo "<IMG src=$filename> <br>";        //현재 페이지 번호에 맞는 파일 프린트
        $i++;   // $i로 세는 거죠.

}
foreach (glob("*[Bb][Mm][Pp]") as $filename) {  //이번엔 BMP파일을 찾습니다.
        if ($i >= ($page-1)*$files_per_page && $i < $page*$files_per_page)
                echo "<IMG src=$filename> <br>";
        $i++;
}
/**************그림 끝****************/
/*********** 이전, 다음 버튼 *********/
if ($page>1)
        echo "<a href='index.php?page=". ($page-1) ."&files_per_page=5'>이전</a> ";
if ($i<=$page*$files_per_page)  // $i= # of all pictures
        ;//echo "end of pictures<br>\n";        // end of files
else
        echo "       <a href='index.php?page=". ($page+1) ."&files_per_page=5'>다음</a>";

echo "<br>";
/************* 앞뒤 5섯개 페이지씩 링크 ********/
if ($page<6)
        $j=1;
else
        $j=$page-5;
for (; $j<$page; $j++)
        echo "<a href='index.php?page=". $j ."&files_per_page=5'>[". $j ."]</a> ";
echo "[".$page."]";

$max_page= ($i)/$files_per_page;
for ($j=$page+1; $j<$max_page+1 && $j<$page+6; $j++) {
        echo  "<a href='index.php?page=". $j ."&files_per_page=5'>[". $j ."]</a> ";
}

?>
</body>
</html>


사용법은 그림 파일(jpg나bmp)을 임의의 디렉토리에 업로드 하신 다음에 이 index.php 파일을 같은 디렉토리에 업로드해주시면 됩니다.
Forums: 
맹고이의 이미지

저도 게시판에 업로드하기
귀찮아서 앨범을 만들고 있었는데..
glob()란 함수가 있었네요-_-;
진작 알았으면 좋았을껄..T_T

$curdir = getcwd();
$handle = opendir($curdir);
        
$directory = array(); 
$file = array();
        
while($var = readdir($handle)) { 
    if(is_dir($curdir . "/" . $var)) { 
        if($var != '.') 
            $directory[]= $var; 
    } elseif (substr($var,-4) == ".jpg" || substr($var,-4) == ".gif" || substr($var,-4) == ".png" || substr($var,-4) == ".JPG" || substr($var,-4) == ".GIF" || substr($var,-4) == ".PNG"){
        $file[] = $var;
    }
} 
closedir($handle);
	
sort($file); 
reset($file);

그래서 전 이렇게 만들었는데... 허접하죠?^^;
glob()란 함수를 안쓰고 더 깔끔한 방법 없을까요?

아, 저도 앨범을 만들어봤어요...

http://gon1982.nafly.net/library/project/image_viewer/view_main.php?dir=picture1&page=1

Quote:
view_main.php
view_img.php
function.php
config.php

파일이 네개라서 소스는 올리기가... ^^;

생각하고 만든게 아니라

생각하면서 만들어서 소스가 좀 조잡합니다.-_-;

그래도 혹시나 필요한데 만들기 귀찮으신 분은 메시지를...

hyunuck의 이미지

glob 함수가 없더라도 이렇게 하시면 간단할것 같은데요 :-)

아래 코드를 적절히 사용하면 될 것 같습니다.

 
 <?php
       $command = "ls -t | egrep -i '.(gif|jpg|bmp|png)$' > filename.txt";
       $filename = "filename.txt";
       shell_exec("$command");
       $line = file($filename);
      
       for($i=0; $i < sizeof($line); $i++)
       {
            echo"<img src=\"$line[$i]\">";
       }
 ?>

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.