Click to See Complete Forum and Search --> : Number of files in a remote folder


Savage Monkey
10-12-2003, 07:04 AM
Hi and thanks for reading. I don't often use Javascript in my websites, but please could you help me with this?

I'd like to set a variable in my script to the number of files in a remote folder (for example, the number of files in www.mywebsite.com/images). The purpose of this is so that I can make a button to move through a series of images and stop when it gets to the last one, but I can handle this part I think ;).

Thanks a lot!

PS - on the host I will be using, you are allowed to view the contents of the folders from your browser. I just thought I'd say this as some hosts make it forbidden to do this (therefore probably making a solution to this problem impossible)

Khalid Ali
10-12-2003, 09:25 AM
with JavaScript,unless all the images are names in a sequence, its impossible to get that number.
You cna do that with any server side language.

Savage Monkey
10-12-2003, 09:38 AM
Well actually the images could be in a numbered sequence (like: 00001.jpg, 00002.jpg, 00003.jpg)... Does this mean you can accomplish this in Javascript?

Khalid Ali
10-12-2003, 10:18 AM
yep....you can ,but make sure that it might not be as reliable as it should be,

something like this will work
var isGood = true;
var imagesArray = new Array();
//take a large number
var len = 100;
for(var n=0;n<len;n++){
if(isGood){break;}
var img = new Image();
img.onerror = new function(){
isGood = false;
}
img.src = ".....";
}

I have not tested the above ,but it should work with very little tweeking.

Savage Monkey
10-12-2003, 05:44 PM
Thanks for the code Khalid, but I am not fully sure how to use it. Where am I meant to put in that script the folder to examine?

Also, what exactly does this part of the script do: img.onerror ?

Thanks a lot for any further help, I greatly appreciate your time.

Khalid Ali
10-12-2003, 07:16 PM
put it in the head section.onerror is triggered when there are not more images in the folder or loading image is not possible.

pyro
10-12-2003, 08:52 PM
As Khalid said, JavaScirpt really isn't the best tool for this job. Does your server support any server-side language (such as PHP, Perl, etc.)?