简体   繁体   English

PHP和AJAX-mkdir问题

[英]PHP and AJAX - mkdir issue

I am working on an intranet application that uses PHP and jQuery. 我正在使用PHP和jQuery的Intranet应用程序上工作。 When a user submits a form, the form is passed to another script via AJAX. 当用户提交表单时,该表单会通过AJAX传递给另一个脚本。

On one such form the user supplies a Windows UNC path to a server on the network. 在一种这样的形式上,用户将Windows UNC路径提供给网络上的服务器。 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. 如果我使用mkdir('\\\\server\\path1\\newpath')从Web服务器运行脚本,则可以正常工作。 So I know the web server user has correct access rights. 因此,我知道Web服务器用户具有正确的访问权限。

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". 但是,当我在通过AJAX调用的脚本中对同一网络服务器使用完全相同的命令时,它将失败,并显示“无此类文件或目录”。

Does the application lose its identity in an ajax call? 应用程序是否会在ajax调用中丢失其身份? Any ideas? 有任何想法吗?

Thanks. 谢谢。

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. 感谢您阅读问题,并为我的双眼衰老道歉。

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. 使用户输入备用目录分隔符,并在php端使用str_replace。
  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. 要求用户输入2个反斜杠和目录分隔符,然后在发布时使用转义符,当到达php时,它将是一个反斜杠的正确分隔符。

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

    submitForm('[2 backslashes]ugly[2 backslashes]windows[2 backslashes]path') SubmitForm('[[2个反斜杠]丑陋[2个反斜杠] windows [2个反斜杠] path')

Not suprisingly the backslashes are preventing me from using the code formatting in the editor. 并不令人意外的是,反斜杠阻止我在编辑器中使用代码格式。

In Javascript you will need to escape the path value with encodeURIComponent(). 在Javascript中,您将需要使用encodeURIComponent()转义路径值。 On the PHP side you will need to decode it with rawurldecode(). 在PHP方面,您将需要使用rawurldecode()对其进行解码。 This will eliminate backslash issues for sure. 这肯定会消除反斜杠问题。

Does the application lose its identity in an ajax call? 应用程序是否会在ajax调用中丢失其身份? 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. 但是,如果您以某种方式处理Cookie,请确保您不会覆盖会话ID Cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM