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.
Related
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.
I think it is something simple that has to do with my paths, because when I just create the files in the current folder, everything works. That being said, I am pretty unfamiliar with fopen(), fwrite(), and fclose(). The errors I am getting are (I think once the first one is fixed, the rest will work, too):
Warning: fopen(test/b/shipLabels/1_8154_0.pdf) [function.fopen]:
failed to open stream: No such file or directory in
/public_html/test/b/lib/FedEx/ShipWebServiceClient/Ground/Domestic
MPS/ShipWebServiceClient.php5 on line 116
Warning: fwrite() expects parameter 1 to be resource, boolean given in
/public_html/test/b/lib/FedEx/ShipWebServiceClient/Ground/Domestic
MPS/ShipWebServiceClient.php5 on line 117
Warning: fclose() expects parameter 1 to be resource, boolean given in
/public_Html/test/b/lib/FedEx/ShipWebServiceClient/Ground/Domestic
MPS/ShipWebServiceClient.php5 on line 118
The lines that references are:
$fp = fopen(SHIP_MASTERLABEL, 'wb');
fwrite($fp, ($masterResponse->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
fclose($fp);
I define SHIP_MASTERLABEL using this:
define('SHIP_MASTERLABEL', 'test/b/shipLabels/'.$clientId.'_'.$invoiceId.'_0.pdf');
In this case, $clientId=1, $invoiceId=8154. For the most part, this syntax has been provided by the FedEx docs.
Thanks so much for your help!
you 'd better use the absolute path ,and use the const like DIR FILE etc...
Given a URL that points to a file, how can I open it and read the contents in PHP?
$file = fopen("http://www.alinktomyfile.com/kasfafa", "r");
This line has an error. The docs say to use stream_get_contents() for a URL, however I get an error:
$file_contents = stream_get_contents($file);
fclose($file);
Error:
Warning: stream_get_contents() expects parameter 1 to be resource, null given in /Users/donald/Desktop/php_boilerplate.php on line 24
try file_get_contents()
$file = file_get_contents("http://www.alinktomyfile.com/kasfafa");
How do I create a file in php?
I have tried using...
$num = file_get_contents('qnum/qnum.txt');
$newFile = 'question'.$num.'.txt';
$newNum = $num++;
$openingQNUM = fopen('qnum/qnum.txt','w');
echo fwrite($openingQNUM,$newNum);
fclose($openingQNUM);
and I know I have called the functions right cause it is giving me the following error messages:
Warning: fopen(qnum/qnum.txt) [function.fopen]: failed to open stream: Permission denied in D:\Hosting\11017597\html\Question Site\ask.php on line 16
Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\Hosting\11017597\html\Question Site\ask.php on line 17
Warning: fclose() expects parameter 1 to be resource, boolean given in D:\Hosting\11017597\html\Question Site\ask.php on line 18
Can someone tell me what I am doing wrong?
PHP is complaining about not being able to create a new file because the filesystem denies access for writing. You should check if the process running PHP is allowed to write to the directory.
And in other news: ´file_put_contents()does all of the above and is the counterpart offile_get_contents()` - consider using it. It won't work right now, though, because of the file permission issue.
And after that: Welcome to the world of concurrent accesses! You are not using any form of locking, which makes your code a victim of race conditions. Two requests might read the same file (counter = 5), both increment the counter (= 6) and write back. Result: Counter is 6, but 7 would have been correct.
It seems you don't have read permissions on that file. Check if is_readable('qnum/qnum.txt') returns TRUE.
I have been using this code on a webpage for about 2 years and all of the sudden it started giving me an error. Is there any noticeable reason why?These are lines 116 through 120 that are referenced in the error
$file = fopen("http:/xxxxxx/climo/reports/".$fileName, "r");
$content = fgetcsv($file, 1000, ",");
$id = 1;
while ($content = fgetcsv($file, 1000, ",") != FALSE) {
error
.gov/climo/reports/today_raw_hail.csv) [function.fopen]: failed to open stream: no suitable wrapper could be found in /homepages/27/xxxx/htdocs/xxxxx/weather/php/mainEngine.php on line 116
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /homepages/27/xxxxx/htdocs/xxxx/weather/php/mainEngine.php on line 117
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /homepages/27/xxxx/htdocs/xxxx/weather/php/mainEngine.php on line 120
Your host probably updated the 'allow_url_fopen' directive in the php.ini
http://php.net/manual/en/filesystem.configuration.php
Apparently your hosting provider dropped support for http:// urls (as you can see in the errors).
Try reading the csv with curl instead.