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.
Related
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.
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 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);