File upload not working | Php
Asked Answered
A

8

6

I'm trying to upload a file in php and unable to do it. The file I'm trying to upload is a csv file, but it should not be a concern. I'm using php to upload my file. 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>";
    }
}

?>

Agueweed answered 22/8, 2016 at 6:39 Comment(6)
enctype = "multipat/form-data" add it to <form>Sulphurate
it should be this - <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype = "multipart/form-data">Ligon
Its multipart not multipatDresden
As soon as I use enctype="multipart/form-data", nothing works at all the page simply refreshes. But, when I'm not using enctype="multipart/form-data", at least it prints File Upload not successfull.Agueweed
@AbhishekDhanrajShahdeo can you please check the codes given to you. check every code and let us know whose code works for you?Sulphurate
I have tried all the codes and found that actual problem in my code was using $_POST for files while I should have been using $_FILES. All the codes mentioned below are working as I have tried them seperately. Now, I have found the actual problem and fix, but thanks to all for their answers.Agueweed
Y
2

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>
Yankeeism answered 22/8, 2016 at 6:50 Comment(2)
if without file selection user hit the submit button then the above code will failsSulphurate
add if condition file is exist or not.Yankeeism
E
4

update your form code with enctype attribute

<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>
Electromagnetism answered 22/8, 2016 at 6:41 Comment(0)
S
3

use 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>
Scrounge answered 22/8, 2016 at 6:41 Comment(0)
Y
2

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>
Yankeeism answered 22/8, 2016 at 6:50 Comment(2)
if without file selection user hit the submit button then the above code will failsSulphurate
add if condition file is exist or not.Yankeeism
L
2

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
  • 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
  • Change from this echo "<p>".$_POST['csv_file']." => file input successfull</p>"; to this echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>"; as you need to use $_FILES to get file name instead of $_POST
  • Last but not the least, complete </body> tag if you have not yet.
Ligon answered 22/8, 2016 at 6:56 Comment(0)
B
1

Upload code in PHP[without checking its extension]

<?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>
Bronson answered 22/8, 2016 at 6:43 Comment(0)
C
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>";
    }
}
Chu answered 22/8, 2016 at 7:54 Comment(0)
B
0

for uploading files, the trick is enctype="multipart/form-data" use this form definition

Berenice answered 14/7, 2018 at 10:56 Comment(0)
F
0

If your code is perfect and still facing that problem, check whether file_uploads in php configuration file (php.ini) is "ON".

Hope your problem is gone now.

Fley answered 12/4 at 17:48 Comment(3)
Did you read the last comment? They said they were using $_POST instead of $_FILES.Cenogenesis
Did you realize you were responding to an 8-year-old question?Cenogenesis
And it already had an accepted answer.Cenogenesis

© 2022 - 2024 — McMap. All rights reserved.