...

View Full Version : check if file exists on server



cullie140
10-11-2006, 08:38 PM
Hello, I am trying to find a way to search to see if there is a specific file located in the current directory on the server. If a folder contains file1.txt or file2.txt and depending on which file is in that folder change a certain javascript variable. What it comes down to is, is there a way to search for specific files on the server and limit that to the current directory/folder. Thank you for any help you may provide.

Philip M
10-12-2006, 08:26 AM
JavaScript is purely a client side language which has no ability to read from or write to a file, nor can it access or read the directory on the server.

mrhoo
10-12-2006, 03:35 PM
If you are checking a file with the same server/domain origin as the
page running the code you can check the status response of a http request (aka AJAX):


function isFile(str){
var O= AJ();
if(!O) return false;
try{
O.open("HEAD", str, false);
O.send(null);
return (O.status==200) ? true : false;
}
catch(er){
return false;
}
}


function AJ(){
var obj;
if (window.XMLHttpRequest){
obj= new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try{
obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
}
catch(er){
obj=false;
}
}
return obj;
}

cullie140
10-12-2006, 05:32 PM
First of all, I appreciate the help I received. But I am confused about how the code by mrhoo works. I made a small if statement based on the code and it doesnt work. I think this is because I am confused on what the function returns as a value. Here is what I wrote.


if(isFile(student.txt) == "true")
var folderName = "STUDE";
else if(isFile(alumni.txt) == "true")
var folderName = "ALUMN";
else if(isFile(employer.txt) == "true")
var folderName = "EMPLO";
else if(isFile(faculty.txt) == "true")
var folderName = "FACUL";
else if(isFile(about.txt) == "true")
var folderName = "ABOUT";

I thought the function would return TRUE of FALSE based on whether the file was there or not. Any help appreciated.

cullie140
10-23-2006, 09:30 PM
Still trying to solve this problem. Does anyone have some ideas? I would really appreciate some help. I'm an asp programmer, so I'm always lost when it comes to Javascript. Thanks

Philip M
10-23-2006, 10:10 PM
true and false are Boolean values. Remove the quotation marks
around == "true"
with quotes the comparison you are making is with the string literal "true".

_Aerospace_Eng_
10-23-2006, 10:12 PM
You should also be passing a string to the isFile function

if(isFile('student.txt') == true)
var folderName = "STUDE";
else if(isFile('alumni.txt') == true)
var folderName = "ALUMN";
else if(isFile('employer.txt') == true)
var folderName = "EMPLO";
else if(isFile('faculty.txt') == true)
var folderName = "FACUL";
else if(isFile('about.txt') == true)
var folderName = "ABOUT";

felgall
10-23-2006, 11:21 PM
== true is redundant since comparing true to true is always true and comparing false to true is always false and therefore the left hand side of the test is identical to the result of the whole test.

_Aerospace_Eng_
10-24-2006, 05:38 AM
Hmm good point felgall. That is true (no pun intended).

if(isFile('student.txt'))
var folderName = "STUDE";
else if(isFile('alumni.txt'))
var folderName = "ALUMN";
else if(isFile('employer.txt'))
var folderName = "EMPLO";
else if(isFile('faculty.txt'))
var folderName = "FACUL";
else if(isFile('about.txt'))
var folderName = "ABOUT";

A1ien51
10-24-2006, 01:01 PM
check the status code. If it is 200 it is there. If it is 404, you know it is not there. I did this a long time ago on my blog: http://radio.javaranch.com/pascarello/2005/06/24/1119626686861.html


Eric

warrenta
12-03-2007, 11:16 PM
I found this code just yesterday so I know I am late to this party. I can't get it to work. It returns a false everytime I think. I am trying to implement a general purpose pageProtect script that simply uses a user password entered as the htm file destination. I am doing this to have a simple way of only allowing authorized users into certain areas of my web site....you would have to know the starting html file name to get access. Before I added this code it would default to my server 404 error handling when I entered a known bad file name....which is ok but not pretty. I thought if I could check first to see if the file entered was valid before I tried to go to it I could send the user back a nifty popup "reject" or "access denied" message instead. But everything I enter get's me the reject message.

Here is my code:

When the user clicks on the button on my link page to go to a protected area my pageProtect function sends him a popup page (pageprotect.htm) telling him this web area is protected and asks him for a password. Here is that function:

<SCRIPT LANGUAGE="javascript">
<!--- Hide from tired old browsers
var protect_password_window = null;
function pageProtect() {
protect_password_window = window.open('pageprotect.htm', 'theProtector',
'width=350,height=200,top=250,left=270,location=no,toolbar=no,status=no,menubar=no,resizable=yes');
}
// End hiding --->
</SCRIPT>

My pageprotect.htm page contains a form with the password field and a submit button and the rest of my scripts needed. This page has the isFile and AJ functions imbedded in it. Here is that page:
<HTML>
<HEAD>
<TITLE>Page Protector</TITLE>

<SCRIPT LANGUAGE="javascript">
<!--- Hide from tired old browsers
var protect_reject_window = null;
function pageProtectno() {
protect_reject_window = window.open('pageprotectno.htm', 'rejectProtect',
'width=350,height=200,top=250,left=270,location=no,toolbar=no,status=no,menubar=no,resizable=yes');
}
// End hiding --->
</SCRIPT>

<SCRIPT LANGUAGE="javascript">
<!--- Hide from tired old browsers
function isFile(str){
var O= AJ();
if(!O) return false;
try{
O.open("HEAD", str, false);
O.send(null);
return (O.status==200) ? true : false;
}
catch(er){
return false;
}
}
// End hiding --->
</SCRIPT>

<SCRIPT LANGUAGE="javascript">
<!--- Hide from tired old browsers

function AJ(){
var obj;
if (window.XMLHttpRequest){
obj= new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try{
obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
}
catch(er){
obj=false;
}
}
return obj;
}
// End hiding --->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
<!--- Hide from tired old browsers that should be put to pasture.

function goForit() {
var location;
var password;
var goodpage;
<!-- goodpage="rhcadminweb/adminstdframea.htm";
password=this.document.testform.inputbox.value;
location="rhcadminweb/" + password + ".htm"
if (isFile(location)) {fetch(location); theProtector=window.close();}
<!-- if (location==goodpage) {fetch(location); theProtector=window.close();}
else
{theProtector=window.close(); pageProtectno();}
}

function fetch(location) {
var root;
if (opener.closed) {
root=window.open('','theProtectorsGopher','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbar s=yes,resizable=yes,copyhistory=no');
root.location.href = location;
} else {
opener.location.href = location;
}
}

// End hiding --->

</SCRIPT>
</HEAD>

<BODY>



<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="75&#37;">
<TR>
<TD ROWSPAN=2 WIDTH="30%">
<img border="0" src="clips/pageprotectorno.gif"
</td width="150" height="175">

<TD WIDTH="70%" ALIGN=CENTER VALIGN=MIDDLE>
<FONT FACE="ARIAL"><B>Hold on there buddy.<br>
This Web Site Area is RESTRICTED.<br>
You'll need a password to get in here!</B></FONT><BR>
</td>

<tr>
<TD WIDTH="70%" ALIGN=CENTER VALIGN=middle>
<CENTER>
<FORM NAME="testform">
<INPUT TYPE="text" NAME="inputbox" VALUE="" size=20>
<INPUT TYPE="button" NAME="button" Value="Submit Password" onClick="goForit(this.form)">
</FORM>
</CENTER>
</td>
</tr>


</TABLE>

</BODY>

</HTML>

When the user enters a password and clicks on the "Submit Password" button he is sent to the goForit function. This function assembles the destination url in the location variable then checks to see if the file exists using the isFile(location) function. If the isFile(location) function returns a true then the user is sent to this page by the fetch(location) function and the original popup window is closed. If the isFile(location) function returns a false then the original p[opup window is closed and the user is sent an "Access Denied" popup page (pageprotectno.htm) by my pageProtectno function.

I believe the isFile (location) test is failing false every time it is executed. I put in some code to hardwire a check to see if location contained a known valid url (contained in goodpage variable) and when this code is turned on it works just fine...so I guess I am sure that the rest of my code is ok....but the security is severely compromised. I need the isFile function (or something like it) to work for me to use this as a standard way to securely check for the existance of a file name.

I am an unskilled javascript guy. I feel lucky to get this far with this already but now I am stumped. Can anybody help me out here?

Philip M
12-04-2007, 12:28 PM
For an example of how to create a customised 404 Error page, see

http://www.pageresource.com/zine/custom404.htm

warrenta
12-04-2007, 06:23 PM
Good suggestion Phillip M. I was trying to avoid touching the host server facilities in any way if possible. That's why I was trying to check to see if the file exists before attempting to load it...and if the file did not exist then not do the page load but instead send the user a message from my script that his entry was invalid. But if there is no other way then I will attempt to send a "personalized" 404 page instead from the server. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum