How to upload audio file in php? :-
In this post you will learn how to upload audio files like: mp3, mp4, etc. using HTML form in PHP. Firstly you have to create simple HTML form.
HTML Code
<input type=“file” name=“audioFile” accept=“audio/*”>
<input type=“submit” name=“Submit” value=“Submit”>
</form>
PHP Code
Using this code you can upload audio – mp3, mp4, etc files. In this code you change the extension of the files which you want to upload and can change the directory path as your requirement.
$streplaceFileName = cleanSpecialCharacters($_FILES[‘audioFile’][‘name’]);
$audioFile = $rand.”-“.$streplaceFileName;$ds = DIRECTORY_SEPARATOR;
$storeFolder = ‘uploads/audio’;if((!empty($_FILES)) && !empty($_FILES[‘audioFile’][‘name’])) {
if(preg_match(‘/[.](mp3)|(mp4)$/’, $_FILES[‘audioFile’][‘name’])) {$filename = $rand . “-” . $streplaceFileName;
$tempFile = $_FILES[‘audioFile’][‘tmp_name’];
$targetPath = $storeFolder . $ds;
$targetFile = $targetPath.$filename;
$check = move_uploaded_file($tempFile,$targetFile);
if($check) echo “File Uploaded Successfully!”;
}
}
function cleanSpecialCharacters($string) {
$string = str_replace(‘ ‘, ‘-‘, $string); // Replaces all spaces with hyphens.
return preg_replace(‘/[^A-Za-z0-9.-]/’, ”, $string); // Removes special chars.
}
Leave a comment