2016-09-26 142 views
0

我一直在關注文件上傳到我的服務器的教程。該腳本似乎有效,但是當我查看上傳目錄時,沒有任何內容。PHP文件上傳腳本失敗

<?php 

$file_result=""; 

if ($_FILES["file"]["error"] > 0) 
{ 
$file_result .= "No file uploaded or invalid file."; 
$file_result .= "Error code: ". $_FILES["file"]["error"]. "<br>"; 
} else { 

$file_result .= 
    "Upload: " . $_FILES["file"]["name"] . "<br>" . 
    "Type: " . $_FILES["file"]["type"] . "<br>" . 
    "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br>" ; 
    "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 

    move_uploaded_file($_FILES["file"]["tmp_name"], 
    "full/path/on/server" . $_FILES["file"]["name"]); //Actual address removed 

    $file_result .= "File Upload Successful!"; 
} echo $file_result; 

?>` 

的HTML看起來像這樣:`

<form enctype="multipart/form-data" action="upload_file.php" method="post"> 
Browse Files:<br> 
    <input name="file" type="file" id="file" size="80"><br> 
    <input type="submit" id="u_button" name="u_button" Value = "Upload the File"> 
</form> 

`

+0

打開錯誤報告,並開始解決這些錯誤,開心調試。目標文件夾有 – Epodax

+0

有寫入權限嗎? – AkshayP

+0

你還沒有使用enctype。請檢查表格爲

.... enctype – madankundu

回答

0

使用下面的代碼上傳:

if (Isset($_FILES["yourfilename"]["name"])) { 
    $uploadOk = 1; 
    $target_dir = "Upload/"; 
    --path 
    $tempname = basename($_FILES["yourfilename"]["name"]); 
    $target_file = $target_dir.basename($_FILES["yourfilename"]["name"]); 
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 
    // Check if image file is a actual image or fake image 
    if ($tempname != "") { 
     if ($_FILES["yourfilename"]["size"] > 20000000) { 
      echo "Sorry, Maximum 20 mg.<br>"; 
      $uploadOk = 0; 
     } 
     if ($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "txt") { 
      echo "Soory, Doc-Docx-Txt Only Format Sopport.<br>"; 
      $uploadOk = 0; 
     } 

     if (move_uploaded_file($_FILES["yourfilename"]["tmp_name"], $target_file); 
     } 
    } 
+0

***解釋***發生了什麼變化。 –