如何上传音频(MP3,OGG,FLAC)使用PHP? [英] How Can I Upload Audio(mp3,ogg,flac) By Using PHP?

查看:186
本文介绍了如何上传音频(MP3,OGG,FLAC)使用PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在这里

我这样做是之前,利用这个PHP code上传图像,但是当我尝试将其更改为上传的音频文件,它只是不能把它上传?

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

 < PHP
$允许=阵列(PNG,JPG,JPEG,GIF,SWF');
如果(使用isset($ _ FILES ['UPL'])及和放大器; $ _FILES ['UPL'] ['错误'] == 0){
$扩展= PATHINFO($ _ FILES ['UPL'] ['名'],PATHINFO_EXTENSION);
如果(!in_array(用strtolower($扩展),$允许)){
    回声{地位:错误};
    出口;
}
如果(move_uploaded_file($ _ FILES ['UPL'] ['tmp_name的值'],'艺术/'.$_ FILES ['UPL'] ['名'])){
    回声{地位:成功};
    出口;
}
}
回声{地位:错误};
出口;
}

PHP code:(上传音频[不起作用):

 < PHP
$允许=阵列(MP3,奥格','后手');
如果(使用isset($ _ FILES ['UPL'])及和放大器; $ _FILES ['UPL'] ['错误'] == 0){
$扩展= PATHINFO($ _ FILES ['UPL'] ['名'],PATHINFO_EXTENSION);
如果(!in_array(用strtolower($扩展),$允许)){
回声{地位:错误};
出口;
}
如果(move_uploaded_file($ _ FILES ['UPL'] ['tmp_name的值'],'上传/'.$_ FILES ['UPL'] ['名'])){
回声{地位:成功};
出口;
}
}
回声{地位:错误};
出口;
}
?>


解决方案

检查您的php.ini文件,并确保upload_max_filesize的就足够了。如果没有,请增加的upload_max_filesize值。然后尝试在code波纹管

 如果(使用isset($ _ POST ['提交']))
{
$ PATH =上传/; //文件到服务器内放置
$ valid_formats1 =阵列(MP3,奥格,后手); //文件扩展名列表被接受
如果(使用isset($ _ POST)和$ _ SERVER ['REQUEST_METHOD'] ==POST)
    {        $文件1 = $ _FILES ['文件1'] ['名']; //这个code输入文件名文件1
        $大小= $ _FILES ['文件1'] ['大小'];
                $的fileInfo = PATHINFO($文件1);
                $ EXT = $的fileInfo ['扩展'];                    如果(in_array($分机,$ valid_formats1))
                    {
                        $ actual_image_name = uniqid()$分机。;
                        $ TMP = $ _FILES ['文件1'] ['tmp_name的值'];
                        如果(move_uploaded_file($ TMP,$路径。$ actual_image_name))
                            {
                            //上传成功
                            }
                        其他
                            回声失败;
                    }其他{
                        回声文件支持不支持;
                    }    }
}

这code测试,所以我认为这将正常工作:)

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

解决方案

Check you php.ini file and make sure upload_max_filesize is enough. If not please increase the upload_max_filesize value. Then try the code bellow

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";
                    }

    }
}

This code is tested so I think it will work fine :)

这篇关于如何上传音频(MP3,OGG,FLAC)使用PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆