Save files to folder

I have a php code, the essence of it is to save the file to a folder

function save_source_code($cName, $cData)
{
    $cache = fopen("source/" . $cName . ".tmp", "w+");
    fwrite($cache, $cData);
    fclose($cache);
}
function get_source_code($cName)
{
    return @file_get_contents("source/" . $cName . ".tmp");
}
function clear_source_code($cName, $tClean)
{
    $cTime = filemtime("source/" . $cName . ".tmp");
    $cLive = (time() - $cTime) / 3600;
    if ($cLive >= $tClean) {
        unlink("source/" . $cName . ".tmp");
    }
}

I call it like this. Example:

$mCode = get_source_code ( md5($_GET['id']) );

Everything works, but the isp manager folder does not save more than 100,000 files. I want to create another subfolder after the folder /source/with the name of the first character .$cName.
So that all files are not in the same folder, but in different ones. Please tell me how this can be done.


Answer 1, authority 100%

function creat_dir($cName){
   $dir=substr($cName, 0, 1);
   if(!is_dir("source/".$dir)){
      if(mkdir("source/".$dir, 0777)){
         return $dir;
      } else return false;
   } else return $dir;
}

And rewrite your save_source_code() function:

function save_source_code($cName,$cData) {
   $dir=creat_dir($cName);
   if($dir){
       $cache = fopen("source/".$dir."/".$cName.".tmp", "w+");
       fwrite($cache, $cData);
       fclose($cache);
   }
}