user3350731
user3350731

Reputation: 972

Check folder existence Ajax - php

I want to check existence of a folder called $subdomaine in the directory \cms\sites\ on the keyup. Here is my script:

<script>
$(document).ready(function(){
$('#subdomain').keyup(subdomain_check);
}); 
function subdomain_check(){ 
var subdomain = $('#subdomain').val();
if(subdomain == "" || subdomain.length < 4){
$('#subdomain').css('border', '3px #CCC solid');
$('#tick').hide();
}else{
jQuery.ajax({
   type: "POST",
   url: "ajax.php",
   data: 'subdomain='+ subdomain,
   cache: false,
   success: function(response){
if(response == 1){
$('#subdomain').css('border', '3px #C33 solid');    
$('#tick').hide();
$('#cross').fadeIn();
}else{
$('#subdomain').css('border', '3px #090 solid');
$('#cross').hide();
$('#tick').fadeIn();
     }
 }
 });
 }
 }
</script>

ajax.php :

<?php 
$dirname = $_SESSION["subdomain"];
$filename = DOCUMENT_ROOT."sites/".$dirname;

if (!file_exists($filename)) {
   echo "The directory $dirname exists.";
    exit;
} 

How do I check the existence of the folder at the input ?

Thanks

Upvotes: 0

Views: 699

Answers (2)

Krish R
Krish R

Reputation: 22711

Try this, you need to use is_dir() function to check given file name is directory.

if (is_dir($filename)) {
  echo "The directory $dirname exists.";
  exit;
} 

For your ajax response,

 if (is_dir($filename)) {
   echo 1;      
}else{
   echo 0;
}

Upvotes: 1

sharif2008
sharif2008

Reputation: 2798

better way---

NB: change $_SESSION to $_POST too.

<?php 
 $dirname = $_POST["subdomain"];  //look this
 $dir = DOCUMENT_ROOT."sites/".$dirname;

if(file_exists($dir) && is_dir($dir)){
   echo "The directory $dirname exists.";
   exit;
} 

?>

Upvotes: 1

Related Questions