简体   繁体   中英

using fopen on uploaded file with php

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>";
}

You might need to specify an absolute server path.

$tempdir = $_SERVER['DOCUMENT_ROOT'] . "/csvtemp/";

Edit: I tested out your code with only a few minor modifications (the absolute path and adding quotes to your array key names) and it worked fine. Its probably something outside the scope of what you posted. Perhaps a file permissions error or something funky with the upload form.

Just a thought, but you might try raising your PHP error level. This might uncover something that had otherwise gone unnoticed.

ini_set("display_errors", 1);
ini_set("error_reporting", 2147483647);

And if that fails, you can always resort to throwing a bunch of echos all over the place until you find the culprit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM