简体   繁体   English

文件上传无效| 腓

[英]File upload not working | Php

I'm trying to upload a file in php and unable to do it. 我正在尝试上传php中的文件而无法执行此操作。 The file I'm trying to upload is a csv file, but it should not be a concern. 我正在尝试上传的文件是一个csv文件,但它不应该是一个问题。 I'm using php to upload my file. 我正在使用php上传我的文件。 I'm also trying to process the form in the same page. 我也试图在同一页面处理表单。 Below is my code for file upload and it is not working... 下面是我的文件上传代码,它无法正常工作......

<!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>";
    }
}

?>

use enctype="multipart/form-data" 使用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>

update your form code with enctype attribute 使用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>

I have try below code and it's work perfect. 我尝试下面的代码,它的工作完美。

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

Upload code in PHP[without checking its extension] 用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>

It should be something like this 它应该是这样的

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

Below are the changes you need to make 以下是您需要进行的更改

  • Add enctype = "multipart/form-data" in form tag as new attribute 在表单标记中添加enctype = "multipart/form-data"作为新属性
  • Change from this if(isset($_POST['csv_file'])) { to this if($_FILES['csv_file']['size'] > 0) { as you need to check size whether its uploaded or not 从这个改变if(isset($_POST['csv_file'])) {到这个if($_FILES['csv_file']['size'] > 0) {因为你需要检查大小是否上传
  • Change from this echo "<p>".$_POST['csv_file']." => file input successfull</p>"; 更改此echo "<p>".$_POST['csv_file']." => file input successfull</p>"; to this echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>"; 回到echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>"; as you need to use $_FILES to get file name instead of $_POST 因为你需要使用$_FILES来获取文件名而不是$_POST
  • Last but not the least, complete </body> tag if you have not yet. 最后但并非最不重要的,完整的</body>标签,如果你还没有。
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>";
    }
}

对于上传文件,技巧是enctype =“multipart / form-data”使用此表单定义

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

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