Jump to content

Is It Possible...


therschbach

Recommended Posts

...for AJAX to look into a folder on the server to see what files exist?I have a bunch of archive text documents for almost every month going back to 2005 and they are all named january06, february07, etc... I would like to create a pop-up menu that generates a list of available archives depending on which ones are actually available. For example, I don't have a september08 so I would want that month grayed out on the menu. But I want the script to do this on the fly (AJAX) so I don't have to constantly update an archive menu list everytime I archive a month.If this is possible, and I imagine it is, how would I do it? Please keep in mind that I'm a noob at ajax, but getting more comfortable with js. Thanks!

Link to comment
Share on other sites

AJAX can handle almost anything your server can throw at it. Almost all the work will happen on the server side. You'll need to learn how your scripting language reads directories, so that you can generate a list of files. The list can be sent to your browser in a variety of formats:1. plain html, like a set of links, that could be inserted into a div;2. a comma-separated list of file names, which you could split into an array, and then loop through the array to build a selection box;3. a JSON string, that would end up like #2, but save you a couple of steps.I've done a thing where I have 2 select boxes side-by-side, one for directories, the next for files, looking very much like a file-open dialog. Click on a directory in the first box, and AJAX asks for a list of items in the directory. Put that list in the next select box. Very convenient. And fast, since a list of files is pretty small, data-wise. A user with DSL or faster could almost be convinced the files are on the local hard drive.

Link to comment
Share on other sites

When you say "how your scripting language reads directories", are you referring to a script on the server-side that isn't javascript? If you're referring to js, how would I go about learning how it reads directories? I really don't know where to start. :)

Link to comment
Share on other sites

He was talking about the server-side script. Your server-side script could be javascript, but only in the context of ASP running on the server, not on the client.As for starters, PHP is probably your best bet as the PHP community here is vastly larger than that of ASP, ASP.NET, and ColdFusion.

Link to comment
Share on other sites

If you're getting a handle on Javascript, you should find PHP pretty easy to learn. Since they both evolved from C, the syntax is very similar. Arrays look the same, loops look the same, functions look the same, etc. You're just doing different sorts of tasks. (Like examining your directory structure.)

Link to comment
Share on other sites

A throwback to Perl, which uses sigils (the name of these guys) to distinguish $scalars (strings and numbers) from @arrays from %hashes(associative arrays).An immediate advantage is that you can name a $variable just about anything without worrying about using a reserved word, function name, or CONSTANT.

Link to comment
Share on other sites

Start here. That's the whole document. It'll print everything you don't need to know about your PHP install, but also a few things it's good to know, like your version number. If you get a blank page, look at the source and see if the source code is visible. That means PHP isn't turned on yet.

<?php   phpinfo();?>

Get to know the manual! http://www.php.net/manualThis http://www.php.net/manual/en/function.readdir.php and this http://www.php.net/manual/en/function.glob.php can do a lot of what you need. Look at the examples. They're a huge part of the manual's value, as are the user comments that follow. Sometimes the comments can be more intense than you need, but sometimes very helpful.

Link to comment
Share on other sites

Take it slow. Don't even worry about HTML for a while. The echo statement will print text to your browser without any tags, though using the <br> tag between items can make things more readable. Try the following:echo a quoted string.set a variable to a string and echo the variable.create an array and echo the first valuewith the same array, create a for loop and echo every valueNOW look at the file systemcreate a readdir loop (just follow the examples) and echo the names of the files in the directory where your script lives. If you're developing the script in a fresh directory (a good idea!) just put some little files in there so you have something to read.modify the same loop to filter out the "." and ".." directories. You know what they are, right? BAD NEWS.Nothing teaches like practice.

Link to comment
Share on other sites

I'm currently going through the PHP tutorial at lynda.com. So far it's basically the same as C which I am somewhat familiar with although it's been a while since I've touched it. Toward the end of the tutorial (I've got a long ways to go) he talks about using it with MySQL so I think he may cover enough to get me started with what I want to do. However, if you think you can guide me through to where I need to be faster, than I will follow your mini-superspeedy-tutorial.I know what the . and .. directories are, but why is that bad to echo the names of the files in those folders?

Link to comment
Share on other sites

Echoing the aliases by itself is not bad. I think the awareness is the important thing. Imagine a really careless programmer who creates a site where users can post photos for their friends, essentially creating a unique directory for each user. Next the careless programmer creates an interface where the user can rename and delete those files. If he didn't know what the dots meant . . . bam!Needless to say, when you give users any control at all over the file system, you need to build in walls and filters so the bad stuff is impossible. (A good regex can handle a lot of the heavy lifting.)Anyway, being familiar with C is a definite plus. PHP in general and PHP 5.x have some file routines that simplify a lot of stuff, and you will like the built-in string and array functions.

Link to comment
Share on other sites

Ok, Here's what I've got so far. I'm making progress.

<html><head></head><body><?php$handle = opendir('./archives');for ($i=0; (false !== ($file = readdir($handle))); $i++) {	if ($file != "." && $file != "..") {		$files_array[$i] = $file;		}	}closedir($handle);?> </body></html>

Now I'm trying to figure out how to search the file list array to find out what years are available and what months are available for each year.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...