PHP move_uploaded_file失败

时间:2014-01-22 23:15:07

标签: php html

我有一个将用户输入发送到MySQL数据库的表单。它还应该将文件从文件上传发送到我服务器上的目录。除了文件上传部分之外,其中的所有内容都运行良好。我是一个PHP新手,所以我显然只是在我的PHP代码中失败了。任何建议将不胜感激。

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);
?>

2 个答案:

答案 0 :(得分:2)

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

上传文件form时,必须具有此属性enctype="multipart/form-data"

答案 1 :(得分:0)

确保您要移动文件的文件夹具有读/写(777)权限。 并尝试将文件路径更改为:

$target_path = "./maps/";