checking for a file

E

Eric

Is it possible to check for existence of a file from a web page using
javascript? I am trying to come up with a routine that i can call from the
rest of my javascript code that will check if a particular file exists on
disk at the server. ie bool FileExists("http://myServer.com/File.gif");
Any ideas on how to do this?
Thanks
Eric
 
N

Neredbojias

To further the education of mankind said:
Is it possible to check for existence of a file from a web page using
javascript? I am trying to come up with a routine that i can call from
the rest of my javascript code that will check if a particular file
exists on disk at the server. ie bool
FileExists("http://myServer.com/File.gif"); Any ideas on how to do
this? Thanks

It could sorta be done with a timing loop and a size check, but how
appropriate this is I don't know.
 
M

Martin Jay

Eric <[email protected]> said:
Is it possible to check for existence of a file from a web page using
javascript? I am trying to come up with a routine that i can call from the
rest of my javascript code that will check if a particular file exists on
disk at the server. ie bool FileExists("http://myServer.com/File.gif");
Any ideas on how to do this?

I cannot think of a way to achieve this.

You could, however, include a list of filenames in your Javascript, and
then simply check it to see whether a particular file exists.

There are some problems, though:

you may not wish to give visitors a list of all the files;

the list may be rather long;

and it might be a pain keeping the list up-to-date. :(

Does your host offer server side scripting, such as PHP or Perl? That's
probably the best solution.
 
E

Eric

Martin said:
I cannot think of a way to achieve this.

You could, however, include a list of filenames in your Javascript, and
then simply check it to see whether a particular file exists.

There are some problems, though:

you may not wish to give visitors a list of all the files;

the list may be rather long;

and it might be a pain keeping the list up-to-date. :(

Does your host offer server side scripting, such as PHP or Perl? That's
probably the best solution.

Yes, but HOW? do you determine a file is there? I'm not looking for unknown
files, I'm looking for a very specific filename.
Eric
 
E

Eric

Neredbojias said:
It could sorta be done with a timing loop and a size check, but how
appropriate this is I don't know.
Why a timing loop? whats that got to do with anything?
The size check sounds promising, how do you do that?
Thanks
Eric
 
A

Andy Dingley

Eric said:
Is it possible to check for existence of a file from a web page using
javascript?

No, it's impossible. It's forbidden by the sheer impossibiility of
accessing the server's filesystem from a remote web client, and by the
sandboxing that would obviously applied if such a feature were even
remotely feasible.

As to the client, then you're again sandboxed from looking around the
client's filesystem (and for good reason). There's a tiny amount that's
possible through manual file upload on a form, but that's hardly
directory listing.

If your client permits a client-side technology like ActiveX, then the
sky's the limit. This is completely insecure, potentially permits
anything (down to machine level access of the HD) if there's a suitable
ActiveX component written in C++ or similar and uploaded with the page.
 
M

Martin Jay

[QUOTE="Eric said:
I cannot think of a way to achieve this.

You could, however, include a list of filenames in your Javascript, and
then simply check it to see whether a particular file exists.

There are some problems, though:

you may not wish to give visitors a list of all the files;

the list may be rather long;

and it might be a pain keeping the list up-to-date. :(

Does your host offer server side scripting, such as PHP or Perl? That's
probably the best solution.
[/QUOTE]
Yes, but HOW? do you determine a file is there? I'm not looking for unknown
files, I'm looking for a very specific filename.
Eric

From the PHP manual:

<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
 
E

Eric

Martin said:
[QUOTE="Eric said:
In message <[email protected]>, Eric <[email protected]>
writes
Is it possible to check for existence of a file from a web page using
javascript? I am trying to come up with a routine that i can call from
the rest of my javascript code that will check if a particular file
exists on disk at the server. ie bool
FileExists("http://myServer.com/File.gif"); Any ideas on how to do this?
I cannot think of a way to achieve this.

You could, however, include a list of filenames in your Javascript, and
then simply check it to see whether a particular file exists.

There are some problems, though:

you may not wish to give visitors a list of all the files;

the list may be rather long;

and it might be a pain keeping the list up-to-date. :(

Does your host offer server side scripting, such as PHP or Perl? That's
probably the best solution.
Yes, but HOW? do you determine a file is there? I'm not looking for
unknown files, I'm looking for a very specific filename.
Eric

From the PHP manual:

<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>[/QUOTE]
How do i do that from javascript? Can i call it somehow? I'm in a javascript
loop looking at a series of files, and if one is missing i need to break
out of the loop early.
Thanks
Eric
 
E

Eric

Andy said:
No, it's impossible. It's forbidden by the sheer impossibiility of
accessing the server's filesystem from a remote web client, and by the
sandboxing that would obviously applied if such a feature were even
remotely feasible.

As to the client, then you're again sandboxed from looking around the
client's filesystem (and for good reason). There's a tiny amount that's
possible through manual file upload on a form, but that's hardly
directory listing.

I'm not trying to do a directory listng, I'm loading a series of images
(with specific filenames) in a loop, and if one is missing i want to break
out of the loop. So is that possible?
Eric
 
M

Martin Jay

Eric <[email protected]> said:
Martin Jay wrote:
How do i do that from javascript? Can i call it somehow? I'm in a javascript
loop looking at a series of files, and if one is missing i need to break
out of the loop early.

Perhaps you should give us the full picture so we know what you're
trying to achieve.

The answer to: "Is it possible to check for existence of a file from a
web page using javascript?" is probably no. But there may be another
solution. Are you able to run scripts on your web server?
 
E

Eric

Martin said:
Perhaps you should give us the full picture so we know what you're
trying to achieve.

The answer to: "Is it possible to check for existence of a file from a
web page using javascript?" is probably no. But there may be another
solution. Are you able to run scripts on your web server?
Ok, I've got some javascript code that loads images in a loop for a
slideshow. The images are gathered from a web cam, 1 each 5 minutes so
that i hae a history going back 2 days (This isnt published by the way its
only for my use "at this time", i may put it public later - not sure yet)
Anyway, its entirely possible that out of the 576 possible images that
something changed and maybe only images 1 to 297 (or whaever number you
like) are currently there.
So, in my javascript loop before i try to load a slide image, i wanted to
see if the pic was actualy there and if not I'd stop loading images, break
out of the loop, and just proceed on with the reduced set
Thanks
Eric
 
N

Neredbojias

Ok, I've got some javascript code that loads images in a loop for a
slideshow. The images are gathered from a web cam, 1 each 5 minutes so
that i hae a history going back 2 days (This isnt published by the way
its only for my use "at this time", i may put it public later - not
sure yet) Anyway, its entirely possible that out of the 576 possible
images that something changed and maybe only images 1 to 297 (or
whaever number you like) are currently there.
So, in my javascript loop before i try to load a slide image, i wanted
to see if the pic was actualy there and if not I'd stop loading
images, break out of the loop, and just proceed on with the reduced
set Thanks

Ahh, you should have said so at first. This is actually easy; I have done
it before, myself.

Sometime before you show the images, check for size (height or width).
This is where the timing loop comes in; you need to be sure the image has
enough time to load before size-checking. If the size remains less than a
number somewhere around 40px, the default size of the missing-image
placement icon +1, the image is missing (or unloadable). I've forgotten if
you can check for size in a js image or not, ie:

var pic=new Image();
pic.src="leek.jpg";
var x=pic.width;

....but you can always use a hidden, absolutely-positioned placebo image on
the page if nothing else.
 
J

Jonathan N. Little

I'm not trying to do a directory listng, I'm loading a series of images
(with specific filenames) in a loop, and if one is missing i want to break
out of the loop. So is that possible?
Eric

After and image loads it trips a onload event. I guess you *could* timed
loop (would require much tweaking to get it right) to test if the onload
had fired, if not load next image. Caveat, the client-side JavaScript
could never know definitively that an image is missing, only that it did
not full load by some specific time that you preset. It coul be that the
image is larger, visitor's connection is slow...whatever. My opinion very
*bad* idea.

Much better idea: put all image in a specified folder, say "slideshow"
then with PHP get a listing of the contents of the "slideshow" folder and
use it to both build the page and the JavaScript list to run the
slideshow! Then all you have to do is whatever you want in the show put
it in the folder, the rest is all automatic...
 
I

I V

Is it possible to check for existence of a file from a web page using
javascript? I am trying to come up with a routine that i can call from the
rest of my javascript code that will check if a particular file exists on
disk at the server. ie bool FileExists("http://myServer.com/File.gif");
Any ideas on how to do this?

As far as I know, there's no way to check if a file exists, but you can
try to load an image and respond appropriately depending on whether or not
it loads successfully, using the onload and onerror events. The only
problem with this is that you'll need to redesign your system to be event
based, rather than load the images in a loop. Depending on what you want
to do, this may or may not be easy. You could try something like:

<html>
<head>
<script type="text/javascript">

// ImageList class - pass a list of URLs,
// call 'get', and then callbacks will be
// called as the loading progresses -
// on_each when each image is successfully loaded,
// on_error if there is an error, and
// on_complete when all images have been loaded.
// Each callback should return 'true' if the next
// URL in the list should be loaded, or false
// if the loading should be aborted.

function ImageList(urls) {
// A list of URLs that have not yet been loaded
this.urls = urls;

// A list of images that have been successfully loaded
this.images = new Array();

// Override these three methods in your own instances
this.on_each = function (url, img) { };
this.on_complete = function () { };
this.on_error = function (url) { };

this.get = function() {
this.get_next_image();
}

this.get_next_image = function() {
var url = this.urls.shift();
if( !url ) {
this.on_complete();
return;
}
var img = new Image();
var il = this;
img.onload = function () {
il.images.push(img);
if( il.on_each(url, img) )
il.get_next_image();
}
img.onerror = img.onabort = function () {
if( il.on_error(url) )
il.get_next_image();
}
img.src = url;
return true;
}
return this;
}

// This will attempt to load four images, but it will fail to load the
// third, and abort at that point.

function test() {
image_urls = [
"http://images.google.com/intl/en/images/logo.gif",
"http://images.google.com/intl/en_ALL/images/images_hp.gif",
"http://test.invalid/no-image-here",
"http://groups.google.com/groups/img/groups_home.gif"
];

var il = new ImageList(image_urls);

// Override the error callback - returns false,
// so no further images will be loaded
il.on_error = function (url) {
document.write("Error at "+url+"<br>");
return false;
}

// Override the callback for each image load,
// returns true, so the next image will be
// downloaded
il.on_each = function (url, img) {
document.write("Got image from "+img.src +"<br>");
return true;
}

// Override the callback which will be invoked when
// all images have been successfully loaded
il.on_complete = function () {
document.write("Complete<br>");
document.write("Loaded "+il.images.length+" images<br>");
}
il.get();
}
</script>
</head>
<body onload="test()">
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top