简体   繁体   中英

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. 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. JavaScript will fail because browser prevent access to the file system with JavaScript for security reasons.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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