2015-12-23 64 views
2

我被困在這裏如何使用PHP上傳音頻(mp3,ogg,flac)?

我這樣做是通過使用這個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代碼:(上傳音頻[不起作用):

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

什麼是你的文件,你要上傳的擴展?並給我們你給錯誤 –

+0

我試着讓用戶上傳一些3種類型的音頻文件(mp3,ogg,flac),然後我把它放到「上傳」文件夾中,用戶可以查看它在「all.html」 –

+0

上,但是當您上傳文件時,會顯示何種類型的錯誤? –

回答

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

但確保您使用的表單是正確的,以支持此代碼asp包括以下內容

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

也好心的把'accept ='mp3','ogg', 'flac''所以輸入只接受給定的文件類型 –

+0

這兩個代碼不工作,也是我在PHP新,我會把所有的音樂在一個文件夾中調用「上傳」 –

0

使用此代碼,請確保您有該文件夾服務器上

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

更改' $ path =「test/music /」;'to'$ path =「uploads /」;' –

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

    } 
} 

代碼此代碼進行測試,所以我認爲它會正常工作:)

+0

也許是我的HTML代碼出錯了,我現在會發布HTML代碼 –

+0

檢查php.ini並將其替換爲'upload_max_filesize = 500M' –