doriansm
doriansm

Reputation: 245

PHP move_uploaded_file failing

I have a form that is sending user input to a MySQL database. It should also be sending a file from a file upload to a directory on my server. Everything in it is working good, other than the file upload part. I am a PHP newbie, so I am obviously just failing in my PHP code. Any advice would be much appreciated.

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

Upvotes: 1

Views: 61

Answers (2)

Bugaloo
Bugaloo

Reputation: 1691

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

When uploading a file form must have this attribute enctype="multipart/form-data"

Upvotes: 2

Alex
Alex

Reputation: 132

Make sure the folder you're moving the files into has read/write (777) permissions on it. and try changing the file path to:

$target_path = "./maps/";

Upvotes: 0

Related Questions