开发者

Function returns CSV File: How to go about checking it?

I am doing something like:

$outputFile = getCurrentDBSnapshot($data);

where $data is the resource stream that am passing in, basically from command prompt am passing an file and am opening it using fopen for writing with 'w+' permissions, now getCurrentDBSnapshot would get the current state of a table and would update the $data csv file, so basically $outputFile would be updated with the current state of database table, now I want to var_dump or print the value of $outputFile to see the data present into it.

But when I do

$this->开发者_如何学Pythonfout = fopen($outputFile,'r') or die('Cannot open file');
$test = fgetcsv($outputFile,5000,";");
var_dump($test);

It gives me an error saying that it expects parameter 1 to be a string type and am passing resource.

My goal to see the contains of $outputFile

and so my question is that

How can I see the contains present in $outputFile or how can I see what getcurrentDBSnapshot function is returning me ?


fegtcsv takes as first parameter a file handle, not a filename. You'll need to do something like:

$this->fout = fopen($outputFile,'r') or die('Cannot open file');
while ($test = fgetcsv($this->fout,5000,";"))
{
    var_dump($test);
}

Note that fgetcsv merely gets a single line of the file, analogously to fgets.

Also I'm not sure why you're passing a semicolon as the third argument to fgetcsv. CSV stands for comma-separated-value; are you sure your file is semicolon-delimited?


Quoting the fgetcsv page of the manual, the first parameter passed to fgetcsv should be :

A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen().


Here, you are passing as first parameter $outputFile, which contains the name of the file you are trying to read from -- i.e. a string, and not a handle to an opened file.

Considering you are calling fopen and storing its return-value in $this->fout, this is probably the variable you should be passing to fgetcsv, like this :

$this->fout = fopen($outputFile,'r') or die('Cannot open file');
$test = fgetcsv($this->fout,5000,";");
var_dump($test);


As a sidenote : fgetcsv will only return the data of one line each time you call it -- which means you might have to use a loop, if you want to see the content of the whole file.

If needed, take a look at Example #1, on the manual page of fgetcsv.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜