Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Exclamation CSV Upload error.....

    Hi Freinds,


    I have created a CSV upload oprtion for my webpage and now I am writing a Php code for action to post data in to mysql.


    PHP Code:
    <?php
    session_start
    ();
    $host="localhost"// Host name 
    $username="******"// Mysql username 
    $password=""// Mysql password 
    $db_name=""// Database name 
    $tbl_name="new_user_verf"// Table name 
    $myusername $_SESSION['myusername']; //user who updating
    $con mysql_connect("$host","$username","$password")or die("cannot connect to server");
    mysql_select_db("$db_name")or die("cannot select DB");
    $fp             fopen("C:\Users\Desktop\new_user_data.csv""r");
    $length         8096/// have to be optional length ...
    $fildDelineate  ','/// or "|" ... declare what you need
    $counter        1// to omission first row if it is table headers
    while( !feof($fp) ) {
      if( !
    $line fgetcsv($fp$length$fildDelineate'') && $counter) {
         continue;
      }
      foreach(
    $line as $key => $value){
       
    // For example insert in 3 column (e.g. id, name, email )
       
    $importSQL "insert into $tbl_name values('$line[0]','$line[1]','$line[2]','$line[3]','$line[4]','$line[5]','$line[6]','$line[7]','$line[8]','$line[9]')";
       
    mysql_query($importSQL) or die(mysql_error());
      }
      
    $counter++;
    }
    {
    header("location:new_user.php") or die("record not inserted");
    }
    fclose($fp);
    ?>
    The code above is generating below errors pls help me.



    Code:
    Warning: fopen(C:\Users\Desktop ew_user_data.csv) [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\SAM\csv_upload.php on line 11
    Code:
    Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SAM\csv_upload.php on line 16
    
    Warning: feof() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SAM\csv_upload.php on line 15

    Please help me !!!!....and suggest what mistake am I doing here!!

    Regards,
    Nani
    Last edited by nani_nisha06; 10-08-2012 at 03:47 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,091
    Thanks
    23
    Thanked 593 Times in 592 Posts
    A place to start
    Code:
    C:\Users\naveen.c\Desktop\new_user_data.csv
    Do not end a directory name with a space or a period. You did. The Desktop in this line throws me also. To make things simple why not put new_user_data.csv into the C: dir and go from there?

  • Users who have thanked sunfighter for this post:

    nani_nisha06 (10-08-2012)

  • #3
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by sunfighter View Post
    A place to start
    Code:
    C:\Users\Desktop\new_user_data.csv
    Do not end a directory name with a space or a period. You did. The Desktop in this line throws me also. To make things simple why not put new_user_data.csv into the C: dir and go from there?
    But in realty if the user try to upload from desktop then what I guess the issue would be the same so can you help me to fix this ???

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Uh, what?
    If you upload, you get the path from the $_FILES superglobal. It is explicit in a chosen temp directory and the filename will be some randomness. You cannot access a remote filesystem.

    The problem here is the use of \. \ followed by another character may be a command which is interpreted in double quotations. Escape the slash with a \ or convert them to front slashes.

    The rest of the errors are because you didn't implement any type of error controlling. You simply assume that the resource is valid and opened successfully instead of checking that it did. Resource handles will return false if they fail, or a resource if they are successful. Block them in branches to verify if they are successful.

  • Users who have thanked Fou-Lu for this post:

    nani_nisha06 (10-09-2012)

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Quote Originally Posted by Fou-Lu View Post
    The problem here is the use of \. \ followed by another character may be a command which is interpreted in double quotations. Escape the slash with a \ or convert them to front slashes.
    Better yet put that absolute path in single quotes and use forward slashes since it is a literal value.

    PHP Code:
    $fp             fopen('C:/Users/Desktop/new_user_data.csv''r'); 
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    nani_nisha06 (10-09-2012)

  • #6
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Sure will change and see if it works !!!

  • #7
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by nani_nisha06 View Post
    Sure will change and see if it works !!!
    Still I see this error when I execute above code with single quotes

    Code:
    Warning: fgetcsv() [function.fgetcsv]: enclosure must be a character in C:\xampp\htdocs\SAM\csv_upload.php on line 16
    Last edited by nani_nisha06; 10-09-2012 at 04:07 PM.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's not a cause of the resource itself, that's a cause of your fgetcsv call for the enclosure. This must be a character, you have given it ''. Give it an enclosing character so if you have a , in your field it can still parse correctly.

  • #9
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's not a cause of the resource itself, that's a cause of your fgetcsv call for the enclosure. This must be a character, you have given it ''. Give it an enclosing character so if you have a , in your field it can still parse correctly.
    Still I am stuck on this can any one help me ???

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.

  • #11
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.
    can that be a \n???

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by nani_nisha06 View Post
    can that be a \n???
    Theoretically it says *character* which implicitly is any character, but no it cannot be a linefeed as that wouldn't make any sense. Delimiters and enclosures should be actual characters, not whitespace based characters.

  • #13
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Theoretically it says *character* which implicitly is any character, but no it cannot be a linefeed as that wouldn't make any sense. Delimiters and enclosures should be actual characters, not whitespace based characters.
    Fou-Lu,

    I have removed this enclosure even though it doesn't work & even after assigning value it gives me same error can you help me solve this pls?

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Post the code and the error.

  • #15
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Fou-Lu,

    I have scussesfully coded below code for exporting CSV file in to Mysql but now I want to know how many of the lines ignored while inserting ...that should be a error message to be echoed to the user ..


    PHP Code:
    <?php
    session_start
    ();
    include_once 
    "C:/xampp/htdocs/SAM/include/database.php";
    $myusername $_SESSION['myusername']; //user who updating

    $file            $_FILES['up_file']['tmp_name'];//"new_user_verf.csv";
    $fp             fopen($file,"r");
    $length         8096/// have to be optional length ...
    $fildDelineate  ','/// or "|" ... declare what you need
    $counter        0// to omission first row if it is table headers


    while( !feof($fp) ) {
      
    $value fgetcsv($fp$length$fildDelineate);
      
    /*print_r($value);*/
      
    $date date('Y-m-d H:m:s',strtotime($value[0]));
       
         
    $importSQL "INSERT IGNORE INTO $tbl_name0 (`Account_Create_datetime`,`Analyst`,`Acc_User_Name`,`Email_Address`,`User_Ip`)
           VALUES ('"
    .$date."','".$myusername."','".$value[1]."','".$value[2]."','".$value[3]."')";
         
    $resultmysql_query($importSQL) or die(mysql_error());
          
    $counter++;
    }
    fclose($fp);
    if (
    $result==1){
    $_SESSION['Notify']['type'] = 'Sucess Message:';
    $_SESSION['Notify']['msg'] = 'Uploaded total number of->'.$counter.'<-records in to the database.\n';
    header("location:/SAM/TEST/test.php") or die("record not inserted");
    }
    elseif(
    $result==IGNORE){
    $_SESSION['Notify']['type'] = 'Error Message:';
    $_SESSION['Notify']['msg'] = 'Could not upload, number of->'.$counter.'<-records in to the database.\n';
    header("location:/mym/new_user/new_user.php") or die("record not inserted");
    }
    else{
    $_SESSION['Notify']['type'] = 'Error Message:';
    $_SESSION['Notify']['msg'] = 'Some how messed up, check with your admin!!!';
    header("location:/SAM/TEST/test.php") or die("record not inserted");
    }
    echo 
    "Imported successfully!";
    ?>

    Any suggestion appreciated.....

    Regards,
    Nani


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •