文件上传不起作用 | php

问题描述 投票:0回答:8

我正在尝试在 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>";
    }
}

?>

php forms file
8个回答
4
投票

使用 enctype 属性更新表单代码

<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>

3
投票

使用 enctype="multipart/form-data"

<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>

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>

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'])) {

            if ($_FILES['csv_file']['size'] > 0)
            {
                echo "<p>".$_FILES['csv_file']['name']." => 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>";
        }
    }

    ?>
</body>

以下是您需要进行的更改

  • 在表单标签中添加
    enctype = "multipart/form-data"
    作为新属性
  • 从这个
    if(isset($_POST['csv_file'])) {
    更改为这个
    if($_FILES['csv_file']['size'] > 0)  {
    ,因为您需要检查尺寸 是否上传
  • echo "<p>".$_POST['csv_file']." => file input
    successfull</p>";
    更改为
    echo "<p>".$_FILES['csv_file']['name']."
    => file input successfull</p>";
    ,因为您需要使用
    $_FILES
    来获取文件名而不是
    $_POST
  • 最后但并非最不重要的一点是,如果您还没有完成
    </body>
    标签,请完成。

1
投票

在 PHP 中上传代码[不检查其扩展名]

<?php
if(isset($_POST['save'])){
$path="var/import/";
$name = $_FILES['csv_file']['name'];//Name of the File
$temp = $_FILES['csv_file']['tmp_name'];
if(move_uploaded_file($temp, $path . $name)){
    echo "success";
}else{
    echo "failed";
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="save" value="submit">
</form>

1
投票
First Give permission to folder where you going to upload Ex: "var/import/" folder.

<!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'])) {
        if(isset($_FILES['csv_file']) && $_FILES['csv_file']['size'] > 0) {             
            echo "<p>".$_FILES['csv_file']['name']." => 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>";
    }
}

0
投票

上传文件的技巧是 enctype="multipart/form-data" 使用此表单定义


0
投票

如果你的代码是完美的,但仍然面临这个问题,请检查php配置文件(php.ini)中的file_uploads是否为“ON”。

希望您的问题现在已经消失了。

© www.soinside.com 2019 - 2024. All rights reserved.