|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Member
Join Date: Sep 2002
Location: New Zealand
Posts: 15
|
can anyone help me to convert the following code to asp.
thanks in advance. PHP Code:
|
|
|
|
|
|
#2 |
|
Sultan of Ping
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Svíþjóð
Posts: 4,097
|
I think the FileExists and OpenTextFile methods of the Scripting.FileSystemObject are what you are looking for
http://msdn.microsoft.com/library/en...FileExists.asp http://msdn.microsoft.com/library/en...enTextFile.asp Multi-language reference: http://www.sloppycode.net/language-ref/ |
|
|
|
|
|
#3 |
|
Rehab is for quiters!
![]() ![]() ![]() Join Date: Apr 2002
Location: Cape Town, South Africa
Posts: 343
|
yip... use the filesystem object to check for the file...
but you'll probably need to use the server.mappath method to obtain the current directory/path somethin like this... directory = server.mappath(.) & "\files\" and since I dont really know much about PHP, I dont know what the include() function does, but it looks like a redirect to another page, so that would be... response.redirect("pathToMyFile.asp") hope that helps ![]()
__________________
Spartan --------------------- It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back. -Mr.Payne |
|
|
|
|
|
#4 |
|
SitePoint Member
Join Date: Apr 2002
Posts: 10
|
php's include works similar to vbScripts <!---#include, the difference being that vbScripts #include doesn't allow you to pass it variables, so you need to be a little sneaky.
Here's a re-write of the above code into ASP. Basic function is this, call the ASP without a "?page=" string, and it will include main.txt from the directory it's called from. Add a ?page= string, and it will try to locate the appropriate .txt file in the \files\ subdirectory. If the file does not exist it will blow out with an error. If the files does exist the script will call ReadDisplayFile to grab the contents of the file and incorporate it into the current file. Problem is that ReadDisplayFile will not process any ASP code in the file it reads, it treats it just as text. To my knowledge there's noway to true dynamic includes in ASP that also run ASP code from the included file. (Aside from a Select Case switch with an entry for each possible ?page= string and a #include for each one)... but that's not truely dynamic (you'd have to edit the code anytime you added a page) Also: If you put this in a production enviroment, you will want to put in a check for recursive directory calls (such as "?page=../../../../../../../../autoexec") just to be safe.. but let's face it, since the .txt is hardcoded, the security dangers here are minimal), but still, a check for '../' would be a good idea. [VBS] <% 'Sub routine to read in a file and incorporate it into the asp file. 'Courtesy of learnasp.com, modified to fit the needs here. SUB ReadDisplayFile(FileToRead) Dim fs, thisfile,tempSTR whichfile=FileToRead Set fs = CreateObject("Scripting.FileSystemObject") Set thisfile = fs.OpenTextFile(whichfile, 1, False) tempSTR=thisfile.readall response.write tempSTR thisfile.Close set thisfile=nothing set fs=nothing END SUB %> <% 'Sample script written by Daynah 'Converted to ASP ' Where all your text files are located at ' This can be just . if it is the current directory Dim sDirectory, sFile, fso 'take out the \files on the line below to work with the current directory instead of a subdirectory sDirectory = server.mappath(".") & "\files\" 'If the variable exists 'Example if the url is index.php?page=aboutme if(Request.QueryString("page") <> "") Then ' Does the file directory/page.txt exist? sPage = sDirectory & Request.QueryString("page") & ".txt" Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FileExists(sPage)) Then Call ReadDisplayFile(sPage) Else Response.Write("File does not exist!") End If Else %><!---#include file ="main.txt"---><% End If %> [/VBS] |
|
|
|
|
|
#5 |
|
SitePoint Member
Join Date: Sep 2002
Location: New Zealand
Posts: 15
|
thank you for the code .Everything looks ok but this link ("default.asp?page=aboutus" where aboutus is a text file in the same direcotry )is not working it says "File does Not exist!".
When i run default.asp..it grab the main.txt file. thanks again.. |
|
|
|
|
|
#6 |
|
SitePoint Member
Join Date: Apr 2002
Posts: 10
|
the .txt files should be in a subdirectory "files" off of the directory default.asp is in.
That was the way the original code was, so I left it in there.. if you want the .txt files in the same directory just edit the sDirectory = server.mappath(".") & "\files\" line to read sDirectory = server.mappath(".") & "\" and then you don't need the files subdirectory. |
|
|
|
|
|
#7 |
|
SitePoint Member
Join Date: Sep 2002
Location: New Zealand
Posts: 15
|
Thank you so much. Everything working fine.
cheers |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 21:13.










Linear Mode
