Created
July 26, 2020 12:02
-
-
Save surferxo3/b16ea13e099ca525757da75e26cf519b to your computer and use it in GitHub Desktop.
Audio Upload based on Bitrate comparison in Kbps or Mbps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*############################# | |
* Developer: Mohammad Sharaf Ali | |
* Designation: Sr. SE | |
* Version: 1.0 | |
*/############################# | |
#function to calculate .wav audio bitrate | |
function calcBitRate($filename) { | |
$check = false; | |
$fp = fopen($filename, 'r'); | |
fseek($fp, 20); | |
$rawheader = fread($fp, 16); | |
$header = unpack('vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits', $rawheader); | |
$bitrateInKb = ($header['samplerate'] * $header['bits'] * $header['channels']) / 1000; | |
$bitrateInMb = $bitrateInKb / 1000; | |
#compare bitrate in kbps or mbps are per requirement | |
if($bitrateInKb >= 128) { | |
$check = true; | |
} | |
else { | |
$check = false; | |
} | |
return $check; | |
} | |
#button is clicked | |
if(isset($_POST['submit'])) { | |
#log errors | |
$log = array(); | |
#allowed extension i.e .wav | |
$allowedExts = array("wav"); | |
#get total count of audio files | |
$totalAudios = count($_FILES['audio']['name']); | |
#loop each audio | |
for($i=0; $i<$totalAudios; $i++) | |
{ | |
#define target directory to upload file | |
$target = $_POST['name'] . '/' . basename($_FILES['audio']['name'][$i]); | |
$targetShift = 'old/' . basename($_FILES['audio']['name'][$i]); | |
#check for valid extension | |
$temp = explode(".", $_FILES["audio"]["name"][$i]); | |
$extension = end($temp); | |
#valid .wav audio file | |
if(in_array($extension, $allowedExts)) { | |
#bitrate valid | |
if(calcBitRate($_FILES['audio']['tmp_name'][$i])) { | |
#audio file don't exist | |
if (!file_exists($target)) { | |
move_uploaded_file($_FILES['audio']['tmp_name'][$i], $target); | |
echo '<pre>'; | |
echo 'Download Link: <a href="'.$target.'">'.basename($_FILES['audio']['name'][$i]).'</a>'; | |
echo '</pre>'; | |
} | |
#audio file already exists -> move existing file to new folder | |
else { | |
if (copy($target, $targetShift)) { | |
#replace existing file in the same folder | |
move_uploaded_file($_FILES['audio']['tmp_name'][$i], $target); | |
echo '<pre>'; | |
echo 'Download Link: <a href="'.$target.'">'.basename($_FILES['audio']['name'][$i]).'</a>'; | |
echo '</pre>'; | |
} | |
} | |
} | |
#bitrate exceeds | |
else { | |
$log[] = array('File' => $_FILES["audio"]["name"][$i], | |
'Msg' => 'Bitrate Exceeds!' | |
); | |
} | |
} | |
#not a valid .wav audio file | |
else { | |
$log[] = array('File' => $_FILES["audio"]["name"][$i], | |
'Msg' => 'Invalid File!' | |
); | |
} | |
} | |
#print errors | |
for($i=0; $i<count($log); $i++) { | |
echo '<pre>'; | |
echo $log[$i]['File']." - ".$log[$i]['Msg']; | |
echo '</pre>'; | |
} | |
#display all files | |
$dir = $_POST['name'] . '/'; | |
$files = scandir($dir); | |
echo '<h3>All files in the Uploads directory</h3>'; | |
for($i=2; $i<count($files); $i++) { | |
echo '<pre>'; | |
echo $files[$i]; | |
echo '</pre>'; | |
} | |
} | |
?> | |
<html> | |
<head> | |
<title>Audio Upload Demo</title> | |
</head> | |
<body> | |
<h2> Audio Upload Demo! </h2> | |
<form id="upload" name="upload" method="post" action="index.php" enctype="multipart/form-data"> | |
<input type="file" id="audio" name="audio[]" multiple="multiple" required="required" accept=".wav" tabindex="1" /><br /> | |
<input type="text" id="name" name="name" required="required" placeholder="Enter foldername" tabindex="2" /><br /> | |
<input type="submit" id="submit" name="submit" value="Submit" tabindex="2" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment