Rename file after 1 minute

-2

How can I rename the file after 1 minute? The idea is to rename the file when the music is finished.

Part of my code:

 echo'<embed src="arquivo.mp3" width="1" height="1" autostart="true"></embed>';
 $NomeReal = "arquivo.mp3";
 $RealNome = "arquivo.tmp";
 rename($NomeReal,$RealNome);
    
asked by anonymous 07.09.2016 / 07:22

1 answer

2

With Javascript + Ajax

Instead of using embed, use the <audio> tag because it has Javascript events that you can combine with ajax to achieve the desired effect, so do this:

<audio id="audio" autoplay controls src="arquivo.mp3" type="audio/mpeg" width="1" height="1"></audio>

<script type="text/javascript">

function request(url, callback) {
    var xhr = new XMLHttpRequest();

    xhr.open("GET", "ajax_info.txt", true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            callback(xhr.responseText);
        }
    };

    xhr.send(null);
}

document.getElementById("audio").addEventListener("ended", function() {
    request("renomearaudio.php?audio=arquivo.mp3", function(resposta) {
          console.log(resposta);
    });
});

</script>

Only as PHP and HTML

You can also do something similar to another answer that I quoted link

Load the file through PHP, the HTML should look like this:

<audio id="audio" autoplay controls src="ouvir.php?path=arquivo.mp3" type="audio/mpeg" width="1" height="1"></audio>
  

Here you can use embed if you want (although the <audio> tag is preferable), it would look something like:

<embed src="ouvir.php?path=arquivo.mp3" width="1" height="1" autostart="true"></embed>

and the listen.php file like this:

<?php
function mimeType($file)
{
    $mimetype = false;

    if (class_exists('finfo')) {//PHP5.4+
        $finfo     = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype  = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else if (function_exists('mime_content_type')) {//php5.3 ou inferiror
        $mimetype = mime_content_type($file);
    }

    return $mimetype;
}

$path = empty($_GET['path']) ? null : $_GET['path'];

if ($path && file_exists($path))
{
    $mime = mimeType($path);

    //Muda content-type para que o arquivo php seja reconhecido como imagem
    header('Content-Type: ' . $mime);

    //Exibe
    echo file_get_contents($path);

    //Renomeia
    rename($path, 'novonome.mp3');
} else {
    header('HTTP/1.0 404 Not Found');
}
    
08.09.2016 / 04:06