Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Upload Audio(mp3,ogg,flac) By Using PHP?

i'm stuck in here

I do this be before,and by using this PHP code to upload image,but when i try to change it to uploading audio file,it just can't upload it?

My PHP code:(Upload Image[Work]):

<?php
$allowed = array('png', 'jpg', 'jpeg', 'gif', 'swf');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
    echo '{"status":"error"}';
    exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'arts/'.$_FILES['upl']['name'])){
    echo '{"status":"success"}';
    exit;
}
}
echo '{"status":"error"}';
exit;
}

PHP Code:(Upload Audio[Not Work]):

<?php
$allowed = array('mp3', 'ogg', 'flac');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error"}';
exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'upload/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
}
?>
like image 556
Felix Fong Avatar asked Dec 04 '25 22:12

Felix Fong


1 Answers

use this code make sure you have the folder on your server

<?php
if(isset($_POST['submit']))
    {

$path = "test/music/"; //file to place within the server
$valid_formats1 = array("mp3", "ogg", "flac"); //list of file extention to be accepted
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
        $file1 = $_FILES['file1']['name']; //input file name in this code is file1
        $size = $_FILES['file1']['size'];

        if(strlen($file1))
            {
                list($txt, $ext) = explode(".", $file1);
                if(in_array($ext,$valid_formats1))
                {
                        $actual_image_name = $txt.".".$ext;
                        $tmp = $_FILES['file1']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                            //success upload
                            }
                        else
                            echo "failed";              
                    }
        }
    }
}
?>
<form enctype="multipart/form-data" id="form1" method="post" action="text1.php">
<input type="file" name="file1" accept=".ogg,.flac,.mp3" required="required"/>
<input type="submit" name="submit"/>
</form>
like image 113
Jeffrey Arzadon Cabang Avatar answered Dec 06 '25 12:12

Jeffrey Arzadon Cabang