gpt4 book ai didi

Java 或 Coldfusion 文件操作

转载 作者:行者123 更新时间:2023-12-02 08:22:28 25 4
gpt4 key购买 nike

我正在使用 Coldfusion MX,我想动态创建 .htaccess 文件的一部分。

例如,我有一个零件的开始和结束看起来像

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

现在,每次我在 Coldfusion 中生成此 .htaccess 内容时,我都想删除 Start 和 End 之间的内容部分并在此处写入新内容。

Coldfusion 中可能吗?

谢谢

最佳答案

假设文件.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

关于Java 或 Coldfusion 文件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5218933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com