Go Back   SitePoint Forums > Forum Index > Program Your Site > Classic ASP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Oct 14, 2002, 03:33   #1
netmastan
SitePoint Member
 
Join Date: Sep 2002
Location: New Zealand
Posts: 15
Thumbs up simple code php to asp ...

can anyone help me to convert the following code to asp.
thanks in advance.



PHP Code:

<?PHP


// Sample script written by Daynah

// Where all your text files are located at
// This can be just . if it is the current directory
$directory = './files/';

// If the variable exists
// Example: If the url is index.php?page=aboutme
if($page)
{
       
// Does the file $directory/$page.txt exist?
       
if(is_file("$directory$page.txt"))
               include(
"$directory$page.txt");
       else
               print
"Sorry, this page does not exist";
              
//header("location:index.php");
}

// If the page is just called as index.php
// Then just include the default main.txt page
else
{
       
$mypage = 'main.txt';
       include(
"$directory$mypage");
}
?>
netmastan is offline   Reply With Quote
Old Oct 14, 2002, 04:50   #2
jofa
Sultan of Ping
 
jofa's Avatar
 
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/
jofa is offline   Reply With Quote
Old Oct 14, 2002, 06:34   #3
spartan
Rehab is for quiters!
 
spartan's Avatar
 
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
spartan is offline   Reply With Quote
Old Oct 14, 2002, 09:18   #4
Shannon
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]
__________________
Shannon
Pure Energy Systems - Low Cost, High Quality Hosting Solutions.
Shannon is offline   Reply With Quote
Old Oct 15, 2002, 03:57   #5
netmastan
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..
netmastan is offline   Reply With Quote
Old Oct 15, 2002, 09:36   #6
Shannon
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.
__________________
Shannon
Pure Energy Systems - Low Cost, High Quality Hosting Solutions.
Shannon is offline   Reply With Quote
Old Oct 15, 2002, 14:40   #7
netmastan
SitePoint Member
 
Join Date: Sep 2002
Location: New Zealand
Posts: 15
Talking Shannon

Thank you so much. Everything working fine.

cheers
netmastan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 21:13.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved