简体   繁体   English

检查输入文件夹是否存在于PHP或JavaScript中

[英]Check if an input folder exists or not in PHP or JavaScript

I have a form which takes a couple of text field inputs and then a folder path. 我有一个表单,需要几个文本字段输入,然后是一个文件夹路径。 Now before I submit the form I want to make sure that whatever the folder path the user specified is correct, if not print an error message. 现在,在提交表单之前,我要确保用户指定的文件夹路径正确无误(如果未显示错误消息)。

Is there a way I can validate this in the same page before I submit the form? 在提交表单之前,是否可以在同一页面上对此进行验证?

I used javascript, but it doesnt seem to work as I expected. 我使用了javascript,但似乎无法正常工作。 Thoughts/Suggestions ? 想法/建议?

<script>
 function checkfolder()
 {
   var myObject;
   myObject = new ActiveXObject("Scripting.FileSystemObject");
   if(!myObject.FolderExists()){
     alert("Folder does not exist");
    }
  }
</script>
<form method=post action="some_file.php">
.
.
.

<input type="submit" name="submit" value="submit" onClick='checkFolder()'>
</form>

You're not going to have much luck with this. 您不会为此感到太多运气。 PHP can't do this because it operates on the server and has no access to the user's computer. PHP无法执行此操作,因为它在服务器上运行并且无法访问用户的计算机。 JavaScript will fail because browser prevent access to the file system with JavaScript for security reasons. JavaScript将失败,因为出于安全原因,浏览器阻止使用JavaScript访问文件系统。

You don't have any way of accessing the local data to validate its existence. 您没有任何访问本地数据以验证其存在的方法。 Even if you do, it's considered really bad. 即使您这样做,也被认为是非常糟糕的。

Instead of writing the file path (which I assume that's what you are doing), just make a javascript Browse file dialog like when you upload an image or file to Gmail. 无需编写文件路径(我假设这就是您正在做的事情),只需像在将图片或文件上传到Gmail一样,创建一个javascript Browse文件对话框即可。 That kind. 那样。 This ensures that it exists at least when you are trying to upload the file. 这样可以确保它至少在您尝试上传文件时存在。

Whether the file actually gets deleted after you have selected the file, and before you submit it. 在选择文件之后和提交之前,实际上是否删除了该文件。 It doesn't matter. 没关系 If it doesn't exist, the upload will fail. 如果不存在,则上传将失败。

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

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