Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to read csv file with PHP

3 Answers

0 votes
$csv_file = fopen("e:/test.csv", "r");
while (!feof($csv_file))
    print_r(fgetcsv($csv_file));
fclose($csv_file);
    
/*
run:

Array ( [0] => csv_post_title [1] => csv_post_post [2] => csv_post_excerpt [3] 
=> csv_post_categories [4] => csv_post_tags [5] => csv_post_date [6] => csv_post_author [7] 
=> csv_post_slug [8] => csv_ctax_art [9] => csv_ctax_country [10] => my_custom1 ) Array ( [0] 
=> Vincent Van Gogh [1] => Vincent Willem van Gogh[a 1] (30 March 1853...

*/

 



answered Dec 9, 2015 by avibootz
0 votes
$csv_file = fopen("e:/test.csv", "r");
while (!feof($csv_file))
{
    echo '<pre>';
    print_r(fgetcsv($csv_file));
    echo '</pre>';
}
fclose($csv_file);
    
/*
run:

Array
(
    [0] => csv_post_title
    [1] => csv_post_post
    [2] => csv_post_excerpt
    [3] => csv_post_categories
    [4] => csv_post_tags
    [5] => csv_post_date
    [6] => csv_post_author
    [7] => csv_post_slug
    [8] => csv_ctax_art
    [9] => csv_ctax_country
    [10] => my_custom1
)

Array
(
    [0] => Vincent Van Gogh
    [1] => Vincent Willem van Gogh[a 1] (30 March 1853 29 July 1890) 
    ...

*/

 



answered Dec 10, 2015 by avibootz
0 votes
/*
 array fgetcsv ( resource $handle [, int $length = 0 
                                  [, string $delimiter = ","  
                                  [, string $enclosure = '"' 
                                  [, string $escape = "\" ]]]] )
 */


$row = 1;
if (($csv_file = fopen("e:/test.csv", "r")) !== FALSE) 
{
    while (($row_data = fgetcsv($csv_file, 1024, ",")) !== FALSE) 
    {
        $num_fields = count($row_data);
        echo "<p> Line $row: <br /></p>\n";
        $row++;
        for ($i = 0; $i < $num_fields; $i++) 
            echo $row_data[$i] . "<br />\n";
    }
    fclose($csv_file);
}

    
/*
run:

Line 1: 


csv_post_title
csv_post_post
csv_post_excerpt
csv_post_categories
csv_post_tags
csv_post_date
csv_post_author
csv_post_slug
csv_ctax_art
csv_ctax_country
my_custom1

Line 2: 


Vincent Van Gogh
Vincent Willem van Gogh 30 March 
...

*/

 



answered Dec 10, 2015 by avibootz

Related questions

1 answer 207 views
1 answer 167 views
1 answer 185 views
1 answer 171 views
171 views asked Jul 17, 2017 by avibootz
2 answers 264 views
3 answers 258 views
258 views asked Dec 1, 2015 by avibootz
...