Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP
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..
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
$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 2-Oct-12 20:15pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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';
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
0 OriginalGriff 478
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100