2012-03-13 33 views
0

我製作了一個用於上傳歌曲的API,但它造成了問題。當我通過Android設備上傳歌曲時,它會上傳到Ubuntu上的Songs文件夾中,但這首歌曲不能播放。請給我解決方案?通過PHP上傳的歌曲

我的代碼是這樣的:

<?php 

include('connect.php'); 
header("Content-type: text/xml"); 

$target_path = "./Songs/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{ 

    echo "The file ".'"'. basename($_FILES['uploadedfile']['name']) .'"'." has been uploaded"; 

    chmod ("Songs/".basename($_FILES['uploadedfile']['name']), 0644); 
} 
else 
{ 
    echo "There was an error uploading the file, please try again!"; 

    echo "filename: " . basename($_FILES['uploadedfile']['name']); 

    echo "target_path: " .$target_path; 
} 

?> 
+0

當你說'不玩'時,你的意思是在服務器上還是流式傳輸到設備上,比如Android設備?上傳成功了嗎? – halfer 2012-10-08 06:33:30

回答

3

我做了它和它的正常工作。

<?php 

$target_path = "./Songs/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

//Get the uploaded file information 

$name_of_uploaded_file =basename($_FILES['uploadedfile']['name']); 

//get the size of the file 

$size_of_uploaded_file = $_FILES["uploadedfile"]["size"]/1024;//size in KBs 

//get the file extension of the file 

$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); 

//echo $size_of_uploaded_file; 

$max_allowed_file_size = 10000; // size in KB 

//Validations 

if($size_of_uploaded_file>$max_allowed_file_size) 
{ 

    $errors .= "\n Size of file should be less than $max_allowed_file_size"; 

    echo $errors; 

    exit(); 

} 

$allowed_extensions = array("mp3"); 

//------ Validate the file extension ----- 

$allowed_ext = false; 

for($i=0; $i < sizeof($allowed_extensions); $i++) 
{ 

    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) 
    { 

    $allowed_ext = true; 

    } 

} 

if(!$allowed_ext) 
{ 

    $errors ="The uploaded file is not supported file type"); 

    echo 'not a song. Error = '.$errors; 

    exit(); 

} 
    $t_remaining = $time_remaining-$duration_of_song ; 

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
    { 

     // perform any operation 

    echo "successfully uploaded"; 

     chmod ("Songs/". basename($_FILES['uploadedfile']['name']), 0777); 

} 
else 
{ 

    echo "error in uploading"; 

} 
+0

此代碼工作正常,並在服務器上上傳歌曲。而當我試圖播放這首歌曲後,在去所有歌曲存儲的特定文件夾的位置播放它時,它會播放很好,這意味着它會很好地上傳。 – chandan 2012-10-12 04:30:58