2014-03-06 52 views
0

我遇到了文件上傳腳本無法正常工作的問題。奇怪的是,它對我設置的圖像字段工作正常,但不適用於.mp3或類似文件的表單字段,並且我對每個字符都使用相同的腳本。這裏是我的代碼:文件上傳PHP腳本不能正常工作

$download = $_FILES['download']['name']; 
$downloadtarget = "events/" . $name . "/"; 

if(move_uploaded_file($download, $downloadtarget)) { 
echo 'do stuff'; 
}else{ 
echo 'don't do stuff'; 

我總是看到後者「不做東西」的文件上傳。 除了互換同一個腳本:

$download = $_FILES['download']['name']; 

$download = $_FILES['picture']['name']; 

早些時候腳本正常工作,文件上傳沒有問題。

+1

mp3文件的大小是多少? –

+1

發佈此類型 –

+0

我認爲應該是'$ download = $ _FILES ['download'] ['' tmp_name'];' – bansi

回答

0

你缺少$_FILES['download']['tmp_name']; 嘗試

move_uploaded_file($_FILES["download"]["tmp_name"], 
      "events/" . $_FILES["download"]["name"]); 

也形成具有enctype="multipart/form-data"財產

有關文件上傳的後續環節的詳細信息: - http://www.w3schools.com/php/php_file_upload.asp

+0

試過這個,同樣的問題依然存在。另外,mp3的文件大小迄今從未超過5MB,但是我的主機對此沒有任何限制,並且腳本中沒有限制文件大小甚至是類型的語句。此外,表格肯定有entype =「multipart/form-data」。 – Dustin

+0

所以你有什麼錯誤?還檢查你的上傳目錄是否可寫 –

+0

@RakeshSharma一些好的程序員說http://www.w3fools.com/。所以請避免使用它。 :) –

0
<?php 
    $download = $_FILES['download']['name']; 
    $downloadtarget = "events/" . $download . ""; 
    $temp_name = $_FILES['download']['tmp_name']; 

    if(move_uploaded_file($temp_name, $downloadtarget)) { 
     echo 'do stuff'; 
    } else { 
     echo 'don\'t do stuff'; 
    } 
?> 
0

嘗試這樣的代碼

if ($_FILES["file"]["error"] == 4) 
      { 
       $file_attachement_message="No Files attached"; 
      } 
      else if ($_FILES["file"]["error"] > 0){ 

       $attachement_ 

error=$_FILES["file"]["error"]; 
      $file_attachement_message="File attachement failed with error code:$attachement_error"; 
     } 
     else 
     { 

      if(!is_dir('../attachements')) 
      { 
       mkdir('../attachements'); 
      } 
      if (file_exists("../attachements/".$id)) 
      { 
       // echo $_FILES["file"]["name"] . " already exists"; 
       $file_attachement_message="Attached file already exists in db"; 
      } 
      else 
      { 
       $info=pathinfo($_FILES['file']['name']); 
       $ext = $info['extension']; // get the extension of the file 
       $newname="$risk_id.".$ext; 
       ["tmp_name"],"../attachements/".$_FILES["file"]["name"]); 
       move_uploaded_file($_FILES["file"]["tmp_name"],"../attachements/".$newname); 

       $file_attachement_message="and File Attached successfully"; 
      } 
     } 
+0

請在發佈回答之前檢查語法錯誤,其中有太多。 – bansi

0

問題似乎是文件大小。經過測試與圖片與MP3,沒有問題的作品。進一步閱讀顯示php.ini默認爲2mb限制。