无法使用PHP在服务器上上传文件

时间:2019-06-08 14:28:47

标签: php html mysql

此代码可以正常工作,没有任何错误,但在单击上载按钮后不会在数据库和目标文件夹中插入文件。

HTML表单

<?php
include_once('../dbconfig.php');
$sql = "SELECT * FROM fileupload";
$result = mysqli_query($conn, $sql);
$files = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Uploads files
if (isset($_POST['save'])) { // if save button on the form is clicked
    // name of the uploaded file
    $filename = $_FILES['myfile']['name'];
    $destination = 'projectdocument/' . $filename;
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $file = $_FILES['myfile']['tmp_name'];
    $size = $_FILES['myfile']['size'];
    if (!in_array($extension, ['zip', 'pdf', 'docx', 'xlsx'])) {
        echo "You file extension must be .zip, .pdf or .docx";
    } elseif ($_FILES['myfile']['size'] > 10000000000) {
        echo "File too large!";
    } else {
        if (move_uploaded_file($filename, $destination)) {
            $mysql = "INSERT INTO fileupload (name,size, downloads) VALUES ('$filename', $size, 0)";
            if (mysqli_query($conn, $mysql)) {
                echo "File uploaded successfully";
            }
        } else {
            echo "Failed to upload file.";
        }
    }
}
?>

PHP文件

UPDATE guest_group
    SET create_time_holder = arrival_date::date + interval '12 hour';

我已经搜索了所有内容,但仍然无法弄清为什么无法上传文件。 如果上面的代码有任何错误或错误,请给我建议。

1 个答案:

答案 0 :(得分:1)

使用__DIR__

只是改变

if (move_uploaded_file($_FILES['myfile']['tmp_name'], __DIR__.'//projectdocument/'. $_FILES["myfile"]['name'])) {