PHP文件上传,文件不会移动到本地目录

时间:2010-02-08 22:29:32

标签: php upload file-upload

将上传的文件移动到本地目录时出现问题。

运行以下代码时,输​​出始终为“错误上载文件”。它似乎总是不符合'move_uploaded_media'函数的条件,因此$ result没有被设置?

有没有明显的错误?

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

?>

2 个答案:

答案 0 :(得分:5)

不确定是否还有更多,但encytype中有enctype而不是<form>

答案 1 :(得分:1)

您可能还想对临时文件执行is_uploaded_file()检查,以确保......