Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    check if file exists on server

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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):

    Code:
    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;          
        	}
    }
    Code:
    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;  
    }

  • #4
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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.

  • #5
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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".

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You should also be passing a string to the isFile function
    Code:
    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";
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    == 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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm good point felgall. That is true (no pun intended).
    Code:
    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";
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #10
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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/pascarell...626686861.html


    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #11
    New Coder
    Join Date
    Dec 2007
    Location
    SoCal
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    For an example of how to create a customised 404 Error page, see

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

  • #13
    New Coder
    Join Date
    Dec 2007
    Location
    SoCal
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •