views:

52

answers:

4

Hi All,

For a PHP project I'm working on, focussing on data homogenity, a certain directory structure can be managed. After a user completes a process creating an object (I won't bother you guys with all the project details;)) this directory structure should be copied to a mapped network drive. Since the users might have the networkdrive mapped to a different driveletter, I'm using javascript (FileSystemObject) to get the driveletter. This is all OK. But now comes the tricky part. How am I able to copy my directorystructure, managed on the webserver by the webapp I'm building, to this networkdrive?

I know I can use the FSO to create new directories, my problem is how to pass the recursive directorystructure from PHP into javascript.

I'm quite stuck in thinking of a solution. So that's why I posted it here. Hope to hear some perspectives from you.

Thanks in advance! Ben

A: 

HTTP does not define anything like a "directory structure"; any mapping of HTTP responses to the filesystem is accidental at best.

Ignacio Vazquez-Abrams
+1  A: 

Since you can't copy a directory structure using HTTP you'll have to create a helper object which will hold directory information. I would use array exported through JSON to JS. Array structure would be like this:

array(1) {
  ["/"]=>
  array(5) {
    ["dir1"]=>
    array(3) {
      ["dir1_1"]=>
      array(1) {
        ["file1.txt"]=>
        string(30) "http://domain/dir1_1/file1.txt"
      }
      ["file1.txt"]=>
      string(28) "http://domain/dir1/file1.txt"
      ["file2.txt"]=>
      string(34) "http://domain/dir1/file2.txt"
    }
    ["dir2"]=>
    array(1) {
      ["file1.txt"]=>
      string(28) "http://domain/dir2/file1.txt"
    }
    ["file1.txt"]=>
    string(23) "http://domain/file1.txt"
    ["file2.txt"]=>
    string(23) "http://domain/file2.txt"
    ["file3.txt"]=>
    string(23) "http://domain/file3.txt"
  }
}

Optionally you can assable the URLs from the key values to save some traffic, but I think this is not the case you need it.

Once you got the object in your JS you just copy the files one by one.

Petr Peller
A: 

? There is no FileSystemObject in Javascript nor ECMAscript - its a jscript extension.

I would guess from this that you are trying to develop on a MS Windows OS (it would have been helpful if you'd mentioned this).

Also, there's a lot of reasons why a jscript downloaded from a weserver should not be able to access the local filesystem - the default settings in MSIE will not allow this, and it won't work in any other browser.

Where does the directory structure reside? On the web server? On the browser client? Your question as to how to pass the details from PHP to jscript rather implies the former - so why not copy the files over using PHP? It'll be a lot simpler than implementing it on the client even disregarding the security problems.

But to answer your question:

 $i=0;
 print "<script>\nvar filesToCopy=New Array();\n";
 foreach ($file_to_copy as $f) {
     print "filesToCopy[$i]='$f';\n";
     $i++;
 }
 print "your_file_copy_js(filesToCopy);\n</script>\n";

C.

symcbean
A: 

I've figured it out. I have recursively read my directorystructure, build an array, looped through that array and put together a javascriptfunction which can use the FileSystemObject to create new directories.

The only downside is that I was not able to add content to files. But that's not that much of a problem. Petr Peller, thanks for you suggestion, it was the lead to my solution.

Ben Fransen