2011-03-07 3 views
0

Coldfusion MX를 사용 중이고 .htaccess 파일의 일부를 동적으로 만들고 싶습니다.Java 또는 Coldfusion 파일 작업

예를 들어 내가 Start and End 부분 사이의 내용을 삭제하고 새로운 내용을 기록 할

> # --- Start Part1 ---# 
> 
> # --- End Part1 ---# 

지금 때마다 나는이 ColdFusion에서의 내용을 .htaccess로 생성과 같은 일부의 시작과 끝의 모양이 이리.

Coldfusion에서 가능합니까?

감사

답변

1

같은 파일 .htaccess.template 가정 :

# BEFORE 
# --- Start Part1 ---# 

# --- End Part1 ---# 
# AFTER 

을하여 ColdFusion 스크립트가있다 같은 디렉토리에서 (이름은 중요하지 않음) htaccess.cfm 말 :

<!--- note double pound signs, necessary to escape in CF ---> 
<cfset start = "## --- Start Part1 ---##"> 
<cfset end = "## --- End Part1 ---##"> 

<cfsavecontent variable="replacement"><cfoutput> 
I will appear between the start and end comments! 
Replace me with what you want to appear in the .htaccess file. 
</cfoutput></cfsavecontent> 

<cfset template = fileRead(getDirectoryFromPath(getCurrentTemplatePath()) & "/.htaccess.template")> 

<cfset startPos = find(start, template)> 
<cfset endPos = find(end, template)> 

<cfset before = left(template, startPos + len(start) - 1)> 
<cfset after = right(template, len(template) - endPos + 1)> 

<cfset content = "#before##replacement##after#"> 

<!--- <cfoutput><pre>#content#</pre></cfoutput> ---> 

<cfset path = getDirectoryFromPath(getCurrentTemplatePath()) & "/.htaccess"> 
<cfif fileExists(path)><cfset fileDelete(path)></cfif> 
<cfset fileWrite(path, content)> 

이 생성됩니다 같은 디렉토리에있는 파일 .htaccess. 한 가지 문제는 삭제/덮어 쓰기를 방지하기 위해 .htaccess에있는 파일 시스템 잠금을 처리하는 것으로 생각합니다.이 상황에서 어떤 작업을해야하는지 확신 할 수 없습니다. 이 예제의 .htaccess에서

은 다음과 같습니다

# BEFORE 
# --- Start Part1 ---# 
I will appear between the start and end comments! 
Replace me with what you want to appear in the .htaccess file. 
# --- End Part1 ---# 
# AFTER 
1
  1. 구분자 endline의 CHR의 것을 사용 listToArray()를 사용한 배열에 VAR 차례
  2. <cffile>
  3. 와 VAR로 파일을 읽
  4. 루프를 통해 출력 <cfsavecontent> 블록을 열고 도달 할 때까지 배열 # --- Start Part1 ---#
  5. 자신의 콘텐츠 추가
  6. (210)
  7. 루프를하고 # --- End Part1 ---#
  8. 루프를 통해 출력 배열에 도달 할 때까지 당신이
  9. 불행하게도 <cffile>

으로 새의 .htaccess에 저장된 컨텐츠를 작성하는 말,하지까지 도달 할 때까지 배열을 건너 우리가 사용할 수있는 CF8은 메모리를 소모하지 않고 한 줄씩 임의로 큰 파일을 읽습니다. 참조 : http://coldfused.blogspot.com/2007/07/new-file-io-in-coldfusion-8.html