I'm trying to give a php file a permission to write to JSON file, but it does not seem to work. here is my code
<?php
$light = $_GET['light'];
if($light == "on") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "on"}');
fclose($file);
}
else if ($light == "off") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "off"}');
fclose($file);
}
chmod(" /home/daffo/public_html/ard/light.json", 0755);
?>
You must remove the space before the first slash:
chmod("/home/daffo/public_html/ard/light.json", 0755);
Other than the obvious, removing the space before your first slash:
chmod("/home/daffo/public_html/ard/light.json", 0755);
You'll run into trouble creating your own json. Do it using in-built functions:
fwrite($file, json_encode(array("light"=> "off")));
Related
How to write in a file text if text doesn't exist and if already exists it continue for other part of text.
I have also to delete text in some conditions (if $data['y'] pass from 1 to 0)
Thank you.
foreach ($x['data1']['alarms2'] as $data){
if ($data['y']==1){
$new_file= "test.txt";
$file = fopen($new_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($file, '....');
fclose($file);
}
}
Use "a+" modificator, instead of "w":
$new_file= "test.txt";
$file = fopen($new_file, 'a+') or die('Cannot open file: '.$my_file);
fwrite($file, '....');
fclose($file);
i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:
Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3
this is my code:
$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
both the php file and input.txt are placed in www folder of wamp.
Hope this will help you:
$File = "log_post.txt";
$fh = fopen ($File, 't') or die("can't open file");
fclose($fh);
$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
Use this code and keep the input.txt file in the same directory where this code is written.
First check if file exist or not?
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
chmod($filename, 0777);
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;
or
$location = file_get_contents('input.txt');
echo $location;
hope it will help
Add full path to file.
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$location = fopen("C:\\folder\\input.txt", "r");
Remove '/'(Slash)
$location = fopen("input.txt", "r") or die("Unable to open file!");
$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);
You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.
I want to filter one text file by writing the results to a second text file.
I have a little bit of code and it doesn't work like it should work, it is only writing the LAST line of the first text file into a separate backup text file.
Code:
//filter the ingame bans
$search = "permanently";
$logfile = "ban_list.txt";
$timestamp = time();
// Read from file
$file = fopen($logfile, "r");
while( ($line = fgets($file) )!= false)
{
if(stristr($line,$search))
{
$cache_ig = "ingamebanlist.txt";
$fh = fopen($cache_ig, 'w') or die("can't open file");
$content = "\n";
fwrite($fh, $content);
$content = $line;
fwrite($fh, $content);
fclose($fh);
}
}
I personally do not see any errors in my code, please help.
REMEMBER: It does kind of work, but it only writes the LAST line of the ban_list.txt file into the ingamebanlist.txt file...
What happens with your code is that you open(using write mode), write and close inside your loop so it will only ever write 1 entry then overwrite it until the last entry, thus only saving the last item.
What you want is to have it outside the loop like this:
<?php
$search = "permanently";
$logfile = "ban_list.txt";
$cache_ig = "ingamebanlist.txt";
$timestamp = time();
$read = fopen($logfile, "r") or die("can't read file");
$write = fopen($cache_ig, 'w') or die("can't write to file");
while(($line = fgets($read)) !== false)
{
if(stristr($line,$search))
{
fwrite($write, $line . "\n");
}
}
fclose($write);
fclose($read);
An alternative solution would be to use a instead of w at your fopen since w will:
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
While a will:
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Which would allow your code to work as is without any changes but the line:
$fh = fopen($cache_ig, 'w') or die("can't open file");
To:
$fh = fopen($cache_ig, 'a') or die("can't open file");
I am a php beginner.I want every time when the web page is open to create a file that does not exist. But every time when I run the program I have an error teling me that the file was not created.
This is my code:
$ip=$_SERVER["REMOTE_ADDR"];
if(!isset($_COOKIE['firsttime'])){
setcookie('firsttime', 'no');
$myfile = 'file/form'.$ip.'.txt';
if(file_exists($myfile) == FALSE){
$fo = fopen($myfile, 'w');
$code = '<form action = "" method = "post">';
fwrite($fo, $code);
fclose($fo);
}else{
unlink($myfile);
$file = new File();
}
}
where is my mistake?
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
Do this to open a file, and it will create the file if it doesn't exist.
Not completely sure but that would result in a pretty weird file name.
$myfile = 'file/form'.$ip.'.txt';
If my ip is 1.0.0.01.23 (really random and pretty weird) the file name would be:
file/form.1.0.0.01.23..txt
Try saving a file with that name in notepad.
Im trying to read and write to/from the same file, is this possible?
Here is what I am getting negative results with:
<?php
$file = fopen("filename.csv", "r") or exit("Unable to open file!");
while (!feof($file)) {
$line = fgets($file);
fwrite($file,$line);
}
fclose($file);
?>
You're opening the file in read-only mode. If you want to write to the file as well, do
fopen("filename.csv", "r+")
You'll need to open the file with more 'r+' instead of just 'r'. See the documentation for fopen: http://php.net/manual/en/function.fopen.php
You opened the file in "read only" mode. See the docs.
$file = fopen("filename.csv", "r+") or exit("Unable to open file!");