Parse CSV file from Advanced Custom Fields ‘file’ field

I am using the Advanced Custom Fields plugin for wordpress to allow a user to upload a CSV file. I then need to get the data from that CSV file. I know I need to use fgetcsv() php function, but I am having trouble.

I can print the object like this:

Read More
$file = get_field('location_info');
print_r($file);

which gives me

Array ( [id] => 8 [alt] => [title] => locations  => [description] => [mime_type] => text/csv [url] => http://lc.something.com/wp-content/uploads/2016/03/locations.csv )

So how can I get the data out of that csv file using PHP.

I am currently trying:

$file = get_field('location_info');
$filecontents = fgetcsv($file);

But that give me the error “

Warning: fgetcsv() expects parameter 1 to be resource, array given in /var/www/websites/metromont/app/wp-content/themes/metromont/page-contact.php on line 7"

How do i get the ‘resource’ out of this?

Related posts

1 comment

  1. Fgetcsv() function expects this resource:

    Try to use code snippet below for getting CSV contents:

    $file = get_field( 'location_info' );
    
    if ( $file ) {
      if ( ( $handle = fopen( $file["url"], "r" ) ) !== FALSE ) {
        while ( ( $data_row = fgetcsv( $handle, 1000, "," ) ) !== FALSE ) {
          $num = count( $data_row );
          for ($c=0; $c < $num; $c++) {
            echo $data_row[ $c ] . ", ";
          }
           echo "<br />";
        }
        fclose( $handle );
      }
    }
    

    Output sample data:

    id, first_name, last_name, email, gender, ip_address,

    1, Philip, Evans, pevans0@hugedomains.com, Male, 8.106.111.3,

    2, Raymond, Matthews, rmatthews1@cdbaby.com, Male, 165.36.29.127,

    3, Kathy, Lawrence, klawrence2@usda.gov, Female, 17.23.224.247,

Comments are closed.