PHP Parsing Question - php

If I am parsing an rss feed with php and xml, how can I parse more than one feed using one fopen statement. I am currently storing the feeds in different variables, and my fopen statement looks like this:
$fp=fopen($feedzero, $feedone, $feedtwo, "r")
When I run the code, I get this error:
fopen() expects parameter 4 to be resource, string given
Any help appreciated.

http://php.net/manual/en/function.fopen.php explains a bit about fopen function. also using http://framework.zend.com/manual/en/zend.feed.consuming-rss.html would save you from bug hunting

You can't. You'll have to open the files using separate handles and iterate over them separately.
$feedoneFp = fopen($feedone, 'r');
$feedtwoFp = fopen($feedtwo, 'r');
$feedthreeFp = fopen($feedthree, 'r');

fopen expects parameter 4 to be a resource. You gave it a string. It looks like you may have meant to pass in a mode representing "read-only". Mode is the 2nd parameter, not the 4th.

Related

PHP: What's the difference between fopen('file.txt', 'r') and file('file.txt')? [duplicate]

This question already has answers here:
Difference between file, file_get_contents, and fopen in PHP
(2 answers)
Closed 5 years ago.
What's the difference between fopen('file.txt', 'r') and file('file.txt')? They both appear to be the same...
Here's some info. Quote on file(), file_get_contents(), and fopen():
The first two, file and file_get_contents are very
similar. They both read an entire file, but file reads the file into
an array, while file_get_contents reads it into a string. The array
returned by file will be separated by newline, but each element will
still have the terminating newline attached, so you will still need to
watch out for that.
The fopen function does something entirely different—it opens a
file descriptor, which functions as a stream to read or write the
file. It is a much lower-level function, a simple wrapper around the C
fopen function, and simply calling fopen won't do anything but
open a stream.
Once you've open a handle to the file, you can use other functions
like fread and fwrite to manipulate the data the handle
refers to, and once you're done, you will need to close the stream by
using fclose. These give you much finer control over the file
you are reading, and if you need raw binary data, you may need to use
them, but usually you can stick with the higher-level functions.
So, to recap:
file — Reads entire file contents into an array of lines.
file_get_contents — Reads entire file contents into a string.
fopen — Opens a file handle that can be manipulated with other library functions, but does no reading or writing itself.
Credit goes to Alexis King.

Is stream_get_contents lower level and faster than file_get_contents?

From a comment to this answer I read that "stream_get_contents is low-level" comparing to file_get_contents. However according to Manual, stream_get_contents is
Identical to file_get_contents(), except that stream_get_contents() operates on an already open stream resource and returns the remaining contents in a string, up to maxlength bytes and starting at the specified offset.
Which statement is correct?
Is stream_get_contents really lower level and faster?
Specifically I am interested in reading local files from HD.
I'm late here but it might help others
file_get_contents() loads the file content into memory. It sits there in memory and waits for the program to call echo upon which it will be delivered to the output buffer.
A good usage example is:
echo file_get_contents('file.txt');
stream_get_contents() delivers the content on an already open stream. An example is this:
$handle = fopen('file.txt', 'w+');
echo stream_get_contents($handle);
You could see that stream_get_contents() used an existing stream created by fopen() to get the contents as a string.
file_get_contents() is the more preferred way as it doesn't depend on an open stream, and is efficient with your memory using memory mapping techniques. For external sites reading, you can also set HTTP headers when getting the content. (Refer to https://www.php.net/manual/en/function.file-get-contents.php for more info)
For larger files/resources, stream_get_contents() may be preferred as it delivers the content fractionally as opposed to file_get_contents() where the entire data is dumped in memory.

Can't read large XML file from PHP

I have a huge XML file (114 KB/1719 lines; see the error message below why I say huge) and I try to read it as I have done with two similar files before.
The other two files load normally and are of comparable size the only difference being that the file in question contains Arabic text. So here is the PHP code:
$doc3 = new DOMDocument('1.0', 'utf-8');
$doc3->load($masternumber.'.xml');
And the error is:
Warning: DOMDocument::load() [domdocument.load]: Excessive depth in document: 256 use XML_PARSE_HUGE option in file: ...
Then $doc3 doesn't load the file. So I modified the code:
$doc3->load($masternumber.'.xml', "LIBXML_PARSEHUGE");
And I end-up with another warrning:
Warning: DOMDocument::load() expects parameter 2 to be long, string given in...
$doc3 is empty again.
What is wrong with it? The other files contain the same text in other languages and load properly but not this one? I am using PHP 5.3.9.
Use a constant, not a string.
$doc3->load($masternumber.'.xml', LIBXML_PARSEHUGE);
See the DOMDocument::load() documentation for complete details. The second parameter is a long integer representing the selected options from the list of constants.
Incidentally, if you need multiple options for any reason, it is done by combining them with the bitwise OR operator |
// Multiple options OR'd together...
// Just FYI, not specific to your situation...
$doc3->load($masternumber.'.xml', LIBXML_PARSEHUGE|LIBXML_NSCLEAN);

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->fout = 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.

PHP fseek() equivalent for variables?

What I need is an equivalent for PHP's fseek() function. The function works on files, but I have a variable that contains binary data and I want to work on it. I know I could use substr(), but that would be lame - it's used for strings, not for binary data. Also, creating a file and then using fseek() is not what I am looking for either.
Maybe something constructed with streams?
EDIT: Okay, I'm almost there:
$data = fopen('data://application/binary;binary,'.$bin,'rb');
Warning: failed to open stream: rfc2397: illegal parameter
Kai:
You have almost answered yourself here. Streams are the answer. The following manual entry will be enlightening: http://us.php.net/manual/en/wrappers.data.php
It essentially allows you to pass arbitrary data to PHP's file handling functions such as fopen (and thus fseek).
Then you could do something like:
<?php
$data = fopen('data://mime/type;encoding,' . $binaryData);
fseek($data, 128);
?>
fseek on data in a variable doesn't make sense. fseek just positions the file handle to the specified offset, so the next fread call starts reading from that offset. There is no equivalent of fread for strings.
Whats wrong with substr()?
With a file you would do:
$f = fopen(...)
fseek($f, offset)
$x = fread($f, len)
with substr:
$x = substr($var, offset, len)
I'm guessing, but maybe what is being asked for is a way to access bytes in a variable by using a pointer.. (using it like an array of bytes like you could do in c - without the memory overhead of putting the data in php arrays) and being able to edit them inplace without the overhead of copying the data.
Not being able to do this is a BIG problem, but if the operating system caches disk data well using fseek on a temporary file could be a workaround.

Categories