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.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help me uploding a csv file.

    Hi guys

    I have a following tasks to be done prior uploading a csv file.
    1) Users selects the csv file.
    2) Loading a contents of csv file.
    3) If user hits save/submit then upload file and insert the records to database.

    I have achieved point # 1 and 2 as below:
    import1.php
    Code:
    <FORM METHOD=POST name="form1" ACTION="import1.php" enctype="multipart/form-data">
    <CENTER>
    <TABLE width="50%" class="table">
    <TR>
        <TD class="tdheader" colspan="2">Importing relation from csv file</TD>
    
    </TR>
    <TR>
        <TD class="tdheader">File</TD>
        <TD class="datatd"><INPUT TYPE="file" NAME="filectrl" class="txtbox"></TD>
    </TR>
    <TR>
        <TD class="tdheader">Name </TD>
        <TD class="datatd"><INPUT TYPE="text" NAME="filename" class='txtbox'></TD>
    </TR>
    <TR>
        <TD class="tdheader" colspan=2 align="center"><INPUT TYPE="button" value="Next" class="button" onclick="javascript:Next();">&nbsp;&nbsp;<INPUT TYPE="button" value="Close" class="button" onclick="javascript:Close();"></TD>
    
    </TR>
    </TABLE>
    </CENTER>
    </FORM>
    On clicking next button the form is submitted to import2.php, where he can view the preview of data.

    PHP Code:
    function get_csv_header_fields($filesource) {

        
    $arr_csv_columns = array();
        
    $fpointer fopen($filesource"r");

        if (
    $fpointer) {
          
    $arr fgetcsv($fpointer10*1024",");

          if(
    is_array($arr) && !empty($arr))
          {
              foreach(
    $arr as $val)
                
    //print '<br>val='.$val;
                
    if(trim($val)!="")
                  
    $arr_csv_columns[] = $val;
          }

          
    fclose($fpointer);
            return 
    $arr_csv_columns;
        } else
          
        return 
    $arr_csv_columns;
      }

        
    $fcontents file ($filectrl); 
        
    $header_fields=  get_csv_header_fields($filectrl);

        
    $delimiter=',';
        
    $previewLimit=5;
        
    //$headers=implode(",", $header_fields); 
    $header_values contains the header's of csv file and $fcontents contains the data, which i have to insert in database.

    But i am failing on step 3. Now when user clicks on save button and i submit the form, i loose the contents of file control ($filectrl)

    Is it possible to display the preview of csv file the user has selected without submitting the form?


    Thanks in advance.
    -------------------------
    Shailesh Patil
    -------------------------

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I have an idea that may trigger some more ideas.

    Generate a random number and save the actual CSV
    file into a temporary directory on the server using that
    random number as the filename.

    Now, you can do whatever you want with that temporary
    file. You can open it, view its contents, and when ready,
    you can open it, process contents and save to database.
    Then, you delete that temporary file when finished.

    During the time that the file is being temporarily held, you
    only need to retain that random number, and use that for
    the dialog of preview, confirming, and finally accepting the
    submittal.

    If you save that random number in a session variable, the
    user can leave your site and return anytime and continue
    the dialog. Perhaps they accidently exit before accepting
    the preview? If you use a cookie, the user can actually
    close their browser and still return later to accept the change.
    You of course have control over deleting that temporary file
    at anytime.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by mlseim View Post
    I have an idea that may trigger some more ideas.

    Generate a random number and save the actual CSV
    file into a temporary directory on the server using that
    random number as the filename.
    A simple and effective way could be: md5($filename . time());

  • #4
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey thanks mlseim and inigoesdr for your time.
    Thats wonderful.
    i will upload the file in some temp directory and generate a preview for user, after he confirms, i will move the file from temp directory to my upload directory and insert the records.


    Thanks once again.
    -------------------------
    Shailesh Patil
    -------------------------


  •  

    Posting Permissions

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