foreach ($files as $file) { if (in_array($file[count($file)-1], $musicTypes)) { rename("/home/m/".join('.', $file), "/home/m/public_html/Music/".join('.', $file)); } elseif (in_array($file[count($file)-1], $videoTypes)) { rename("/home/m/".join('.', $file), "/home/m/public_html/Video/".join('.', $file)); } elseif ((in_array($file[count($file)-1], $imageTypes)) { rename("/home/m/".join('.', $file), "/home/m/public_html/Images/".join('.', $file)); } elseif (in_array($file[count($file)-1], $docTypes)) { rename("/home/m/".join('.', $file), "/home/m/public_html/Docs/".join('.', $file)); } }