如何使用PHP上传音频文件?

时间:2013-11-29 10:00:12

标签: php html html5

我想使用html和php上传音频文件。它总是返回错误信息(无效文件)。请帮助我。 我跟着这个网址 - https://forums.digitalpoint.com/threads/mp3-file-upload-in-php.1174500/;

http://p2p.wrox.com/php-how/53040-how-upload-mp3-file-using-php.html

这是我正在使用的代码 -

<form enctype="multipart/form-data" action="sound_action.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE"/>
    Choose a file to upload: <input name="file" type="file" /><br />
    <input type="submit" value="Upload File" />
</form>

<?php
    if ((($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/mp4")
    || ($_FILES["file"]["type"] == "audio/wav"))
    && ($_FILES["file"]["size"] < 1000000))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
            echo "Type: " . $_FILES["file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

            if (file_exists("upload/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }
        }
    }
    else
    {
        echo "Invalid file";
    }
?>

3 个答案:

答案 0 :(得分:1)

对于mp4,mime类型的mp3为audio/mpeg,您必须使用video/mp4并且wave为audio/x-wavaudio/wav

此外,您应该增加文件大小,因为此参数以字节为单位,1000000小于1mb。也许你还要在php.ini

中增加上传文件的大小

答案 1 :(得分:0)

有一种验证文件的替代方法,但它是检查文件验证的基本方法,不确定文件是否包含正确的类型。

$valid_extension = array('.mp3', '.mp4', '.wav');
$file_extension = strtolower( strrchr( $_FILES["file"]["name"], "." ) );

if( in_array( $file_extension, $valid_extension ) && 
    $_FILES["file"]["size"] < 1000000 ){
    // Rest Logic Here
}
else
{
    print_r( $_FILES );
}

注意:

这是一个基本的文件验证。

答案 2 :(得分:0)

您缺少if子句:

SELECT Projects.SpCall,Projects.CustName,Projects.CustNumber
,Projects.ReceiveDate,Projects.StartDate,Projects.ProjectType
,Calculator.AN,Projects.Professional,Projects.PmUserName
,Projects.AcountManager,Projects.CrmCallNum,Projects.ProjectCategory
,Projects.CallNum,Projects.ContactName,Projects.ContactPhone
,Projects.ContactEmail,Projects.HiddenNote,Projects.RowColor
, Projects.HeaderCellText,
 SUM(Calculator.K + Calculator.L + Calculator.M + Calculator.N + Calculator.AD + Calculator.AR) AS sumofTotal
 ,partialBilling.Ammount FROM Projects LEFT JOIN Calculator ON Projects.SpCall=Calculator.AQ
  LEFT JOIN partialBilling ON Projects.SpCall = partialBilling.spCall
   WHERE PmUserName= 'JOHN DOE'AND OpertionalStatus
    <> 'Billed' AND OpertionalStatus<> 'Finished' AND
     OpertionalStatus<> 'Passed To Billing' AND OpertionalStatus<> 'Scanning' 
     AND OpertionalStatus<> 'Ended' 
     AND OpertionalStatus<> 'Green Billing' 
     AND (GeneralStatus= 'Passed To Project Manager' 
     OR GeneralStatus= 'Astrategic Project') 
     GROUP BY Projects.SpCall,Projects.CustName,Projects.CustNumber
     ,Projects.ReceiveDate,Projects.StartDate,Projects.ProjectType
     ,Calculator.AN,Projects.Professional,Projects.PmUserName
     ,Projects.AcountManager,Projects.CrmCallNum,Projects.ProjectCategory
     ,Projects.CallNum,Projects.ContactName,Projects.ContactPhone
     ,Projects.ContactEmail,Projects.HiddenNote,Projects.RowColor
     , Projects.HeaderCellText,partialBilling.Ammount;