简体   繁体   English

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

[英]Unable to upload file on server using PHP

This code works fine,no any error but does not insert file in database and destination folder after hit upload button. 此代码可以正常工作,没有任何错误,但在单击“上载”按钮后不会在数据库和目标文件夹中插入文件。

HTML Form HTML表格

<html>
    <body>
        <form action="includes/parts-cat/zip-download.php" method="post" enctype="multipart/form-data" >
            <div class="col-md-3 col-sm-3">
                <label for="">Upload File:</label>
                <div class="input-group">
                    <input type="file" name="myfile">
                </div>
            </div>
            <div class="col-sm-2">
                <label></label>
                <input id="dngr" class="btn btn-danger" type="submit" name="save" style="margin-top:15px;" value="Upload">
            </div>
        </form>
    </body>
</html>

PHP File PHP文件

<?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.";
        }
    }
}
?>

I have searched all over and still unable to figure out why unable to upload files. 我搜索了所有内容,但仍然无法弄清为什么无法上传文件。 Please give me suggestion if any mistake or error in above code. 如果以上代码有任何错误或错误,请给我建议。

Use __DIR__ 使用__DIR__

just change 只是改变

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM