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);
Related
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")));
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 making a blog without using a database. So I have a add entry form which when submitted should be processed by a php file that creates files for the entries. My question is how do i store multiple files??
Thanks!
You can use this one:
Creates the file
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file
Write to a file
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);
Further information:more file handling in PHP
<?php
session_start();
$username = $_SESSION['username']; //this allows the user to return to blog and edit before submitting
$filename = $username . "/entry.txt";
$handle = fopen($filename, "a+");
$entrydata = fread($handle, filesize($filename)); //opens file for reading, and reads the file for any text that may already exist
fclose($handle); //close the handle
if (!empty($_POST['posted'])){ //maybe add this as a hidden field in your form to verify if the form was actually posted
$entry = $_POST['entry']; //saves text that has been entered in the entry form
$handle = fopen($filename, "w+");
if (flock($handle, LOCK_EX)){ //an exclusive lock to file
if (fwrite($handle, $entry) == FALSE){
echo "Cannot write to file";
}
flock ($handle, LOCK_UN); //release the lock
}
}
else{
//some code
}
I have a txt file with all the sql commands. I need to open the file, read the commands and execute against postgresql.
$file = fopen("createme.sql.txt", "r") or exit("Unable to open file!");
while(!feof($file))
{
pg_query($conn, fgets($file));
}
fclose($file);
I expected the above code to work. But is shows an error:
PHP Warning: pg_query(): Query failed:
How do I execute the commands from a text file?
fgets get file content line by line.
Using fread to get all content of file, set the second parameter to size of your files.
$fileName = "createme.sql.txt";
$file = fopen($fileName, "r") or exit("Unable to open file!");
// checking if opening file is error.
if(!feof($file))
{
$str = fread($file, filesize($fileName));
pg_query($conn, $str);
}
fclose($file);
Another trick thanks to #smassey
$fileName = "createme.sql.txt";
$str = file_get_contents($fileName);
pg_query($conn, $str);
fclose($file);
try trim() to cutoff "\r\n" from the end of each line
$file = fopen("createme.sql.txt", "r") or exit("Unable to open file!");
while(!feof($file))
{
pg_query($conn, trim(fgets($file)));
}
fclose($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.