parameter error with fputcsv() in PHP - php

I am trying to run through a dictionary, .txt file, calculate the metaphone() values and append that to each line. Then write this to a new file.
I am getting an error though on the line that I am using fputcsv() and it says: expects parameter 1 to be resource, boolean given
I don't believe I am passing it a boolean. I don't understand what I am doing wrong.
<?php
$dict = fopen("originalDictionary.txt", "r");
$keyedDict = fopen("dictionary.txt", "w");
while ($line = fgets($dict)){
$line = trim(strtolower($line));
fputcsv($keyedDict, array($line,metaphone($line)));
}
fclose($dict);
fclose($keyedDict);
?>
Here is a link to originalDictionary.txt if that helps.

fopen
Returns a file pointer resource on success, or FALSE on error.
probably the opening of the file fails and the function returns false, there you have your boolean
check the file permissions and wether any errors are triggered (display them for example with ini_set('display_errors',1); error_reporting(E_ALL);

Related

PHP $filename - expects parameter 1 to be a resource error

I am trying to write this file to a json file on my server and have successfully done this in many other places but for some reason this one is throwing an error that I don't understand because the $filename is correct. This works for other feeds but not this one - it is roughly 3.5mb in size so not huge. Any ideas as to why it would throw this error when public_html/data/GeoMAC-Current-Fires.json is correct? This may seem like I duplicate but I did read every suggested question/answer to no avail.
[14-Aug-2018 16:44:21 UTC] PHP Warning: fopen(public_html/data/GeoMAC-Current-Fires.json): failed to open stream: No such file or directory in /home4/theprfk5/public_html/cronjobs/GeoMAC-Current-Fires.php on line 5
[14-Aug-2018 16:44:21 UTC] PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in /home4/theprfk5/public_html/cronjobs/GeoMAC-Current-Fires.php on line 6
[14-Aug-2018 16:44:24 UTC] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in ///public_html/cronjobs/GeoMAC-Current-Fires.php on line 8
Code:
<?php
$site = 'https://wildfire.cr.usgs.gov/arcgis/rest/services/geomac_dyn/MapServer/3/query?where=Active%20=%20%27Y%27&geometryprecision=4&outfields=incidentname,datecurrent,active,gisacres&f=geojson';
$homepage = file_get_contents($site);
$filename = 'public_html/data/GeoMAC-Current-Fires.json';
$handle = fopen($filename,"w");
fwrite($handle,$homepage);
echo file_get_contents($site);
fclose($handle);
?>
These two lines cause the issue:
$filename = 'public_html/data/GeoMAC-Current-Fires.json';
$handle = fopen($filename,"w");
When fopen() tries to open $filename it returns false. Therefore fwrite() is trying to write to false which causes fwrite() expects parameter 1 to be resource, boolean given in ...
You will need to make sure that your path in your $filename is correct. As – Héctor mentioned above, you will need use the real path to that file.

Php fwrite/fclose warning

I have the following code fragment which works btw:
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
fwrite($slot, $txt);
fclose($slot);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);
fwrite($oc);
fclose($oc);
But the following warnings get logged when running it:
Line 81 : fwrite() expects parameter 1 to be resource, integer given
Line 82 : fclose() expects parameter 1 to be resource, integer given
Line 85 : fwrite() expects at least 2 parameters, 1 given
Line 86 : fclose() expects parameter 1 to be resource, integer given
Probably my logic is wrong here. Maybe someone can shed some light here?
You don't need fwrite() or fclose() at all when you use file_put_contents(). From the docs for file_put_contents():
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.
Your code should look like:
$file = fopen("../offer/work/uploads/".$php_id.".html","w");
fwrite($file,$data); // Note: you could use file_put_contents here, too...
fclose($file);
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);
As for why you get errors with your current code: fwrite() and fclose() expect the first parameter to be a resource (the type of return value you get from fopen()). But you're passing them the value returned by file_put_contents(), which is an integer. So, you get an error.
file_put_contents handles the operations open, write and close all in one go – there is not need to call fwrite and fclose after it. (Not only no need – it doesn’t even make any sense, because with file_put_contents you do not even have a file handle to begin with.)
file_put_contents returns the number of bytes written, an integer value – and that’s why you get those warnings.

Why does reading from a php://memory wrapper I've just written to fail?

I'm trying to read from the php://memory wrapper using fread() but fread() always returns false. My code is simplified:
$file_handle = fopen('php://memory', 'w+'); // Have tried php:temp also.
fwrite($file_handle, 'contents');
$file = fread($file_handle, filesize($file_handle)); // Have also tried 99999 for filesize.
$file always is false after the fread().
What's going on?
Thanks in advance!
You'll need to rewind($file_handle) after writing before you can read what you've just written, because writing moves the file pointer to the end of the file
Call rewind($file_handle) before fread()
Found the answer here:
https://stackoverflow.com/a/2987330/2271704
I need to call rewind($file_handle) before I call fread().
You should be getting this:
Warning: filesize() expects parameter 1 to be string, resource given
And those are the the first problem:
int filesize ( string $filename ) — Gets the size for the given file.
... and the second one: you haven't enabled error reporting.
Third problem is that your pointer is at the end of the file. Try something like this:
error_reporting(E_ALL);
ini_set('display_errors', true);
// ...
rewind($file_handle);
$file = ''; //
while (!feof($file_handle)) {
$file .= fread($file_handle, 8192);
}

PHP Parsing Question

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.

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.

Categories