Rename file after 1 minute

Asked

Viewed 169 times

-2

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

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);
  • adds a sleep(60); before the name.

  • Use a Sleep and wait for the page to load for an impossible minute there is no other alternative?

  • Only with PHP I don’t know if there is any way... What you can do is send another request via javascript for example just to rename.

  • What purpose to rename?

  • Create a Thread to not stop the Main Thread from reloading the page

  • @Papa Charlie’s reason for renaming is for the file not to become available after execution

  • Can create a temp from the original and compare the date when it is loaded, so it will no longer be available.

Show 2 more comments

1 answer

2

With Javascript + Ajax

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

<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 similar to another answer I quoted you /a/151523/3635

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 like (although the tag is preferred <audio>), would look something like:

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

and the file listen to php. thus:

<?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');
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.