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.
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 can generate all keys fine, when I go to encrypt the file I get an error thrown at me: error:0E06D06C:configuration file routines:NCONF_get_string:no value but googling and checking stackoverflow only shows people with problems generating the keys.
My code: Here
EDIT: The error pops up when I put or die(openssl_error_string()); after openssl_private_encrypt($fileToEnc, $encFile, $privateKey) but I am now getting fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\xr1\encrypt.php on line 9
There was an error in my fopen dir somehow and it just broke. It is all good and fixed now, thanks.
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'm redoing a large portion of my website to use older mysql_* extensions because they execute faster. Yes I know people say its not a good idea to use older extensions, but I need to go for speed since this code is part of the back-end operations to my website and I want to serve as many people as possible.
I do have a small issue after converting things over. Sometimes when I try to close a database connection I get a PHP warning of:
PHP Warning: mysql_close(): 33 is not a valid MySQL-Link resource in /path/to/script.php on line #
and the line refers to the code below:
function DBclose($c){
if (isset($c) && !mysql_close($c)){
error_log("DB handle is invalid/null. Called from ".debug_backtrace()[2]['function'].'->'.debug_backtrace()[1]['function']);
}
}
This only happens sometimes.
What I want to do is change only this warning so that it includes the function that called it. Maybe to something more like:
PHP Warning: mysql_close(): 33 is not a valid MySQL-Link resource in /path/to/script.php on line # called from parent function <function-name>
How do I fix this and still make it so that other function calls I make that begin with # will have no errors printed on screen?
I don't think you can change the message, however, you can check if it's a resource using is_resource()
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...