Can I use JS to search folder and create array from files

Discussion in 'Javascript' started by mdh2972@gmail.com, Jan 9, 2007.

  1. Guest

    I would like to put all my .jpg images in a folder into an array to be
    viewed. can I use JS to do this automatically. Without having to type
    by hand?
     
    , Jan 9, 2007
    #1
    1. Advertising

  2. Evertjan. Guest

    wrote on 09 jan 2007 in comp.lang.javascript:

    > I would like to put all my .jpg images in a folder into an array to be
    > viewed. can I use JS to do this automatically. Without having to type
    > by hand?


    IS "can I use JS to do this automatically" the question?

    The answer is yes, you can, but not with js in a html page on the client's
    browser without compromizing web security, if your folder is on the
    client's hard disk.

    Yes you can, if the folder is on a server and the javascript is serverside,
    probably on an ASP platform.



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 9, 2007
    #2
    1. Advertising

  3. Benjamin Guest

    wrote:
    > I would like to put all my .jpg images in a folder into an array to be
    > viewed. can I use JS to do this automatically. Without having to type
    > by hand?

    No, JS cannot access any of the files or directories it sits with on
    the server. You'd have to use a server side language like PHP, Perl, or
    ASP.
     
    Benjamin, Jan 10, 2007
    #3
  4. Randy Webb Guest

    Benjamin said the following on 1/9/2007 10:10 PM:
    > wrote:
    >> I would like to put all my .jpg images in a folder into an array to be
    >> viewed. can I use JS to do this automatically. Without having to type
    >> by hand?

    > No, JS cannot access any of the files or directories it sits with on
    > the server.


    I don't believe that. You can't get a directory listing, easily, but you
    *do* have access to any file that is in the same domain as long as the
    server doesn't prohibit access. If JS couldn't access them, AJAX would
    be a totally dead technology.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jan 10, 2007
    #4
  5. Hi,

    wrote:
    > I would like to put all my .jpg images in a folder into an array to be
    > viewed. can I use JS to do this automatically. Without having to type
    > by hand?


    You don't give enough details to answer that question. For example:

    - Is the folder with the files on the client or the server?
    - Do you have a possibility to add code to the server? For example
    ASP.NET, PHP, ...
    - Do you want the page to be refreshed when you navigate to a new folder
    or not?

    If the folder is on the client, the answer is no in normal web security
    conditions. If you have reduced security (for example in the "file"
    protocol), then you can access the client's filesystem, but that's
    probably not what you want.

    If the folder is on the server, then there are solutions using
    JavaScript, with or without server counterpart.

    Please elaborate.
    Laurent
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion [MVP], Jan 10, 2007
    #5
  6. Benjamin Guest

    Randy Webb wrote:
    > Benjamin said the following on 1/9/2007 10:10 PM:
    > > wrote:
    > >> I would like to put all my .jpg images in a folder into an array to be
    > >> viewed. can I use JS to do this automatically. Without having to type
    > >> by hand?

    > > No, JS cannot access any of the files or directories it sits with on
    > > the server.

    >
    > I don't believe that. You can't get a directory listing, easily, but you
    > *do* have access to any file that is in the same domain as long as the
    > server doesn't prohibit access. If JS couldn't access them, AJAX would
    > be a totally dead technology.

    These are different things. Yes, AJAX can acess files back at the
    server, but it has to name them specifically. You can not scan a
    directory and retrieve the listing with JS.
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    > Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Benjamin, Jan 14, 2007
    #6
  7. ASM Guest

    Benjamin a écrit :
    > Randy Webb wrote:
    >> Benjamin said the following on 1/9/2007 10:10 PM:
    >>> wrote:
    >>>> I would like to put all my .jpg images in a folder into an array to be
    >>>> viewed. can I use JS to do this automatically. Without having to type
    >>>> by hand?
    >>> No, JS cannot access any of the files or directories it sits with on
    >>> the server.

    >> I don't believe that. You can't get a directory listing, easily, but you
    >> *do* have access to any file that is in the same domain as long as the
    >> server doesn't prohibit access. If JS couldn't access them, AJAX would
    >> be a totally dead technology.

    > These are different things. Yes, AJAX can acess files back at the
    > server, but it has to name them specifically. You can not scan a
    > directory and retrieve the listing with JS.


    You probably can use an artifice,
    if access to the folder is allowed you can try to open this folder in an
    iframe or a popup, then to analyze links listed in this window via DOM
    and to extract paths of images to display them somewhere.

    OK it is not a livable way to do ... but it is possible.

    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date
     
    ASM, Jan 14, 2007
    #7
  8. Benjamin Guest

    ASM wrote:
    > Benjamin a écrit :
    > > Randy Webb wrote:
    > >> Benjamin said the following on 1/9/2007 10:10 PM:
    > >>> wrote:
    > >>>> I would like to put all my .jpg images in a folder into an array to be
    > >>>> viewed. can I use JS to do this automatically. Without having to type
    > >>>> by hand?
    > >>> No, JS cannot access any of the files or directories it sits with on
    > >>> the server.
    > >> I don't believe that. You can't get a directory listing, easily, but you
    > >> *do* have access to any file that is in the same domain as long as the
    > >> server doesn't prohibit access. If JS couldn't access them, AJAX would
    > >> be a totally dead technology.

    > > These are different things. Yes, AJAX can acess files back at the
    > > server, but it has to name them specifically. You can not scan a
    > > directory and retrieve the listing with JS.

    >
    > You probably can use an artifice,
    > if access to the folder is allowed you can try to open this folder in an
    > iframe or a popup, then to analyze links listed in this window via DOM
    > and to extract paths of images to display them somewhere.

    This is counting on the server to list the contents of the directory if
    there is no index file. Also, you would have to change your script for
    every server.
    >
    > OK it is not a livable way to do ... but it is possible.
    >
    > --
    > Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    > Stephane Moriaux and his (less) old Mac already out of date
     
    Benjamin, Jan 16, 2007
    #8
  9. Dr. No Guest

    Benjamin wrote:
    > ASM wrote:
    >> Benjamin a écrit :
    >>> Randy Webb wrote:
    >>>> Benjamin said the following on 1/9/2007 10:10 PM:
    >>>>> wrote:
    >>>>>> I would like to put all my .jpg images in a folder into an array to be
    >>>>>> viewed. can I use JS to do this automatically. Without having to type
    >>>>>> by hand?
    >>>>> No, JS cannot access any of the files or directories it sits with on
    >>>>> the server.
    >>>> I don't believe that. You can't get a directory listing, easily, but you
    >>>> *do* have access to any file that is in the same domain as long as the
    >>>> server doesn't prohibit access. If JS couldn't access them, AJAX would
    >>>> be a totally dead technology.
    >>> These are different things. Yes, AJAX can acess files back at the
    >>> server, but it has to name them specifically. You can not scan a
    >>> directory and retrieve the listing with JS.

    >> You probably can use an artifice,
    >> if access to the folder is allowed you can try to open this folder in an
    >> iframe or a popup, then to analyze links listed in this window via DOM
    >> and to extract paths of images to display them somewhere.

    > This is counting on the server to list the contents of the directory if
    > there is no index file. Also, you would have to change your script for
    > every server.
    >> OK it is not a livable way to do ... but it is possible.
    >>
    >> --
    >> Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    >> Stephane Moriaux and his (less) old Mac already out of date

    >


    Images on a remote server that you control should be trivial with PHP:

    <?php
    $localPath = '/usr/home/public_html/images';
    $remotePath = 'http://yousite.com/images';
    $fileList = '';

    echo "<script language=\"JavaScript\" type=\"text/javascript\">\n";
    echo "var remotePath = \"$remotePath\"";
    echo "var imageFilenamesArr = new Array ("

    if (is_dir($localPath)) {
    if ($dh = opendir($localPath)) {
    while (($file = readdir($dh)) !== false) {
    $fileList .= "\"$file\",";
    }
    closedir($dh);
    $fileList = substr($fileList, 0, strlen($fileList) - 1); // Remove the
    final comma.
    }
    }
    echo ");\n"
    echo "</script>";
    ?>

    That's just a draft, but the idea is that you can use PHP to generate your
    JavaScript, and basically initialize variables that require some sort of data
    from the server side environment. Its a simple, non-AJAXian solution to the
    problem. If you're running 5.2.0, you could easily do this with JSON to, but
    without the need to do complex XMLHttpRequest calls.

    On a server-side file, you _shouldn't_ be able to access it directly with
    JavaScript.
     
    Dr. No, Jan 17, 2007
    #9
    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. Replies:
    7
    Views:
    416
    Gabriel Genellina
    Sep 25, 2007
  2. Jochen Brenzlinger
    Replies:
    5
    Views:
    1,000
    Roedy Green
    Sep 2, 2011
  3. Abby Lee
    Replies:
    5
    Views:
    480
    Abby Lee
    Aug 2, 2004
  4. Paw
    Replies:
    8
    Views:
    412
  5. Replies:
    3
    Views:
    151
    Dave Weaver
    Sep 8, 2005
Loading...

Share This Page