tags:

views:

39

answers:

3

I am working on an intranet application that uses PHP and jQuery. When a user submits a form, the form is passed to another script via AJAX.

On one such form the user supplies a Windows UNC path to a server on the network. The handler script needs to create the directory the user specifies so that files can be moved via another process.

If I run a script from the web server using mkdir('\server\path1\newpath') it works just fine. So I know the web server user has correct access rights.

But when I use the exact same command to the same network server in a script called via AJAX it fails with "No such file or directory".

Does the application lose its identity in an ajax call? Any ideas?

Thanks.

A: 

I had no idea backslashes can be this hard to deal with. Your choices are

  1. Make users enter an alternate directory seperator and use str_replace on the php side.
  2. Ask users to enter 2 backslashes and the directory seperator and then use escape when posting, when it reaches php it will be the correct seperator of one backslash.

    submitForm = function(path) { $.ajax({ type: 'POST', url: 'your_script.php', data: {path: escape(path)}, success: function() { } }); }

    submitForm('[2 backslashes]ugly[2 backslashes]windows[2 backslashes]path')

Not suprisingly the backslashes are preventing me from using the code formatting in the editor.

Lincoln B
+1  A: 

Found the answer - one of those "oh!" moments. The user form field had a space at the beginning of it (it is populated from a database field). A simple trim fixed that issue.

Thanks for reading the question, and apologies for my aging eyes.

menkes
A: 

In Javascript you will need to escape the path value with encodeURIComponent(). On the PHP side you will need to decode it with rawurldecode(). This will eliminate backslash issues for sure.

Does the application lose its identity in an ajax call? Any ideas?

Under normal condition - no. It should preserve session. But if you manipulate cookies somehow - make sure you don't overwrite session id cookie.

Ogre_BGR