views:

154

answers:

2

I have a problem when moving an uploaded file into a local directory.

When running the following code, the output is always "error uploading file". It seems to always not meet the condition for the 'move_uploaded_media' function and therefore $result is not being set?

Are there any glaring mistakes?

<?php

$page_title = 'Admin | Multimedia Portfolio';

include('includes/admin_header.html');

if(isset($_POST['submitted']))
{
    $uploadDir = 'files/';

    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $filePath = $uploadDir . $fileName;

    $result = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
        echo "Error uploading file"; // Here is were the it always gets caught
        exit;
    }

    require_once('mysql_connect.php');

    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
        $filePath = addslashes($filePath);
    }

    $query = "INSERT INTO files (name, size, type, path ) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

    mysqli_query($dbc, $query) or die('Error, query failed : ' . mysql_error());

    mysqli_close($dbc);

    echo "<br>Files uploaded<br>";

    }

?>

<div id="content-wrap">

<h1>Upload Media</h1>

<div id="content">

    <form method="post" action="upload.php" encytype="multipart/form-data">

    <fieldset>

        <div class="entry">

                <label>Which media <span class="highlight">file</span> would you like to upload?</label>
                <input type="file" name="userfile" id="userfile" size="30" />

            </div>

                <fieldset id="button">
                <input type="submit" value="Register" />
                <input type="hidden" name="submitted" value="TRUE" />
            </fieldset>

        </fieldset>

    </form>

</div>

</div>

<?php

include('includes/admin_footer.html');

?>
+5  A: 

Not sure if there is more, but you have encytype instead of enctype in your <form>.

kemp
Fantastic! Thank you so much, I have been staring at the PHP part of this for hours, what a plonker. Thanks again for the very very quick answer.
Ronnie
A: 

you might also want to perform an 'is_uploaded_file()' check on the temporary file, just to be sure...

HorusKol