www.webdeveloper.com
Results 1 to 8 of 8

Thread: Use Java to list files in a directory and then pull into an iframe

  1. #1
    Join Date
    Feb 2012
    Posts
    4

    Use Java to list files in a directory and then pull into an iframe

    Hello and thank you in advance for your time!
    I am building a page that will display a file in an iframe.
    The problem is that the files won't necessarily be static.
    We are collecting weather data at 1 min. intervals which is saved into a flat file. I will be uploading these files (and re-uploading at set intervals) into a set directory with a naming scheme of Feb12log.* (type of file doesn't really matter. could be .txt, .csv, .xls) but the name will change corresponding with the month & year: {MMM}{DD}log.{ext}. Each month, a new file is created
    I can successfully pull a file into an iframe. I can build a drop box and a static list of files (File1, File2, File3 etc). I am pretty sure that I can build a search box in the page that will parse the information in the iframe (Ctrl+F would work as well I guess). Hopefully this is enough background info.
    Instead of building a page for each new month ad nauseam, I would love to be able to pull a list of files currently in the upload directory into a dropdown box and when a file is selected, have it display in an iframe on the same page. The ability to be able to search the page would be awesome.
    So I can pull a drop down from an array:


    Code:
    <script language="javascript">
    
    function optionSel(selectbox,text,value )
    {
    	var optn = document.createElement("OPTION");
    	optn.text = text;
    	optn.value = value;
    	selectbox.options.add(optn);
    }
    
    function Option_list(selectbox){
    var month = new Array("Jan12","Feb12","Mar12","Apr12","May12","Jun12","Jul12","Aug12","Sep12","Oct12","Nov12","Dec12");
    for (var i=0; i < month.length;++i){
    
    optionSel(document.drop_list.Month_list, month[i], month[i]);
    }
    }
    </script>
    </head>
    
    <body onLoad="Option_list()";>
    	<FORM name="drop_list" action="yourpage.php" method="POST" >
    		
    		<SELECT  NAME="Month_list">
    			<Option value="" >Month list</option>
    		</SELECT>
    	</form>
    </body>
    And I have figured out how to pull multiple links into a target iFrame:


    Code:
    <html>
    <head>
    <TITLE>Site Current Month Totals</TITLE>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </HEAD>
    <TABLE ALIGN=Left BORDER=1 BGCOLOR="#80FF00">
    <TR>
    <TD><H4>Monthly Totals</H4>
    
    
    <UL>
    <LI><A HREF="Jan12log.txt" TARGET="DisplayWindow">Document 1</A>
    <LI><A HREF="Feb12log.txt" TARGET="DisplayWindow">Document 2</A>
    <LI><A HREF="Mar12log.txt" TARGET="DisplayWindow">Document 3</A>
    </UL>
    
    <IFRAME 
        SRC="VerCurMonth.htm"
        NAME="DisplayWindow"
        WIDTH=1024 HEIGHT=480>
    </IFRAME></TD>
    </TR></TABLE>
    		
    </BODY>
    </HTML>
    Does anyone have an idea for code for a recursive search that can read a directory into an array? It could be a server side script.
    If I can figure that out, and figure how to tie it all together, the only thing left is to format the directory list as links and it should be good.
    Thoughts?

    I am not looking for a handout, and I appreciate any input at all. My questions are: 1. Is this possible? Or should I resign myself to manually creating new pages every month? 2. Would anyone be able give me a nudge in the correct direction?
    Thanks again!
    M

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    JS can't read the content of a folder unless a server-side script makes the data available, and if you can do that then why not just use server-side code to generate the list of options?

    Alternatively if you're using a consistent naming scheme, a script could use the date to predict the contents of the folder.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Feb 2012
    Posts
    4

    Thanks for the response!

    Quote Originally Posted by Logic Ali View Post
    JS can't read the content of a folder unless a server-side script makes the data available, and if you can do that then why not just use server-side code to generate the list of options?
    Alternatively if you're using a consistent naming scheme, a script could use the date to predict the contents of the folder.
    @Logic, Thank you for the response. The directory portion would be server-side, but I do not know where to start on that (I Perhaps posted in the wrong forum). I suppose reading the contents into an array might not be too difficult (although I can't seem to wrap my mind around it)..but then how to pass that array to the client in order to populate the the drop down????

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    You can always build javascript files with php for the preceding months (or days for the current month) to load this scripts dynamically. Then you have only to read with PHP the files of the day.

  5. #5
    Join Date
    Feb 2012
    Posts
    4

    To Clarify

    Again, thanks for the replies.
    To clarify a bit: I will have a directory on a web server "WeatherFiles". I am looking to build a script that will read a list of files in that directory into an array (or something else, if better suited).
    From that array, I plan to populate a drop down box (Or even just a list) with the list of those files (which I hope to make links) that will then, when clicked populate an iFrame on the same page.
    Through all of my searches, I cannot seem to find something that will fit the bill. I now understand that the script or piece of programming that polls the directory needs to be server side, I just have no idea where to start in order to build it....

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    You have to work on the server with PHP and with readdir.
    There is not very difficult to build a list of files with this kind of script (index.php) to run on a server (like to test EasyPHP, WAMP or an other local server)

    Code:
    <?php
      	$dir = opendir("WeatherFiles");
    	//List files in WeatherFiles directory
    	$lst='';
    	while (($file = readdir($dir)) !== false){
    		// conditions on filenames
      		if ($file!='.' && $file!='..') $lst.=",".$file;
      	}
      	closedir($dir);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Untitled</title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    // we display our string (without the first comma) in a javascript var
    var lst="<?php echo substr($lst,1); ?>".split(','); //to build an array
    
    </script>
    </head>
    <body>
    Good luck !
    </body>
    </html>
    The you will be able to build different arrays, to sort them and so on... (See array, array functions, sort, explode, implode...)
    Last edited by 007Julien; 02-07-2012 at 07:42 AM. Reason: complements

  7. #7
    Join Date
    Feb 2012
    Posts
    4

    I will give it a go

    @007Julien,
    Thank you for the reply!
    I will install php on the server and give it a whirl!
    I will post back when I have a result.

  8. #8
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    594
    You could use AJAX to first pull the list and again later to pull the selected file. That way you don't need to make any changes to the server.
    Please give me a hand at http://www.gofundme.com/bkzr98.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles