...

View Full Version : Help me uploding a csv file.



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();">&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.



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.

mlseim
08-18-2007, 04:16 PM
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.

Inigoesdr
08-18-2007, 04:42 PM
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());

shaileshpatil
08-20-2007, 05:15 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum