using fopen on uploaded file with php

Posted by Patrick on Stack Overflow See other posts from Stack Overflow or by Patrick
Published on 2010-04-11T02:05:22Z Indexed on 2010/04/11 2:13 UTC
Read the original article Hit count: 388

Filed under:
|

Im uploading a file, and then attempting to use fgetcsv to do things to it.

This is my script below. Everything was working fine before i added the file upload portions. Now its not showing any errors, its just not displaying the uploaded data.

Im getting my "file uploaded" notice, and the file is saving properly, but the counter for number of entries is showing 0.

if(isset($_FILES[csvgo])){

    $tmp = $_FILES[csvgo][tmp_name];

    $name = "temp.csv";

    $tempdir = "csvtemp/";

    if(move_uploaded_file($tmp, $tempdir.$name)){ echo "file uploaded<br>"; }

    $csvfile = $tempdir.$name;

    $fh = fopen($csvfile, 'r');
    $headers = fgetcsv($fh);
    $data = array();
    while (! feof($fh))
    {
        $row = fgetcsv($fh);
        if (!empty($row))
        {
            $obj = new stdClass;
            foreach ($row as $i => $value)
            {
                $key = $headers[$i];
                $obj->$key = $value;
            }
            $data[] = $obj;
        }
    }
    fclose($fh);

    $c=0;

    foreach($data AS $key){

            $c++;

            $phone = $key->PHONE;

            $fax = $key->Fax;

            $email = $key->Email;

            // do things with the data here

    }

    echo "$c entries <br>";
}

© Stack Overflow or respective owner

Related posts about php

Related posts about fgetcsv