18 Replies Latest reply on Apr 16, 2014 8:45 AM by BKBK

    how to create an txt file from a dynamic cfm page.

    RaehmLeonard

      I need to recreate the .htaccess file whenever the page structure changes.

       

      I have wrapped the cf code with cfsavecontent, then write the cfsavecontent variable using cffile.

       

      The results produce a file that just reproduces my cf code, not the net output of that code.

       

      Any suggestions?

        • 1. Re: how to create an txt file from a dynamic cfm page.
          BKBK Adobe Community Professional & MVP

          Why wrap the cf code, and not just the output?

          Oh, and what is your ColdFusion version?

          • 2. Re: how to create an txt file from a dynamic cfm page.
            RaehmLeonard Level 1

            My file looks like this:

             

            <cfsavecontent variable="myhtaccess">

             

            <font face="courier new"><font size="2">

             

            <b>****Copy, Save and Upload this file to replace '.htaccess' for Non Profit Dynamics.</b>

            <br><br>

             

            RewriteEngine on<br><br>

             

            RewriteRule index.htm index.cfm [I,O,R=301,L]<br><br>

             

            </cfsavecontent>

             

            <cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

             

            The output file simply records everything inside the cfsavecontent tags, but not the results of the cf code.

            • 3. Re: how to create an txt file from a dynamic cfm page.
              BKBK Adobe Community Professional & MVP

              There is no cf code!

              • 4. Re: how to create an txt file from a dynamic cfm page.
                RaehmLeonard Level 1

                here is the full code:

                 

                <cfoutput>

                 

                <cfsavecontent variable="myhtaccess">

                 

                <font face="courier new"><font size="2">

                 

                <b>****Copy, Save and Upload this file to replace '.htaccess' for #getaccount.client#.</b>

                <br><br>

                 

                RewriteEngine on<br><br>

                 

                RewriteRule index.htm index.cfm [I,O,R=301,L]<br><br>

                 

                RewriteCond %{HTTP_HOST} !^$<br>

                RewriteCond %{HTTP_HOST} !^www\. [NC]<br>

                RewriteCond %{HTTPS}s ^on(s)|<br>

                RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<br><br>

                 

                RewriteCond %{REQUEST_FILENAME} !-f<br>

                RewriteCond %{REQUEST_FILENAME} !-d<br>

                RewriteRule ^home index.cfm [NC,L,QSA]<br><br>

                 

                <CFQUERY

                Name="getmenu"

                Datasource="#db.02.dsn#"  >

                SELECT

                *

                FROM page

                order by pageid

                </CFQUERY>

                 

                <div class="bcopy11">

                 

                <cfloop query="getmenu" startrow="1" endrow="#getmenu.recordcount#">

                 

                <cfif getmenu.pageid neq 20>

                <cfif getmenu.pageid neq 21>

                <cfif getmenu.pageid neq 22>

                <cfif getmenu.pageid neq 23>

                 

                <cfif getmenu.pageid neq 28>

                <cfif getmenu.pageid neq 29>

                <cfif getmenu.pageid neq 30>

                <cfif getmenu.pageid neq 31>

                 

                 

                <CFQUERY

                Name="getmenusub"

                Datasource="#db.02.dsn#"  >

                SELECT

                *

                FROM pagesub

                where pageid = #getmenu.pageid#

                </CFQUERY>

                 

                <cfif getmenu.pagename contains " ">

                <cfset menu = ReReplace(getmenu.pagename, " ", "\ ", "ALL")>

                <cfelse>

                <cfset menu = getmenu.pagename>

                </cfif>

                 

                RewriteRule ^#menu# page.cfm?pageid=#getmenu.pageid# [NC,L,QSA]<br>

                 

                 

                    <!start level 2>

                 

                    <cfloop query="getmenusub" startrow="1" endrow="#getmenusub.recordcount#">

                 

                    <CFQUERY

                    Name="getmenusub2"

                    Datasource="#db.02.dsn#"  >

                    SELECT

                     *

                    FROM pagesub2

                    where pagesubid = #getmenusub.pagesubid#

                    </CFQUERY>

                 

                <cfif getmenusub.pagename contains " ">

                <cfset menusub = ReReplace(getmenusub.pagename, " ", "\ ", "ALL")>

                <cfelse>

                <cfset menusub = getmenusub.pagename>

                </cfif>

                 

                RewriteRule ^#menusub# pagesub.cfm?pagesubid=#getmenusub.pagesubid# [NC,L,QSA]<br>

                 

                 

                        <!start level 3>

                 

                 

                        <cfloop query="getmenusub2" startrow="1" endrow="#getmenusub2.recordcount#">

                 

                        <CFQUERY

                        Name="getmenusub3"

                        Datasource="#db.02.dsn#"  >

                        SELECT

                         *

                        FROM pagesub3

                        where pagesub2id = #getmenusub2.pagesub2id#

                        </CFQUERY>

                 

                <cfif getmenusub2.pagename contains " ">

                <cfset menusub2 = ReReplace(getmenusub2.pagename, " ", "\ ", "ALL")>

                <cfelse>

                <cfset menusub2 = getmenusub2.pagename>

                </cfif>

                 

                RewriteRule ^#menusub2# pagesub2.cfm?pagesub2id=#getmenusub2.pagesub2id# [NC,L,QSA]<br>

                 

                            <!start level 4>

                 

                            <cfloop query="getmenusub3" startrow="1" endrow="#getmenusub3.recordcount#">

                 

                            <CFQUERY

                            Name="getmenusub4"

                            Datasource="#db.02.dsn#"  >

                            SELECT

                             *

                            FROM pagesub3

                            where pagesub3id = #getmenusub3.pagesub3id#

                            </CFQUERY>

                 

                <cfif getmenusub3.pagename contains " ">

                <cfset menusub3 = ReReplace(getmenusub3.pagename, " ", "\ ", "ALL")>

                <cfelse>

                <cfset menusub3 = getmenusub3.pagename>

                </cfif>

                 

                RewriteRule ^#menusub3# pagesub3.cfm?pagesub3id=#getmenusub3.pagesub3id# [NC,L,QSA]<br>

                 

                 

                 

                            </cfloop>

                            <!end level 4>

                 

                        </cfloop>

                 

                        <!end level 3>

                 

                    </cfloop>

                    </ul>

                    <!end level 2>

                 

                <!end level 1>

                 

                </cfif>

                </cfif>

                </cfif>

                </cfif>

                 

                </cfif>

                </cfif>

                </cfif>

                </cfif>

                 

                </cfloop>

                 

                 

                </cfsavecontent>

                 

                #myhtaccess#

                 

                 

                <cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

                 

                </cfoutput>

                • 5. Re: how to create an txt file from a dynamic cfm page.
                  WolfShade Level 4

                  <cfsavecontent variable="myhtaccess">

                   

                  DirectoryIndex index.php index.html index.htm

                  php_value memory_limit 40M

                  php_flag magic_quotes_gpc 1

                  php_flag magic_quotes_runtime 0

                  php_flag file_uploads 1

                  php_flag session.auto_start 0

                  php_flag session.bug_compat_warn 0

                   

                  </cfsavecontent>

                   

                  <cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

                   

                  .. should do it.  Why the font tag?  <br />?  <b>?  I didn't think these were supposed to be in a .htaccess file.

                   

                  ^_^

                  • 6. Re: how to create an txt file from a dynamic cfm page.
                    RaehmLeonard Level 1

                    the output from that code should produce this:

                     

                    ****Copy, Save and Upload this file to replace '.htaccess' for clientname.

                      RewriteEngine on

                      RewriteRule index.htm index.cfm [I,O,R=301,L]

                      RewriteCond %{HTTP_HOST} !^$
                    RewriteCond %{HTTP_HOST} !^www\. [NC]
                    RewriteCond %{HTTPS}s ^on(s)|
                    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

                      RewriteCond %{REQUEST_FILENAME} !-f
                    RewriteCond %{REQUEST_FILENAME} !-d
                    RewriteRule ^home index.cfm [NC,L,QSA]

                       

                        RewriteRule ^Home page.cfm?pageid=1 [NC,L,QSA]
                                 RewriteRule ^Surveys page.cfm?pageid=2 [NC,L,QSA]
                                 RewriteRule ^Services page.cfm?pageid=3 [NC,L,QSA]
                           RewriteRule ^Program\ Costs pagesub.cfm?pagesubid=31 [NC,L,QSA]
                                      RewriteRule ^Clients page.cfm?pageid=4 [NC,L,QSA]
                           RewriteRule ^Classic-Car\ Auctions pagesub.cfm?pagesubid=36 [NC,L,QSA]
                                RewriteRule ^ECCO pagesub.cfm?pagesubid=37 [NC,L,QSA]
                                RewriteRule ^St.\ Peter\ in\ Chains pagesub.cfm?pagesubid=38 [NC,L,QSA]
                                RewriteRule ^Operation\ R\ &\ R pagesub.cfm?pagesubid=39 [NC,L,QSA]
                                RewriteRule ^Respite\ Care\ Charleston pagesub.cfm?pagesubid=40 [NC,L,QSA]
                                RewriteRule ^Pilgrim\ UCC pagesub.cfm?pagesubid=41 [NC,L,QSA]
                                RewriteRule ^The\ CARES\ Clinic pagesub.cfm?pagesubid=42 [NC,L,QSA]
                                RewriteRule ^Courageous\ Kidz pagesub.cfm?pagesubid=43 [NC,L,QSA]
                                      RewriteRule ^Testimonials page.cfm?pageid=5 [NC,L,QSA]
                           RewriteRule ^Lakeview\ UCC pagesub.cfm?pagesubid=32 [NC,L,QSA]
                                RewriteRule ^St.\ Peters pagesub.cfm?pagesubid=44 [NC,L,QSA]
                                      RewriteRule ^About\ NPD page.cfm?pageid=6 [NC,L,QSA]
                                 RewriteRule ^Contact\ Us page.cfm?pageid=7 [NC,L,QSA]
                                 RewriteRule ^Donate page.cfm?pageid=24 [NC,L,QSA]
                                 RewriteRule ^Privacy\ Policy page.cfm?pageid=25 [NC,L,QSA]
                                 RewriteRule ^Photos page.cfm?pageid=26 [NC,L,QSA]
                                 RewriteRule ^Change\ Font\ Size page.cfm?pageid=27 [NC,L,QSA]
                                 RewriteRule ^Register page.cfm?pageid=32 [NC,L,QSA]

                    • 7. Re: how to create an txt file from a dynamic cfm page.
                      BKBK Adobe Community Professional & MVP

                      I see what you mean. First things first, the outermost cfoutput tags are unnecessary. I should also like to add to what WolfShade has said.

                       

                      The tags <br>, <font> and <b> are HTML. They are usually to be rendered by the browser. So, it makes sense for them to be in the output only if you intend the final text to be displayed in a browser or some other HTML reader. So, to be consistent, you have to open the saved file in a browser, not in a text editor!

                       

                      If you wish to use the end result as a functional .htaccess file, then you have to remove the HTML tags. However, nil desperandum! There are still possibilities. For example, to ColdFusion, <br> is just a string. What ColdFusion understands as a linefeed/carriage-return is #chr(10)##chr(13)#.

                       

                      I asked for your ColdFusion version because my suggestion is to use in-memory files. This requires the following setting in the Mappings page in the ColdFusion Administrator:

                       

                      Logical Path: /inMemory

                      Directory Path: ram:///

                       

                      You could then proceed as follows:

                       

                      <cfsavecontent variable="myhtaccess_cfml">

                      etc.

                      </cfsavecontent>

                      <!--- Write to file in memory --->

                      <cffile action="write" output="#myhtaccess_cfml#" file="ram:///myhtaccessFile.cfm"/>

                       

                      <!--- Execute in-memory CFM file. Remember the mapping is called 'inMemory' --->

                      <cfsavecontent variable="myhtaccess_output">

                          <cfinclude template="/inMemory/myhtaccessFile.cfm">

                      </cfsavecontent>

                       

                      <!--- Write output to real file --->

                      <cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess_output#" >

                      • 8. Re: how to create an txt file from a dynamic cfm page.
                        BKBK Adobe Community Professional & MVP

                        I should hurry to add that the method I have just given makes sense only when the output of the first cfsavecontent is CFML code! Otherwise, you should just use the original, simpler version, namely:

                         

                        <cfsavecontent variable="myhtaccess">

                        etc.

                        </cfsavecontent>

                         

                        <!--- Write output to real file --->

                        <cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

                        • 9. Re: how to create an txt file from a dynamic cfm page.
                          Dave Ferguson Level 3

                          I really don't understand this response.  This adds tons of complexity to a solution that doesn't need it.  You could easily just do a savecontent and then write that result to a file. 

                           

                          BKBK wrote:

                           

                          .....

                           

                          <cfsavecontent variable="myhtaccess_cfml">

                          etc.

                          </cfsavecontent>

                          <!--- Write to file in memory --->

                          <cffile action="write" output="#myhtaccess_cfml#" file="ram:///myhtaccessFile.cfm"/>

                           

                          <!--- Execute in-memory CFM file. Remember the mapping is called 'inMemory' --->

                          <cfsavecontent variable="myhtaccess_output">

                              <cfinclude template="/inMemory/myhtaccessFile.cfm">

                          </cfsavecontent>

                           

                          <!--- Write output to real file --->

                          <cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess_output#" >

                          • 10. Re: how to create an txt file from a dynamic cfm page.
                            WolfShade Level 4

                            The inmemory (VFS) is a great idea as long as the CF version is 10+.  Earlier versions do not have a "per application" VFS, allowing any site/app on that server to access any file in the VFS.

                             

                            Just my $0.032749 worth.

                             

                            ^_^

                            • 11. Re: how to create an txt file from a dynamic cfm page.
                              Dave Ferguson Level 3

                              VFS is a great idea when using it makes sense.  However, using it when it will overcomplicate a solution doesn't.

                               

                               

                              WolfShade wrote:

                               

                              The inmemory (VFS) is a great idea as long as the CF version is 10+.  Earlier versions do not have a "per application" VFS, allowing any site/app on that server to access any file in the VFS.

                               

                              Just my $0.032749 worth.

                               

                              ^_^

                              • 12. Re: how to create an txt file from a dynamic cfm page.
                                RaehmLeonard Level 1

                                Thanks all for the help. Got it!  I needed to strip all the html tags and remove extraneous lines in my code.  Still needed cfoutput tags inside of cfsavecontent to generate the variables.

                                 

                                This is cool because now  the htaccess file will be re-created dynamically every time a client changes page structure.

                                 

                                P.S.  I hate SEO (search engine optimized) page names!

                                 

                                Here's the finished code:

                                 

                                <cfsavecontent variable="myhtaccess">

                                 

                                <cfoutput>

                                 

                                 

                                RewriteEngine on

                                 

                                RewriteRule index.htm index.cfm [I,O,R=301,L]

                                 

                                RewriteCond %{HTTP_HOST} !^$

                                RewriteCond %{HTTP_HOST} !^www\. [NC]

                                RewriteCond %{HTTPS}s ^on(s)|

                                RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

                                 

                                RewriteCond %{REQUEST_FILENAME} !-f

                                RewriteCond %{REQUEST_FILENAME} !-d

                                RewriteRule ^home index.cfm [NC,L,QSA]

                                 

                                <CFQUERY

                                Name="getmenu"

                                Datasource="#db.02.dsn#"  >

                                SELECT

                                *

                                FROM page

                                order by pageid

                                </CFQUERY>

                                 

                                 

                                <cfloop query="getmenu" startrow="1" endrow="#getmenu.recordcount#">

                                <CFQUERY

                                Name="getmenusub"

                                Datasource="#db.02.dsn#"  >

                                SELECT

                                *

                                FROM pagesub

                                where pageid = #getmenu.pageid#

                                </CFQUERY>

                                <cfif getmenu.pagename contains " ">

                                <cfset menu = ReReplace(getmenu.pagename, " ", "\ ", "ALL")>

                                <cfelse>

                                <cfset menu = getmenu.pagename>

                                </cfif>

                                RewriteRule ^#menu# page.cfm?pageid=#getmenu.pageid# [NC,L,QSA]

                                    <cfloop query="getmenusub" startrow="1" endrow="#getmenusub.recordcount#">

                                    <CFQUERY

                                    Name="getmenusub2"

                                    Datasource="#db.02.dsn#"  >

                                    SELECT

                                     *

                                    FROM pagesub2

                                    where pagesubid = #getmenusub.pagesubid#

                                    </CFQUERY>

                                <cfif getmenusub.pagename contains " ">

                                <cfset menusub = ReReplace(getmenusub.pagename, " ", "\ ", "ALL")>

                                <cfelse>

                                <cfset menusub = getmenusub.pagename>

                                </cfif>

                                RewriteRule ^#menusub# pagesub.cfm?pagesubid=#getmenusub.pagesubid# [NC,L,QSA]

                                        <cfloop query="getmenusub2" startrow="1" endrow="#getmenusub2.recordcount#">

                                        <CFQUERY

                                        Name="getmenusub3"

                                        Datasource="#db.02.dsn#"  >

                                        SELECT

                                         *

                                        FROM pagesub3

                                        where pagesub2id = #getmenusub2.pagesub2id#

                                        </CFQUERY>

                                 

                                <cfif getmenusub2.pagename contains " ">

                                <cfset menusub2 = ReReplace(getmenusub2.pagename, " ", "\ ", "ALL")>

                                <cfelse>

                                <cfset menusub2 = getmenusub2.pagename>

                                </cfif>

                                RewriteRule ^#menusub2# pagesub2.cfm?pagesub2id=#getmenusub2.pagesub2id# [NC,L,QSA]

                                            <cfloop query="getmenusub3" startrow="1" endrow="#getmenusub3.recordcount#">

                                            <CFQUERY

                                            Name="getmenusub4"

                                            Datasource="#db.02.dsn#"  >

                                            SELECT

                                             *

                                            FROM pagesub3

                                            where pagesub3id = #getmenusub3.pagesub3id#

                                            </CFQUERY>

                                <cfif getmenusub3.pagename contains " ">

                                <cfset menusub3 = ReReplace(getmenusub3.pagename, " ", "\ ", "ALL")>

                                <cfelse>

                                <cfset menusub3 = getmenusub3.pagename>

                                </cfif>

                                RewriteRule ^#menusub3# pagesub3.cfm?pagesub3id=#getmenusub3.pagesub3id# [NC,L,QSA]

                                            </cfloop>

                                        </cfloop>

                                    </cfloop>

                                </cfloop>

                                </cfoutput>

                                 

                                </cfsavecontent>

                                 

                                done!

                                 

                                 

                                <cffile action="write" file="#getaccount.subsite#/adm/htaccess.txt" output="#myhtaccess#" charset="utf-8">

                                • 13. Re: how to create an txt file from a dynamic cfm page.
                                  BKBK Adobe Community Professional & MVP

                                  fergusondj wrote:

                                   

                                  I really don't understand this response.  This adds tons of complexity to a solution that doesn't need it.  You could easily just do a savecontent and then write that result to a file. 

                                  Quite right. When you look back you can see I said pretty much the same thing, and mentioned where in-memory file processing would be suitable. Our posts seem to have crossed on the wire.

                                  • 14. Re: how to create an txt file from a dynamic cfm page.
                                    BKBK Adobe Community Professional & MVP

                                    RaehmLeonard wrote:

                                     

                                    Thanks all for the help. Got it!

                                    Good music!

                                    • 15. Re: how to create an txt file from a dynamic cfm page.
                                      RaehmLeonard Level 1

                                      As does it usually happen, the solution turns out to be much more simple and elegant than first anticipated!

                                       

                                      Thanks again!

                                      • 16. Re: how to create an txt file from a dynamic cfm page.
                                        BKBK Adobe Community Professional & MVP

                                        WolfShade wrote:

                                         

                                        The inmemory (VFS) is a great idea as long as the CF version is 10+. 

                                        CF 9 had it too.

                                        • 17. Re: how to create an txt file from a dynamic cfm page.
                                          WolfShade Level 4

                                          I know CF9 had it too.. it just wasn't "per application", allowing any app on the server to access any file.  The benefit to 10 is the per application preventing apps from accessing other VFS files that belong to other apps.

                                           

                                          ^_^

                                          • 18. Re: how to create an txt file from a dynamic cfm page.
                                            BKBK Adobe Community Professional & MVP

                                            @RaehmLeonard

                                            Please mark your answer as 'correct'. It will help someone else in future.