Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually I have to import a csv file and that data want to store in database..I ve done it successfully.. but I add one validation there for type of csv file.if the file any other than csv then it should not be imported.. I did coding for this file..but it is successful only in mozila-firefox . Other browser giving some error to me..Please help me..Here is my code..
PHP
if (isset($_POST['load']))
   {
     if ( isset($_FILES["file"]))
     {
       if ($_FILES["file"]["error"] > 0)
       {
        echo "<p><font color='red'>No File Selected </p></font> <br />";
       }
       else
       {

               $file_name = $_FILES["file"]["name"];
               if($_FILES["file"]["type"] != "text/csv")
           {

               die("<p><font color='red'>This is not a CSV file.</p></font>");
           }
                 else
               {
                   $location="C:\\xampp\\mysql\\data\\import\\";
                   move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);
               }

        }
        $counter=0;

          $handle = fopen($location . $_FILES["file"]["name"], 'r');

Problem is at
XML
$file_name = $_FILES["file"]["name"];
               if($_FILES["file"]["type"] != "text/csv")
           {

               die("<p><font color='red'>This is not a CSV file.</p></font>");
           }


but i dnt know how to solve it??
Posted

On post, file type is not calculated as you wish. You need an other method to determine file type, like this one: http://www.php.net/manual/en/ref.fileinfo.php[^]. But be aware that might be able to detect only well formed csv.
 
Share this answer
 
if(preg_match('/MSIE/i',$u_agent))
{
if($_FILES["file"]["type"] !="application/vnd.ms-excel")

{
echo ("

This is not a CSV file.

");
}
else
{
$file_uploaded=move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);
}
}
elseif(preg_match('/Firefox/i',$u_agent))
{
if($_FILES["file"]["type"] !="text/csv")

{
echo ("

This is not a CSV file.

");
}
else
{
$file_uploaded=move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);

}
}
elseif(preg_match('/Chrome/i',$u_agent))
{
if($_FILES["file"]["type"] !="application/vnd.ms-excel")

{
echo ("

This is not a CSV file.

");
}
else
{
$file_uploaded=move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);
}
}
elseif(preg_match('/Safari/i',$u_agent))
{
echo'This is valid Safari';
}
elseif(preg_match('/Opera/i',$u_agent))
{
if($_FILES["file"]["type"] !="text/comma-separated-values")

{
echo ("

This is not a CSV file.

");
}
else
{
$file_uploaded=move_uploaded_file($_FILES["file"]["tmp_name"], $location . $_FILES["file"]["name"]);
}
}
elseif(preg_match('/Netscape/i',$u_agent))
{
echo'Please change browser';
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900