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');
}
adds a
sleep(60);
before the name.– Guilherme Lautert
Use a Sleep and wait for the page to load for an impossible minute there is no other alternative?
– Rose
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.
– gmsantos
What purpose to rename?
– Papa Charlie
Create a Thread to not stop the Main Thread from reloading the page
– Giancarlo Abel Giulian
@Papa Charlie’s reason for renaming is for the file not to become available after execution
– Rose
Can create a
temp
from the original and compare the date when it is loaded, so it will no longer be available.– Papa Charlie