重命名文件不能在php上运行

Hi i have problem with this code with the code i can change whole my folder file to number like 1.mp4 2.mp4 ect...

i test the code and i print the name of the file from it and every thing is right but rename function is not working this is my code

$dir = opendir('.');

$i = 1;

// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{


    // if the extension is '.mp4'
    if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'mp4')
    {
 echo $file ;

        // do the rename based on the current iteration
        $newName = $i . '.mp4';
        rename($file, $newName);

        // increase for the next loop
        $i++;
    }
}

// close the directory handle
closedir($dir);

?>

what is the problem now ?

NEW iNFO

i tried the code inside my localhost and it's work but it's not working inside the server 

It's more efficient to use glob()

foreach(glob(__DIR__ . '/*.mp4') as $key => $file) {
  if(!rename($file, __DIR__ . '/' . ($key + 1) . '.mp4')) {
    throw new Exception('Unable to write to '. $file);
  }
}

I'll hazard a guess it's a write permissions issue though - I don't see anything directly wrong with the script.