5

as title says, is it possible to monitor a local dir in the real filesystem (not html5 sandbox)? I'd like to write an automatic photo uploader that looks for new photos and uploads them.

luca
  • 12,311
  • 15
  • 70
  • 103
  • I don't think it can be done with client-side scripts. You must use a server-side code such as PHP for this as far as I know. Similarly, see also: http://stackoverflow.com/questions/6920343/using-javascript-to-search-a-directory-for-the-most-recent-file-and-create-a-url – Solo Jan 19 '13 at 15:51
  • No, that would be a security problem. – ZippyV Jan 19 '13 at 15:51
  • @Solo If he could run any server-side code than he wouldn't need to upload it to a server anymore. – ZippyV Jan 19 '13 at 15:53
  • @ZippyV - Yes, but what I'm saying is I don't think it's possible to read a directory using JavaScript. – Solo Jan 19 '13 at 15:54
  • @ZippyV It is possible in all browsers that implement the filesystem api. Chrome 21+ is the only browser that has this support, though. – Ray Nicholus Jan 19 '13 at 16:02
  • @Ray Nicholus - Interesting as I didn't know this. It kind of isn't a way to go yet however because it isn't cross-browser and isn't the proper way to do it. But, nice to know this. – Solo Jan 19 '13 at 16:10
  • is it possible in chrome? I thought it was only the sandboxed html5 filesystem, not the real one. Provide links please ;-) – luca Jan 19 '13 at 16:31
  • @luca of course it's sandboxed. you can read more about the filesystem api here http://www.w3.org/TR/file-system-api/ – Ray Nicholus Jan 19 '13 at 16:37

2 Answers2

1

Potential repeat of Local file access with Javascript.

My understanding is that you can't access the local filesystem directly through a web browser, you have to use an intermediary like the form input tag or drag and drop.

You may be able to get away with accessing the filesystem if you were to use the operating system's javascript interpreter or something like V8. There may also be experimental javascript api's in Chrome that you could look for on the Chrome flags page if thats your browser of choice. That all depends on whether or not you were doing a personal project or something for the web.

Otherwise another scripting language such as PHP, Ruby, or Python would better suit your needs.

Community
  • 1
  • 1
  • It used to be impossible to do this on the client-side, but some browsers are now able to access the local file system using the [File System Access API](https://stackoverflow.com/a/51150691/975097). – Anderson Green Feb 23 '22 at 21:26
-1

You can set a Javascript Timing event. ie: use the setInterval() method.

On the other hand, you can make a button to trigger an onClick event, or any other event, to execute the following code.

NOTE:

If you set an interval, make sure the request was received before sending it again.

For achieving this, you need to check that the readyState of your XML HTTP Request equals 4, as follows:

xmlhttp.readyState == 4

NOTE:

This is for sending the request, parsing the response and putting it in a Javascript array:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "check_dirs.php", true);
xmlhttp.send();

fileArray = new Array();

xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        xmlDoc = xmlhttp.responseXML;

        fileList = xmlDoc.getElementsByTagName("filesChanged");

        while (fileArray.length > 0)

        // clean the whole array.
        // we want to store the newly generated file list
        {
                    fileArray.pop();
        }

        for (i = 0; i < fileList.length; i++)
        {
            fileArray[fileArray.length] = fileList[i].childNodes[0].nodeValue;
        }
    }
} 

Moreover, you will need to write a little PHP script to check your custom directory for files newer than a given date, that could be sent in the request by the way, and send an XML response back, like this:

<?php

(...) // check dir. output $files contain the xml nodes for the files to send

      // mockup below


  // Get our XML. You can declare it here or even load a file.
  $xml_builder = '<?xml version="1.0" encoding="utf-8"?>';

  $xml_builder .= $files;

  // We send XML via CURL using POST with a http header of text/xml.
  $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.hello..co.uk');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  curl_close($ch);

  /*
  echo $ch_result;
  */
?>

Here are some mockup functions to check the directory and build the XML response:

<?php

function analizeDir($dir)
{
    if (is_dir($dir))
    {
        $dir_resource = opendir($dir);
        while (false !== ($res = readdir($dir_resource)))
        {
            if ($res != "." && $res != ".." && $res != "old")
            {
                if (is_dir($dir . "\\" . $res)) // this is a subforder
                {
                    analizeDir($dir . "\\" . $res);

                } else { // this is a file

                    checkFile($dir . "\\" . $res);
                }
            }
        }
    }
}

function checkFile($file)
{
    $today = date("Y-m-d H:i:s");

    // if the difference in days between today
    // and the date of the file is more than 10 days,
    // print it in the response

    if (date_diff(datemtime($file), $today) > 10)
    {
         $files .= "<filesChanged>" . $file . "</filesChanged>";
    }
}

?>
canolucas
  • 1,482
  • 1
  • 15
  • 32