文件上传不起作用 | php

Abh*_*deo 5 php forms file

我正在尝试在 php 中上传文件但无法执行。我尝试上传的文件是一个 csv 文件,但这应该不是问题。我正在使用 php 上传我的文件。我也在尝试在同一页面中处理表单。以下是我的文件上传代码,但它不起作用...

<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="file" name="csv_file">
    <input type="submit" name="submit">
</form>


<?php   
    if(isset($_POST['submit'])) {
        if(isset($_POST['csv_file'])) {             
            echo "<p>".$_POST['csv_file']." => file input successfull</p>";
            fileUpload();
        }
    }   

function fileUpload () {
    $target_dir = "var/import/";
    $file_name = $_FILES['csv_file']['name'];
    $file_tmp = $_FILES['csv_file']['tmp_name'];

    if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
        echo "<h1>File Upload Success</h1>";            
    }
    else {
        echo "<h1>File Upload not successfull</h1>";
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

Nik*_*ela 2

我尝试过下面的代码,效果很完美。

<!DOCTYPE html>
<html>
    <head>
        <title>File Upload</title>
    </head>
    <body>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
            <input type="file" name="csv_file">
            <input type="submit" name="submit">
        </form>


        <?php
        if (isset($_POST['submit'])) {
            echo "<p>" . $_POST['csv_file'] . " => file input successfull</p>";
            $target_dir = "images ";
            $file_name = $_FILES['csv_file']['name'];
            $file_tmp = $_FILES['csv_file']['tmp_name'];

            if (move_uploaded_file($file_tmp, $target_dir . $file_name)) {
                echo "<h1>File Upload Success</h1>";
            } else {
                echo "<h1>File Upload not successfull</h1>";
            }
        }
        ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)