-1

How do I check if a folder on my server exists /etc/exmaple/ in jQuery or JavaScript or Ajax?

user3350731
  • 962
  • 1
  • 10
  • 30
  • 1
    Your best choice would be to call through ajax a php file checking for that – Leonardo Feb 26 '14 at 15:21
  • 1
    javascript is a client side language. You need something on server side to do it for you. – Gaurav Feb 26 '14 at 15:21
  • @Gaurav: No, it's just a language. I use it server-side all the time. And in the console for scripting. The OP is probably using it in a browser, though. – T.J. Crowder Feb 26 '14 at 15:22
  • 1
    @T.J.Crowder: While that's technically correct, this answer is tagged with "php", so in this case, JS is client-side. – Cerbrus Feb 26 '14 at 15:24
  • possible duplicate of [How do I check if file exists in jQuery or JavaScript?](http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascript) – Rajesh Dhiman Feb 26 '14 at 15:24
  • 1
    Sorry but i checked that url and it doesn't answer my question as @T.J.Crowder said, I'm using "PHP", so is there any script that i can be based on ? – user3350731 Feb 26 '14 at 15:27

2 Answers2

1

Use ajax to call a php file which checks if there is a directory called "dir"

$.post( "checkfile.php", { directory: "dir" })
.done(function( data ) {
alert( data);
});

Then in checkfile.php:

echo is_dir($_POST['directory']);
nowhere
  • 1,558
  • 1
  • 12
  • 31
0

Since you mention jQuery and Ajax, I'm going to assume you mean "With client side programming".

The client has no access to the server's file system, so it cannot directly test to see if a directory exists.

To find out anything about the server, the information either needs to come with the page or you need to use Ajax.

You can make a request to a URL on the server and see if you get a 200 response back. That will tell you if a resource exists, but it won't tell you if that is a resource created by the server mapping the URL onto the file system, finding a directory and generating an HTML document listing the files in it or something else.

Having done the above, you could check the responseText to see if it the document you got back looks like the type of page your server generated when it finds a directory.

If you really want to know if something is a directory or not, then you need server side code to do it. You have tagged the question php, so it is worth mentioning that you can use PHP for this (the is_dir function). You would make a request to a web service that you write, passing the directory you want to test the existence of as a parameter. The web service can then respond indicating if it is a directory or not.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335