无法上传.pdf文件和.doc来保留

时间:2014-01-31 22:05:07

标签: php html

这就是我应该如何上传pdf文件和。 doc到服务器和网站。

问题是这样它只是出现在数据库中并写入“数组”,我写了var_dump来发现它失败了,它说:

array(5) { ["name"]=> string(15) "hello.docx" ["type"]=> string(71) "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ["tmp_name"]=> string(14) "/tmp/phpX85fgs" ["error"]=> int(0) ["size"]=> int(2421) }

我必须在 opgaveFile

中使用它

PHP

move_uploaded_file("/opgaveFile/" . $_FILES["file"]);
    var_dump($_FILES["file"]);
    /*
    if ($stmt = $this->mysqli->prepare('INSERT INTO dwopgaver (title, filenavn) VALUES (?, ?)')) { 
        $stmt->bind_param('ss', $title, $filenavn);
        $title = $_POST["title"]; 
        $filenavn = $_FILES["file"];
        $stmt->execute();

        $stmt->close();

    } else {
        echo 'Der opstod en fejl i erklæringen: ' . $this->mysqli->error;
    }
    */

HTML

<form action="#" enctype="multipart/form-data" method="post">
                <table width="90%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>Title</td>
                        <td><input type="text" name="title" maxlength="50"></td>
                    </tr>
                    <tr>
                        <td>Filer</td>
                        <td><input type="file" name="file"></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" name="opret" value="Upload Opgave" class="click border"></td>
                    </tr>
                </table>
            </form>

1 个答案:

答案 0 :(得分:0)

var_dump可以看出,$_FILES["file"]是一个数组。您需要提取它的特定元素以获得适当的属性。

move_uploaded_file($_FILES["file"]["tmp_name"], "/opgaveFile/" . $_FILES["file"]["name"]);
var_dump($_FILES["file"]);

if ($stmt = $this->mysqli->prepare('INSERT INTO dwopgaver (title, filenavn) VALUES (?, ?)')) { 
    $stmt->bind_param('ss', $title, $filenavn);
    $title = $_POST["title"]; 
    $filenavn = $_FILES["file"]["name"];
    $stmt->execute();

    $stmt->close();

} else {
    echo 'Der opstod en fejl i erklæringen: ' . $this->mysqli->error;
}