如何使用PHP上传音频(mp3,ogg,flac)?

时间:2015-12-23 05:40:41

标签: php image audio upload

我被困在这里

我以前这样做,并且通过使用这个PHP代码上传图片,但是当我尝试将其更改为上传音频文件时,它只是无法上传它?

我的PHP代码:(上传图片[工作]):

<?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代码:(上传音频[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;
}
?>

3 个答案:

答案 0 :(得分:1)

使用此代码确保您的服务器上有该文件夹

<?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>

答案 1 :(得分:0)

尝试使用

$path = "../test/cover/"; //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($name2))
                {
                    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";              
                        }
            }
        }

但请确保您使用的表单是正确的,以支持此代码,包括以下内容

 enctype="multipart/form-data" method="post"

答案 2 :(得分:0)

检查你的php.ini文件并确保upload_max_filesize足够了。如果没有,请增加upload_max_filesize值。然后尝试下面的代码

if(isset($_POST['submit']))
{      
$path = "uploads/"; //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'];


                $fileInfo=pathinfo($file1);
                $ext=$fileInfo['extension'];

                    if(in_array($ext,$valid_formats1))
                    {
                        $actual_image_name = uniqid().".".$ext;
                        $tmp = $_FILES['file1']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                            //success upload
                            }
                        else
                            echo "failed";              
                    }else{
                        echo "File Support Not Support";
                    }

    }
}

此代码经过测试,因此我认为它可以正常工作:)