当前位置: 动力学知识库 > 问答 > 编程问答 >

echo text if .php extentin exists in a folder using a php script

问题描述:

im trying to make a php script that if a .php extention exists in a folder it says check mail, if no .php files exist no mail either one or the other results show up not both.

<?php

$directory = "http://server1.bioprotege-inc.net/roleplay_realm_online/contact_us_files/";

if (glob($directory . "*.php") != true) {

echo 'Check Mail:';

}

else {

echo 'No mail Today';

}

?>

that what I got but it aint working it only shows the same result if there is a .php file in the folder or not

网友答案:

The glob() function returns the array of files name with extension so you can not check them with true and false.

Check the reference site: glob function in php

That's why you should use following code to check it:

<?php
$directory = "http://server1.bioprotege-inc.net/roleplay_realm_online/contact_us_files/";

// Open a known directory, and proceed to read its contents
if (is_dir($directory)) {
    $arr = array();
    if ($dh = opendir($directory)) {
        while (($file = readdir($dh)) !== false) {
            $arr[] = $file;
        }
        $name = implode($arr);
        if(strstr($name,".php")){
             echo "Check Mail:";
        } else {
              echo "No mail Today";
        }
        closedir($dh);
    }
}
?>

This will be helpful to you. I hope so.

网友答案:

Understanding the risk, this is how it can be done

$dir="core/view/";

if(glob($dir . "*.php")!=null)
    echo " New Mail";
else
    echo "No Mail";

You can use glob if you have less than a 100k files, else you may get a Allowed memory size of XYZ bytes exhausted ..." error.

In that case you can change the setting in php.ini or you can use

readdir()

You can use readdir() in this manner

if ($handle = opendir('core/view')) {
    $flg=0;
    while (false !== ($entry = readdir($handle))) {
        if(strcasecmp(pathinfo($entry, PATHINFO_EXTENSION),"php")==0){
        $flg=1;
        break;
    }
}
echo $flg;
closedir($handle);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: