Cant view the .csv file on load

I am trying to load a field into mysql database. the field can be uploaded by the user by typing it manually or by uploading a CSV file containing this field. manual text input works well, but when I try to load using .csv it doesn't see the file and doesn't go to the "else" segment of my code. this is my code:

<?php
    $classname = $_POST['classname'];
    $classname = stripslashes($classname);
    $classname = mysql_real_escape_string($classname);


    $save = $_POST['save'];
    $save = stripslashes($save);
    $save = mysql_real_escape_string($save);


    $myfile = $_POST['myfile'];
    $myfile = stripslashes($myfile);
    $myfile = mysql_real_escape_string($myfile);


    $import = $_POST['import'];
    $import = stripslashes($import);
    $import = mysql_real_escape_string($import);

    if($classname && $save)
    {
        $choose_db;
        $sql="INSERT INTO classes (`class`) VALUES ('$classname')";
        $res=mysql_query($sql, $connect) or die (mysql_error());

        if ($res)
        {
            echo "<script type='text/javascript'>
                    alert('Class added succefully');
                    window.location='../classes.php'
                </script>";
        }
        else
        {
            echo "<script type=text/javascript'>
                    alert('Class not added');
                    window.location('../classes.php');
                </script";
        }
    }




    elseif ($import && $myfile)
    {
        echo $filename = $_FILES["file"]["tmp_name"];

        if ($_FILES["file"]["size"] > 0)
        {
            $thefile=fopen($filename, "r");

            while (($thedata = fgetcsv($thefile, 10000, ",")) !== FALSE)
            {
                $sql_file="INSERT INTO classes (`class`) VALUES ('$thedata[1]')";
                $res_file=mysql_query($sql_file, $connect) or die (mysql_error());

                if (!$res_file)
                {
                    echo "<script type='text/javascript'>
                                alert ('Invalid file type!');
                                window.location = '../classes.php';
                        </script>                   
                    ";
                }
            }

            fclose($thefile);

            echo "<script type='text/javascript'>
                        alert ('File Imported Successfully!');
                        window.location = '../classes.php';
                    </script>                   
            ";

        }
    }




    else
    {
        echo "<script type='text/javascript'>
                        alert('Fill the form or choose a CSV file');
                        window.location='../classes.php'
                </script>";
    }


?>

      

here is the html form

<form class="form-horizontal" method="post" name="addclass" action="popups/saveclass.php">

    <div style="margin:5px" class="form-group">
        <label class="col-sm-12">New Class Name:</label>
        <div class="col-sm-12">
            <input type="text" name="classname" class="form-control" placeholder="Class Name" />
        </div>
    </div>

    <center><input type="submit" name="save" value="Save" id="submit" style="width:200px; height:30px; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#002435; font-size:14px; cursor:pointer"/>
    </center>
</form> 

<form class="form-horizontal" method="post" name="uploadcsv" action="popups/saveclass.php" enctype="multipart/form-data">
    <br /><center>Or Upload CSV file</center><br />


    <div style="margin:5px" class="form-group">

        <div class="col-sm-12">
            <input type="file" name="myfile" class="form-control" />
        </div>
    </div>

    <center><input type="submit" name="import" value="Import" style="width:200px; height:30px; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#002435; font-size:14px; cursor:pointer"/></center>

</form> 

      

+3


source to share


1 answer


The file downloads are only in $_FILES

, not $_POST

. So $myfile =

$ _ POST ['myfile']; won't do anything, get rid of everything related to

$ myfile`.

There is no need to call stripslashes

or mysql_real_escape_string

on $_POST['import']

, since you do not store it in the database.

You are using $_FILES['file']

but the name of this input myfile

, so it should be $_FILES['myfile']

.



So the code should be:

elseif (isset($_POST['import'], $_FILES['myfile'])) {
    echo $filename = $_FILES["myfile"]["tmp_name"];
    if ($_FILES["myfile"]["size"] > 0)
    {
        ...
    }
}

      

+2


source







All Articles