CFDIRECTORY Tag에서 다중목록 Filter 적용
Category : ColdFusion Comments : 댓글쓰기 Posted by 장창학 at 11:19 AM
Bookmark and Share

ColdFusion에서 cfdirectory태그는 여러가지 기능을 가지고 있지만 cfdirectory action="LIST"와 같이 특정 디렉토리안의 모든 파일목록을 불러올 수 있는 기능을 제공합니다. 물론 Filter 옵션이 있어서 특정 확장자만, 또는 측정 파일명만, 또는 특정 파일명중 앞자리 3자리가 동일한 것만 등 다양한 옵션을 주어 파일을 목록화할 수 있습니다.

오늘은 간단하게 특정 폴더안의 모든 이미지 파일을 불러와서 jpg, png, gif와 같은 이미지 파일을 목록화 해보고, 특정 파일명으로 시작하거나 특정 확장자만 정렬해 보겠습니다.

uploads라는 디렉토리안의 모든 이미지를 추출하고자 하는 경우는 다음 코드와 같이 filter="*.jpg|*.JPG|*.png|*.PNG|*.gif|*.GIF" 와 같이 각각의 이미지 확장자별로 모든 파일을 필터링하면 됩니다. 대소문자를 구분하는 Linux 시스템에서의 체크를 위해 확장자 대소문자 필터를 추가합니다.

그런데, 이 필터 옵션에서 filter="sp*.*" 와 같이 옵션을 주게 되면, sp로 시작하는 파일을 모두 가져오게 됩니다. 여기서는 이미지 파일만 디렉토리안에 있으므로 sp로 시작하는 두개의 이미지 파일만 목록화됩니다. 간단하지만 파일필터를 할때 매우 유용한 코드입니다.

Demo 보기

<cfdirectory action="LIST" 
				   name="Images" 
				   directory="#ExpandPath('./uploads')#" 
				   filter="*.jpg|*.JPG|*.png|*.PNG|*.gif|*.GIF">

<cfoutput query="Images">
	<a href="./uploads/#Images.name#" target="_blank">
		<img src="./uploads/#Images.name#" border="0" width="150"><br />
		#Images.name#
	</a>
	<br />
</cfoutput>

<cfdirectory action="LIST" 
				   name="filenames" 
				   directory="#ExpandPath('./uploads')#" 
				   filter="sp*.*">

<cfoutput query="filenames">
	<a href="./uploads/#filenames.name#" target="_blank">
		<img src="./uploads/#filenames.name#" border="0" width="150"><br />
		#filenames.name#
	</a><br />
</cfoutput>

추가팁 !!
만약, 어떤 디렉토리안에 이미지 파일이 있는데 이것을 광고등의 목적으로 랜덤하게 하나씩 보여줘야 한다면 어떻게 해야 할까요? 다음과 같이 해볼 수 있습니다. 어때요? 간단하죠?

<cfdirectory action="LIST" 
                   name="Images" 
				   directory="#ExpandPath('./uploads')#" 
				   filter="*.jpg|*.JPG|*.png|*.PNG|*.gif|*.GIF">

<cfoutput>
<img src="./uploads/#Images.name[RandRange(1, Images.RecordCount)]#" border="0">
</cfoutput>

Comments


Write your comment



(it will not be displayed - 실제로 댓글 목록에서는 출력되지 않습니다)



Leave this field empty:




About me

Categories

Monthly Archives

Links

Recent Posts

Recent Comments