[ad_1]
I am trying to save an audio file that I record at the time of filling out the form.
In the form I have an input type=”file” which normally saves the attached files in it, whether they are audio or documents, saves the storage path in the database and saves the file on the server’s storage disk.
But just as the attachment is saved I also want to save the recorded audio.
If a file is attached, it is not necessary to record audio, but if no file is attached, it is necessary to record audio and save it.
This is the form code:
<form name=search id="search" action='uploadfile_audio.php' method=post enctype="multipart/form-data">
<input type=hidden name=uploadby value="<?php echo $uploadby; ?>">
<input type=hidden name=study_uidd value="<?php echo $study_uidd; ?>">
<input type=hidden name=inform value="<?php echo $inform_uniqueid; ?>">
<input type=hidden name=user value="<?php echo $uservalidate; ?>">
<input type=hidden name=user_id value="<?php echo $usercreate; ?>">
<input type=hidden name=user_id value="<?php echo $fullname; ?>">
<div class="row cols-2">
<div class="col-sm-12 col-md-12 col-lg-5">
<label class="bodytext"><b>Nombre del paciente</b></label>
<input style="width: 100%;" type=text readonly class="bodytext caja_texto"
value="<?php echo $patient_name; ?>">
<br><br>
<label class="bodytext"><b>Identificación del paciente</b></label>
<input type=text readonly class="bodytext caja_texto" style="width: 100%;"
value="<?php echo $patient_id; ?>">
<br><br>
<label class="bodytext" style="width: 49%;"><b>Edad</b></label>
<label class="bodytext" style="width: 50%;"><b>Sexo</b></label>
<br>
<input type=text readonly class="bodytext caja_texto" style="width: 49%;"
value="<?php echo $patient_age; ?>">
<input type=text readonly class="bodytext caja_texto" style="width: 50%;"
value="<?php echo $patient_sex; ?>">
<br><br>
<label class="bodytext"><b>Descripción del estudio</b></label>
<input readonly type="text" class="bodytext caja_texto" style="width: 100%;"
value="<?php echo $study_description; ?>" />
<br><br>
<label class="bodytext"><b>Diagnóstico</b></label>
<input readonly type="text" class="bodytext caja_texto" style="width: 100%;"
value="<?php echo $comment; ?>" />
<br><br>
<div style="border: 1px solid #fff; border-style: dashed; border-radius: 5px; padding: 10px;">
<label for="search" class="bodytext form-label" style="font-size: 1em;"><b>Audio</b></label>
<br>
<!-- ADJUNTAR AUDIO -->
<label for="search" class="bodytext form-label"><b>Subir</b></label>
<input class="bodytext caja_texto form-control" type="file" id="formFileAudio" name="upload"
style="width: 100%; padding: 5px;">
<tr>
<td class="bodytext">
<table>
<?php if ($e == 1) { ?>
<tr>
<td class=error>El documento no pudo ser subido al servidor PACS.</td>
</tr>
<?php } ?>
<tr>
<td class=bodytext style="width: 50%;">
<?php if ($audio_control == true) { ?>
<img id=btnsound title="Escuchar sonido" src="https://stackoverflow.com/questions/72483874/image/sound.png" border=0
style="cursor: pointer;" onclick="showDiv('soundedit');">
<?php } ?>
</td>
</tr>
</table>
</td>
</tr>
<!-- ADJUNTAR AUDIO -->
<br>
<label for="search" class="bodytext form-label"><b>Grabar</b></label>
<!-- GRABAR AUDIO -->
<div>
<audio id="audio" name="audio" controls></audio>
<p></p>
<input id="record" type="button" class="btn btn-success" value="Grabar" />
<input id="pause" type="button" class="btn btn-warning" value="Pausar" disabled />
<input id="resume" type="button" class="btn btn-primary" value="Continuar" disabled />
<input id="stop" type="button" class="btn btn-danger" value="Parar" disabled />
</div>
<!-- GRABAR AUDIO -->
</div>
<br>
</div>
<div class="col-sm-12 col-md-12 col-lg-7">
<div class="row cols-3">
<div class="col-sm-12 col-md-12 col-lg-4 order-3 order-sm-1" style="text-align: center;">
<input type="button" id="btnRecord" value="Iniciar Dictado" />
<div style="font-size: 12pt; padding: 10px; padding-top: 18px; position: absolute;">
<div id="interimResult">
</div>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-4 order-1 order-sm-2" style="text-align: center;">
<?php echo "<a onclick='openViewer(\"$study_uid\", \"2\", \"$inform_uniqueid\", \"$modality\");' style="width: 250px;" ><img class=textworklist alt="Ver Imágenes"
title="Ver Imágenes" src="/image/Boton Lupa Visor.png" style="cursor: pointer; width: initial; height: inherit; border-radius: 5px; border-bottom: initial; padding: initial;"></a>"; ?>
</div>
<div class="col-sm-12 col-md-12 col-lg-4 order-2 order-sm-3" style="margin-top: 13px; margin-bottom: 1rem; text-align: center;">
<?php
if ($document_count > 0)
echo "<a href="download.php?s=$study_uniqueid" target="_blank"><img alt="Documento cargado" title="Documento cargado" style="width: 36px;" src="/image/documentocargado.png"></a>";
else
echo "<span> </span>";
?>
<a href="download.php?s=<?php echo $study_uniqueid ?>" target="_blank" class="botonhead"
style="margin-top: 0.4em; padding: 5px; border: 4px #2ad4ff solid !important;">Descargar
Imágenes</a>
</div>
</div>
<label class="bodytext"><b>Informe del estudio</b></label>
<textarea name=inform_text id=inform_text style="border-radius: 5px; font-size: 12pt; border: 1px solid silver; padding: 10px; background-color: #213544; border: 1px #105d94 solid;" class="bodytext"><?php echo $inform_text; ?></textarea>
<br>
<label class="bodytext"><b>Recomendaciones</b></label>
<textarea name=recomendaciones id=recomendaciones style="border-radius: 5px; font-size: 12pt; border: 1px solid silver; padding: 10px; background-color: #213544; border: 1px #105d94 solid; height: 30% !important;" class="bodytext"><?php echo $recomendaciones; ?></textarea>
<br>
<?php if ($role == 15) { ?>
<label class="bodytext"><b>Médico Cardiólogo</b></label>
<?php echo $util->getInformedByCardio($inform_by_id, $userbusiness); ?>
<?php } else { ?>
<label class="bodytext"><b>Médico Radiólogo</b></label>
<input type=text readonly class=caja_texto style="width: 100%;"
value="<?php echo $fullname; ?>">
<input type=hidden name=informedby id=informedby value="<?php echo $usercreate; ?>">
<?php } ?>
<br>
<input type=checkbox name=is_novedad id=is_novedad onclick='uncheck()'> <b>Es una novedad?</b>
<input type=checkbox name=is_alerta_critica id=is_alerta_critica onclick='uncheck()'> <b>Alerta
crítica?</b>
<?php if ($modality == 'MG') { ?>
<label><b>Birad</b></label>
<label class="bodytext"><?php echo $util->getBirad($birad); ?></label>
<?php }else{ ?>
<label><b>Birad</b></label>
<label class="bodytext"><?php echo $util->getBirad($birad); ?></label>
<?php } ?>
<br>
<?php if ($uploadby == $inform_by_id) { } ?>
<br>
<input type=submit class="botonactualizar" value="Guardar informe">
</div>
</div>
</form>
This is the script with which I record the audio:
<!-- GRABAR AUDIO -->
<script>
if (navigator.mediaDevices) {
var chunks = [];
var audio = document.getElementById('audio');
var record = document.getElementById('record');
var pause = document.getElementById('pause');
var resume = document.getElementById('resume');
var stop = document.getElementById('stop');
navigator.mediaDevices.getUserMedia({
audio: true
})
.then(function(stream) {
var mediaRecorder = new MediaRecorder(stream);
record.onclick = function() {
if (mediaRecorder.state != 'inactive') {
return;
}
record.disabled = true;
pause.disabled = false;
stop.disabled = false;
mediaRecorder.start();
}
pause.onclick = function() {
if (mediaRecorder.state != 'recording') {
return;
}
pause.disabled = true;
resume.disabled = false;
mediaRecorder.pause();
}
resume.onclick = function() {
if (mediaRecorder.state != 'paused') {
return;
}
pause.disabled = false;
resume.disabled = true;
mediaRecorder.resume();
}
stop.onclick = function() {
if (mediaRecorder.state == 'inactive') {
return;
}
record.disabled = false;
pause.disabled = true;
resume.disabled = true;
stop.disabled = true;
mediaRecorder.stop();
}
mediaRecorder.onstop = function(e) {
audio.controls = true;
var blob = new Blob(chunks, {
'type': 'audio/ogg; codecs=opus'
});
chunks = [];
audio.src = URL.createObjectURL(blob);
}
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
}
})
.catch(function(err) {
console.log('The following error occurred: ' + err);
})
}
</script>
<!-- GRABAR AUDIO -->
and this is the code of the file action uploadfile_audio.php with which I save the attachment in the database and on the disk:
//Guardar archivo
//Guardar archivo
if($_FILES["upload"]["tmp_name"] != "" || $_FILES["upload"]["tmp_name"] != null){
print_r($_FILES["upload"]["tmp_name"]);
echo " <br>";
$usercreate = $acc->getUserID($uservalidate);
$filename = "E:\\document\\{$uploadby}_" . date('Ymdmi') . '_' . $_FILES["upload"]["name"];
$filetemp = $_FILES["upload"]["tmp_name"];
$filetype = $_FILES["upload"]["type"];
if (move_uploaded_file($filetemp, $filename)) {
$query = " insert into tb_document(study_uniqueid, document, upload_by, doctype)
values('$study_uidd', '$filename', '$uploadby', '$filetype')";
if ($db->query($query)){
$archivoAudio = true;
}
}else {
header('Location: loadfile_audio.php?e=1');
}
}
//Guardar archivo
Thank you very much in advance for your help.
[ad_2]