doupo2241 2014-02-26 09:23
浏览 44
已采纳

检查文件夹存在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

展开全部

  • 写回答

2条回答 默认 最新

  • dqkx69935 2014-02-26 09:30
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部