checking for a file

Discussion in 'HTML' started by Eric, May 16, 2006.

  1. Eric

    Eric Guest

    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
     
    Eric, May 16, 2006
    #1
    1. Advertising

  2. Barbara de Zoete, May 16, 2006
    #2
    1. Advertising

  3. Eric

    Neredbojias Guest

    To further the education of mankind, Eric <> vouchsafed:

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

    --
    Neredbojias
    Infinity has its limits.
     
    Neredbojias, May 17, 2006
    #3
  4. Eric

    Martin Jay Guest

    In message <>, Eric <>
    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.
    --
    Martin Jay
     
    Martin Jay, May 17, 2006
    #4
  5. Eric

    Eric Guest

    Martin Jay wrote:

    > In message <>, Eric <>
    > 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
     
    Eric, May 19, 2006
    #5
  6. Eric

    Eric Guest

    Neredbojias <http://www.neredbojias.com/fliam.php?cat=alt.html> wrote:

    > To further the education of mankind, Eric <> vouchsafed:
    >
    >> 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.
    >

    Why a timing loop? whats that got to do with anything?
    The size check sounds promising, how do you do that?
    Thanks
    Eric
     
    Eric, May 19, 2006
    #6
  7. Eric

    Andy Dingley Guest

    Eric wrote:
    > 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.
     
    Andy Dingley, May 19, 2006
    #7
  8. Eric

    Martin Jay Guest

    In message <>, Eric <>
    writes
    >Martin Jay wrote:
    >> In message <>, Eric <>
    >> 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";
    }
    ?>
    --
    Martin Jay
     
    Martin Jay, May 19, 2006
    #8
  9. Eric

    Eric Guest

    Martin Jay wrote:

    > In message <>, Eric <>
    > writes
    >>Martin Jay wrote:
    >>> In message <>, Eric <>
    >>> 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";
    > }
    > ?>

    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
     
    Eric, May 19, 2006
    #9
  10. Eric

    Eric Guest

    Andy Dingley wrote:

    >
    > Eric wrote:
    >> 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.


    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
    >
    > 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.
     
    Eric, May 19, 2006
    #10
  11. Eric

    Martin Jay Guest

    In message <>, Eric <>
    writes
    >Martin Jay wrote:


    >> 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";
    >> }
    >> ?>


    >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?
    --
    Martin Jay
     
    Martin Jay, May 19, 2006
    #11
  12. Eric

    Eric Guest

    Martin Jay wrote:

    > In message <>, Eric <>
    > writes
    >>Martin Jay wrote:

    >
    >>> 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";
    >>> }
    >>> ?>

    >
    >>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?

    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
     
    Eric, May 19, 2006
    #12
  13. Eric

    Neredbojias Guest

    To further the education of mankind, Eric <> vouchsafed:

    >>>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?


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

    --
    Neredbojias
    Infinity has its limits.
     
    Neredbojias, May 19, 2006
    #13
  14. In article <>, says...
    > Andy Dingley wrote:
    >
    > >
    > > Eric wrote:
    > >> 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.

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

    --

    Jonathan
     
    Jonathan N. Little, May 19, 2006
    #14
  15. Eric

    I V Guest

    On Tue, 16 May 2006 14:46:07 -0700, Eric wrote:

    > 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>
     
    I V, May 20, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc S. Gibian

    Rigorous File operation error checking

    Marc S. Gibian, Jul 15, 2003, in forum: Perl
    Replies:
    2
    Views:
    1,198
    Andrew Savige
    Jul 22, 2003
  2. brad
    Replies:
    2
    Views:
    4,009
    Joe Smith
    Jan 19, 2004
  3. Rich Bogle

    File Name Checking

    Rich Bogle, Apr 22, 2004, in forum: Perl
    Replies:
    2
    Views:
    503
    Jim Gibson
    Apr 22, 2004
  4. Vicky

    Checking file upload size

    Vicky, Feb 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    377
    =?Utf-8?B?QXZuZWVzaA==?=
    Feb 11, 2004
  5. blue

    Checking File Permissions

    blue, Jul 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    392
    Ken Cox [Microsoft MVP]
    Jul 15, 2004
Loading...

Share This Page