2012-05-27 34 views
1

我有點新手到PHP和編碼非常多。我使用純HTML和css創建了該網站www.newbornsounds.co.uk,但現在正在將會員功能與PHP集成。 我在上傳腳本時遇到問題,它不允許上傳音頻。我已經檢查過,php.ini允許上傳大小足夠大,並且經歷了我能想到的所有事情。選擇音頻文件時,只會回顯名稱,而不是大小或文件類型。順便說一句,我使用wamp作爲開發服務器,如果有任何意義。PHP mp3上傳不起作用。 JPEGS和大多數其他文件類型雖然

這裏是PHP腳本的PHP文件中的形式:

<?php 
// Connects to your Database 
include("phpscripts/database.php"); 


//checks cookies to make sure they are logged in 

{ 
$username = $_COOKIE['username']; 

$pass = $_COOKIE['password']; 

    $check = mysql_query("SELECT * FROM members WHERE username = '$username'")or die(mysql_error()); } 

while($info = mysql_fetch_array($check)) 

    { } 

    if ($pass != $info['password']) 


     { 

        } 



else 



{   

header("Location: login.html"); 

} 


//********************************************************************************************** 


echo "Please wait while we attempt to upload your file...<br><br>"; 

//********************************************************************************************** 


$target_path = "../uploads/"; 

$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't  upload. 

$filename = $_FILES['file']['name']; 
$filesize = $_FILES['file']['size']; 
$mimetype = $_FILES['file']['type']; 

$filename = htmlentities($filename); 
$filesize = htmlentities($filesize); 
$mimetype = htmlentities($mimetype); 

$target_path = $target_path . basename($filename); 

if($filename != ""){ 

echo "Beginning upload process for file named: ".$filename."<br>"; 
echo "Filesize: ".$filesize."<br>"; 
echo "Type: ".$mimetype."<br><br>"; 

} 

    //First generate a MD5 hash of what the new file name will be 
    //Force a MP3 extention on the file we are uploading 

$hashedfilename = md5($filename); 
$hashedfilename = $hashedfilename.".mp3"; 

//Check for empty file 
if($filename == ""){ 
$error = "No File Exists!"; 
$flag = $flag + 1; 

} 

//Now we check that the file doesn't already exist. 
$existname = "../uploads/".$hashedfilename; 

if(file_exists($existname)){ 

if($flag == 0){ 
$error = "Your file already exists on the server! 
Please choose another file to upload or rename the file on your 
computer and try uploading it again!"; 
} 

$flag = $flag + 1; 
} 

//Whitelisted files - Only allow files with MP3 extention onto server... 

$whitelist = array(".mp3"); 
foreach ($whitelist as $ending) { 

if(substr($filename, -(strlen($ending))) != $ending) { 
$error = "The file type or extention you are trying to upload is not allowed! 
You can only upload MP3 files to the server!"; 
$flag++; 
} 
} 


//Now we check the filesize. If it is too big or too small then we reject it 
//MP3 files should be at least 1MB and no more than 6.5 MB 

if($filesize > 8920600){ 
//File is too large 

if($flag == 0){ 
$error = "The file you are trying to upload is too large! 
Your file can be up to 8.5 MB in size only. 
Please upload a smaller MP3 file or encode your file with a lower bitrate."; 
} 

$flag = $flag + 1; 
} 

if($filesize < 104860){ 
//File is too small 

if($flag == 0){ 
$error = "The file you are trying to upload is too small! 
Your file has been marked as suspicious because our system has 
determined that it is too small to be a valid MP3 file. 
Valid MP3 files must be bigger than 1 MB and smaller than .5 MB."; 
} 

$flag = $flag + 1; 

} 

//Check the mimetype of the file 
if($mimetype != "audio/mp3" and $mimetype != "audio/mpeg"){ 

if($flag == 0){ 
$error = "The file you are trying to upload does not contain expected data. 
Are you sure that the file is an MP3?"; 
} 

$flag = $flag + 1; 
} 

//Check that the file really is an MP3 file by reading the first few characters of the file 
$f = @fopen($_FILES['uploadedfile']['tmp_name'],'r'); 
$s = @fread($f,3); 
@fclose($f); 
if($s != "ID3"){ 

if($flag == 0){ 
$error = "The file you are attempting to upload does not appear to be a valid MP3  file."; 
} 

$flag++; 
} 



//All checks are done, actually move the file... 

if($flag == 0){ 

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


//Change the filename to MD5 hash and FORCE a MP3 extention. 

if(@file_exists("uploads/".$filename)){ 

//Rename the file to an MD5 version 
rename("uploads/".$filename, "uploads/".$hashedfilename); 

echo "The file ". basename($filename). " 
    has been uploaded. Your file is <a href='uploads/$hashedfilename'>here</a>."; 

} 
else{ 
    echo "There was an error uploading the file, please try again!"; 
} 


} else{ 
echo "There was an error uploading the file, please try again!"; 
} 

} 
else { 
echo "File Upload Failed!<br>"; 
if($error != ""){ 
echo $error; 
} 
} 

?> 

<div id="uploadcontainer"> 
<script type="text/javascript"> 
    function performClick(node) { 
    var evt = document.createEvent("MouseEvents"); 
    evt.initEvent("click", true, false); 
    node.dispatchEvent(evt); 
    } 
</script> 
    <a href="#" onclick="performClick(document.getElementById('file'));"> 
    <div id="selectfile"> 
    <img src="images/upload.gif" width="100" height="96" alt="upload button" border="0">  <br><br> 
    <input type="button" id="uploader" value="Select a file and upload it" class="button" > 
    </div></a> 
     <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" id="form"  method="post" enctype="multipart/form-data"> 
    <input type="file" id="file" name="file" class="noshow"> 
    </form> 
    <script type="text/javascript"> 
     $(function(){ 
     $("#file").change(function(){ 
     $("#form").submit(); 
    }); 
    }); 
    </script> 
<br><p2>We support MP3, WAV, FLAC, AIFF and MP2 file formats</p2> 
    <br><br> 
    <p2><b>Problem uploading?</b> Get in touch via<a  href="mailto:[email protected]"><b> email </b></a>and we'll help sort things out.<br> 

重要:你必須擁有的權利,所有你上傳的曲目,你必須是一個無符號的藝術家。

回答

0

現在全部排序。原來,我不得不在.htaccess文件中添加一些代碼。花了我一段時間,但現在我們可以開始爲NBS提供新功能。

+0

提示:在$ _FILES中沒有檢查'['error']'參數是否實際成功/失敗 - 即使上傳失敗,文件名信息也可能存在。永遠不要假設上傳成功。 –

+0

說出你添加到.htaccess文件中的內容是有用的... –