Info: I am trying to read and write to a file at the same time, but if i have r+ it will only write and if i only have r it will only read, but i need it do both so it can get the info into the document and the info out of it to a cmd window.
Question: Is there i way i can do this and that both will work?
$outputMXA = shell_exec($commandMXA);
$fhMXA = fopen('MXA.txt','r+');
fwrite($fhMXA,$outputMXA);
$MXA = fgets($fhMXA);
Try this :
$outputMXA = shell_exec($commandMXA);
$fhMXA = fopen('MXA.txt','rw');
fwrite($fhMXA,$outputMXA);
$MXA = fgets($fhMXA);
fclose($fhMXA);
Edit:
Or you must separate both actions :
$outputMXA = shell_exec($commandMXA);
$fhMXA = fopen('MXA.txt','w+');
fwrite($fhMXA,$outputMXA);
fclose($fhMXA);
$fhMXA = fopen('MXA.txt','r+');
$MXA = fgets($fhMXA);
fclose($fhMXA);
Related
Im very new to programming and this is the first time i use PHP.
Im working on a webpage to switch 433mhz relays and show their current state.
But to know which switch is on or off you need to remember it when the button is clicked. So i thought of a States.txt file and update a line from 0 to 1. (or from 1 to 0)
I can already read a specific line, but when i try to write on a specific rule using "file_put_contents" it reads the line, and writes to a new file called like the rule.
$file = "States.txt";
$lines = file($file);
$btn1 = $lines[0];
$btn2 = $lines[1];
$btn3 = $lines[2];
$btn4 = $lines[3];
$btn5 = $lines[4];
$btn6 = $lines[5];
//file_put_contents($file, "test123");
file_put_contents( $lines[2] , "test123");
It should overwrite the specified rule, but instead it reads the line, and creates a new file called like the content of that line.
$file = "States.txt";
$lines = file($file);
$lines[2] = 'NEW VALUE' . PHP_EOL;
file_put_contents($file, $lines);
I have a Subversion server running with AuthzSVNAccessFile specified in a virtual host.
The file "permissions.txt" should be edited by the users themselves to add or remove team members but limited to max. 4 members. Possible via PHP and how to implement it?
In PHP each logged in user has the $username = user_x; whereas x is an integer.
The AuthzSVNAccessFile "permissions.txt":
[repo_1001:/]
user_1 = rw
# the lines below only editable by user_1
user_1-member_1 = r
user_1-member_2 = r
user_1-member_3 = r
user_1-member_4 = r
[repo_1002:/]
user_2 = rw
# the lines below only editable by user_2
user_2-member_1 = rw
user_2-member_2 = rw
Reading and writing the file in PHP:
$file_permissions = "/.../permissions.txt";
if (isset($_POST) && ...) {
$textFileContents = $_POST['textFileContents'];
$fd=fopen($file_permissions ,"w");
fwrite($fd, $textFileContents);
fclose($fd);
}
$fd = fopen($file_permissions,"r");
$textFileContents = fread($fd,filesize($file_permissions));
fclose($fd);
var_dump($textFileContents);
I tried "AbraCadaver" mentioned parse-ini-file function but I do not get any output.Even no errors are seen:
$file_permissions = "/.../permissions.txt";
$textFileContents = parse_ini_file($file_permissions);
var_dump($textFileContents);
The output is just "NULL".
ADD: The cause why I got "NULL" is that my php.ini on the remote server was configured to disable the function parse_ini_file for security reasons.Now it works!
I'm relatively new to PHP and I'm trying to get a small script running. I have a VB .net program that posts data using the following function.
Public Sub PHPPost(ByVal User As String, ByVal Score As String)
Dim postData As String = "user=" & User & "&" & "score=" & Score
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://myphpscript"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.ContentLength = byteData.Length
Dim postReqStream As Stream = postReq.GetRequestStream()
postReqStream.Write(byteData, 0, byteData.Length)
postReqStream.Close()
End Sub
Where "myphpscript" is acutally the full URL to the PHP script. Basically I'm trying to POST the "User" variable and the "Score" variable to the PHP script. The script I've tried is as follows:
<?php
$File = "scores.rtf";
$f = fopen($File,'a');
$name = $_POST["name"];
$score = $_POST["score"];
fwrite($f,"\n$name $score");
fclose($f);
?>
The "scores.rtf" does not change. Any help would be appreciated. Thanks ahead of time, I'm new to PHP.
Ensure that your script is receiving the POST variables.
http://php.net/manual/en/function.file-put-contents.php
You can try file_put_contents, it combines the use of fopen ,fwrite & fclose.
It could be wise to use something like isset/empty to check that there is something to write before writing.
<?php
$file = 'scores.rtf';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= print_r($_POST);
//Once confirmed remove the above line and use below
$current .= $_POST['name'] . ' ' . $_POST['score'] . "\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
Also, totally overlooked the RTF part, definitely look into what Mahan mentioned. I'd suggest the above if you have no need for that specific file type.
The "scores.rtf" does not change.
well RTF files is handled differently because its not really a pure text file it contains meta-data and tags that controls how text is displayed on the rtf file. please have a time to read to the following sources
http://www.webdev-tuts.com/generate-rtf-file-using-php.html
http://b-l-w.de/phprtf_en.php
http://paggard.com/projects/doc.generator/doc_generator_help.html
if in any case you want a normal text file you can use the code below, don't use fwrite(), please use file_put_contents()
file_put_contents("scores.txt", "\n$name $score");
Am developing an admin center where I can edit configuration files (written in PHP). I do NOT want to store these values in a mySQL table (for various reasons). So say my config.php has contents like:
<?php
$option1 = 1;
$option2 = 2;
$option4 = 5;
$option7 = array('test','a','b',c');
?>
Now say in one of the admin pages I will only be changing a few values like option2 or option4 etc. Any ideas on what would be the best way to go about this.
I know one option is to read the PHP file completely and write parts of it using REGEX. Any way to make this more efficent? I don't want the config.php file to break because of some error on the user's end. Any ideas on how to ensure that it works?
If you have some liberty about the way you store configuration values, you may use ini files.
All you have to do is load the content of the ini file in an array with parse_ini_file, then modify values in that array and finally overwrite the file with new values, as described in this comment.
For obvious security reasons it's a good idea to place those files out of your document root.
sample content of ini file :
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
sample code (using safefilewrite function) :
<?php
$ini_file = '/path/to/file.ini';
$ini_array = parse_ini_file($ini_file);
$ini_array['animal'] = 'CAT';
safefilerewrite($file, implode("\r\n", $ini_array));
?>
var_export() is probably the function you're looking for.
You can write/read the settings to a file using the following code:
$content = array();
//fill your array with settings;
$fh = fopen ( $bashfile, 'w' ) or die ( "can't open file" );
fwrite ( $fh, $content );
fclose ( $fh );
to read it you use:
file_get_contents() //this will return a string value
OR
Line by line:
$lines = file('file.txt');
//loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
print "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
I have a file called config.php, I want it to remain exactly as is, however on Line # 4 there is a line saying:
$config['url'] = '...could be anything here';
I'd like to only replace the contents of line # 4 with my own url provided for $config['ur'], is there a way to do this in PHP?
Since you know the exact line number, probably the most accurate way to do this is to use file(), which returns an array of lines:
$contents = file('config.php');
$contents[3] = '$config[\'url\'] = "whateva"'."\n";
$outfile = fopen('config.php','w');
fwrite($outfile,implode('',$contents));
fclose($outfile);
$myline = "my confg line";
$file = "config.php";
$contents = file($file);
$contents[3] = $myLine;
$file = implode("\n", $contents);
Either create another config (myconfig.php). Include the original and overwrite the option. include myconfig.php instead of the original one.
OR
Go to where config is included (you did use a single place for all your includes right?) and set the config option there.
include "config.php"
$config['url'] = "my_leet_urlz";
you could read the file, use str_replace or preg_replace on the appropriate strings, then write the file back to itself.
$filename = "/path/to/file/filename";
$configFile = file($filename);
$configFile[3] = '$'."config['url'] = '...could be anything here';";
file_put_contents($filename ,$configFile);
If there is only one $config['url'], use a preg_replace()
http://us.php.net/preg_replace
something like:
$pattern = '\$config['url'] = "[\d\w\s]*"'
$file_contents = preg_replace($pattern, $config['url'] = "my_leet_urlz";,$file_contents);
you'll need to fix the pattern as I'm new at regex and I know that's not right.