Yii Framework Forum: Cuploadedfile Header - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cuploadedfile Header Rate Topic: -----

#1 User is offline   Led 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 208
  • Joined: 16-January 12

Posted 29 April 2013 - 03:44 PM

need to put in array the first line(header) of uploaded csv.

this is my code

$model= new ImportForm;
        if (isset($_POST['ImportForm'])) {
            $arr= array();
             
           $arr=  str_getcsv( fgets(CUploadedFile::getInstance($model,'csv')),0,';');


fgets() expects parameter 1 to be resource, object given

I need to compare the header to the atributeLabels of the model
and validate
0

#2 User is offline   Fabrizio Caldarelli 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 336
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 29 April 2013 - 03:50 PM

You have to read entire file, so use file_get_contents instead fgets (that read a line at time).

Then pass "tempName" attribute to file_get_contents, that contains temporary path of file you have uplaoded.

Should be:
$model= new ImportForm;
        if (isset($_POST['ImportForm'])) {
            $arr= array();
             
           $arr=  str_getcsv( file_get_contents(CUploadedFile::getInstance($model,'csv')->tempName),0,';');

0

#3 User is offline   Led 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 208
  • Joined: 16-January 12

Posted 29 April 2013 - 04:08 PM

View PostFabrizio Caldarelli, on 29 April 2013 - 03:50 PM, said:

You have to read entire file, so use file_get_contents instead fgets (that read a line at time).

Then pass "tempName" attribute to file_get_contents, that contains temporary path of file you have uplaoded.

Should be:
$model= new ImportForm;
        if (isset($_POST['ImportForm'])) {
            $arr= array();
             
           $arr=  str_getcsv( file_get_contents(CUploadedFile::getInstance($model,'csv')->tempName),0,';');


thanks.
but how to get the first line ?
0

#4 User is offline   Fabrizio Caldarelli 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 336
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 29 April 2013 - 04:14 PM

Sorry, then for only the first line use fgets:

$handle = fopen(CUploadedFile::getInstance($model,'csv')->tempName, "r");
if ($handle) {
   $firstLine = fgets($handle, 4096);
   $arr=  str_getcsv( $firstLine,0,';');

   fclose($handle);
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users