Problem with counter

Greetings!

here’s a listing of my script which is supposed to do the following :

  • read the given folder content (file names are like : john1.mp3, john2.wma, peter1.wma, peter2.mp3, etc…)
  • ‘take out’ the user’s name from the file name : john1.mp3 –> john
  • count the files of john and increment the counter $nr_of_audios
  • later the script saves the next uploaded audio file to john4.mpr/wma if the john3.mp3/wma is tha LAST
  • I hope I was clear enough… :slight_smile:

here’s the code :


// START OF AUDIOS LIST CHECK
  $count = 0;
  if(is_dir($audios_folder)) {
	 if($handle = opendir($audios_folder)) {
	 while(($file = readdir($handle)) !== false) {
	 $count++;
  }
  closedir($handle);
  }
  }

  $dh  = opendir($audios_folder);
  while (false !== ($file__name = readdir($dh))) {
	$files[] = $file__name;
	}
		
	for ($i=0; $i<$count; $i++) {

	$pattern = array("'_'", "'0'", "'1'", "'2'", "'3'", "'4'", "'5'", "'6'", "'7'", "'8'", "'9'"); // get rid of the numbers and _
	$replace = array('', '', '', '', '', '', '', '', '', '', ''); 
		
	$user_audio[$i] = preg_replace($pattern, $replace, $files[$i]);

	$file_ext[$i] = substr($files[$i], strrpos($files[$i],".")+1); // file ext

	$audio_username[$i] = basename($user_audio[$i], ".".$file_ext[$i]); // just john or peter
					
	if ($audio_username[$i] == $user_name) { // if there's a file increment the counter
		
	$nr_of_audios++;
	}
	}
	// END OF IMAGES LIST CHECK

$hashedfilename = $user_name.$nr_of_audios.".".$ext;

Now what is happening :

  • if there’s NO file with the given username, the first file is saved, let’s say john1.mp3
  • when John tries to upload his next audio (wma in this case), the script should save it as john2.wma BUT is saves it as john1.wma… :confused:
    I get lost cause I’m looking at the code and trying modifications for an hour and I bet it’s one line of code that has to be corrected I just can’t see where and what’s wrong…
    Any help is appreciated!!! Thank you advanced!