开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜