shaileshpatil
08-18-2007, 09:18 AM
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
<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();"> <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.
function get_csv_header_fields($filesource) {
$arr_csv_columns = array();
$fpointer = fopen($filesource, "r");
if ($fpointer) {
$arr = fgetcsv($fpointer, 10*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.
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
<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();"> <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.
function get_csv_header_fields($filesource) {
$arr_csv_columns = array();
$fpointer = fopen($filesource, "r");
if ($fpointer) {
$arr = fgetcsv($fpointer, 10*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.