2014-01-22 70 views
1

我有一個表單將用戶輸入發送到MySQL數據庫。它也應該從文件上傳文件發送到我的服務器上的目錄。除了文件上傳部分,其中的一切都運行良好。我是一個PHP新手,所以我顯然只是在我的PHP代碼中失敗。任何意見將不勝感激。PHP move_uploaded_file失敗

questCreate.php:

<form name="quest" action="questSave.php" method="post"> 

<h1> Create a Quest </h1> 

<!!!---QUEST BASICS---!!!> 

<table> 
    <tr> 
     <td align="center"><b>Quest Basics</b></td> 
    </tr> 
    <tr> 
     <td align="right">Quest:</td> 
     <td> 
     <input type="text" name="questName" size="30" placeholder="Quest Name" onfocus="this.placeholder = ''" onblur="this.placeholder='Quest Name'"> 
     </td> 
    </tr> 
    <tr> 
     <td align="right">Creator:</td> 
     <td> 
     <input type="text" name="creator" size="30" placeholder="Player Name" onfocus="this.placeholder = ''" onblur="this.placeholder='Player Name'"> 
     </td> 
    </tr> 
    <tr> 
     <td align="right">Campaign Setting:</td> 
     <td> 
     <select name="setting" size="1" onchange="Set_Setting()"> 
      <option> D&D Generic Setting</option> 
      <option> Eberron</option> 
      <option> Forgotten Realms</option> 
      <option> Greyhawk</option> 
      <option> Ravenloft</option> 
     </select> 
     </td> 
    </tr> 
    <tr> 
     <td align="right"> Map upload:</td> 
     <td> 
      <input type="file" id="map" name="map"> 
     </td> 
    </tr> 

<tr> 
<td><br></td> 
</tr> 

<!!!---PLAYER ALLOTTMENTS---!!!> 

<tr> 
    <td align="center"><b>Player Allottments</b></td> 
</tr> 
<tr> 
    <td align="right">Total Quest Experience:</td> 
    <td> 
     <input type="number" name="experience" size="30" placeholder="100"> 
    </td> 
</tr> 
<tr> 
    <td align="right">Items/Weapons:</td> 
    <td> 
     <textarea name="items_weapons" cols="30" rows="5" placeholder="Earnable Items and Weapons" onfocus="this.placeholder = ''" onblur="this.placeholder='Earnable Items and Weapons'" style="resize: none;"></textarea> 
    </td> 
</tr> 
<tr> 
    <td align="right">Save Quest:</td> 
    <td> 
     <input type="submit"> 
    </td> 
</tr> 

</form> 
</table> 
</center> 

questSave.php:

<?php 
$con=mysqli_connect("localhost","username","password","db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql="INSERT INTO quests (questName, creator, setting, experience, items_weapons) 
VALUES ('$_POST[questName]','$_POST[creator]','$_POST[setting]','$_POST[experience]','$_POST[items_weapons]')"; 

$target_path = "maps/"; 

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

if(move_uploaded_file($_FILES['map']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 

if (!mysqli_query($con,$sql)) 
    { 
    die('Error: ' . mysqli_error($con)); 
    } 

header("Location: questList.php"); 
die(); 

mysqli_close($con); 
?> 
+0

什麼是顯示錯誤消息? –

+0

沒有錯誤消息,它只是沒有將文件保存到/ maps目錄中。 – Aldentec

+0

我希望該數據庫的用戶名和密碼不是真實的:/只是fyi – Sephedo

回答

2
<form name="quest" action="questSave.php" method="post" enctype="multipart/form-data"> 

在上傳文件form必須有這個屬性enctype="multipart/form-data"

+0

這將是原因 – Sephedo

+0

這樣做了!這簡直太可笑了...... – Aldentec

0

確保該文件夾您將文件移動到具有讀/寫(777),它的權限。 並嘗試改變文件路徑:

$target_path = "./maps/";