Web Hosting Talk







View Full Version : PHP : Reading text files ?


SunShellNET
11-12-2009, 11:19 AM
Hi
I need a help.

This is the sample of Nginx web server configiraton.

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}



I want to write a php script for getting the value of "root" , "fastcgi_pass" and "fastcgi_index" from the file.

There may be hundreds of such blocks in the file and I want to collect all.

Is that possible with php ? and how ?


Harris

NeilAgg
11-12-2009, 11:22 AM
You can try opening the file with the fopen command:
http://php.net/manual/en/function.fopen.php

You will need to make sure the account running your PHP processor has access to the file.

After that, you should be able to read and parse the file in your own code.

SunShellNET
11-12-2009, 11:26 AM
You can try opening the file with the fopen command:
http://php.net/manual/en/function.fopen.php

You will need to make sure the account running your PHP processor has access to the file.

After that, you should be able to read and parse the file in your own code.

Please read my question before making nonsense replies.
I know how to open file, but I want to get certain values only.

NeilAgg
11-12-2009, 11:30 AM
In the original post, you did not state you were able to open the file so I assumed that is where you had the trouble.

Regarding writing code to extract the contents, read the file line by line and look for "root" , "fastcgi_pass" and "fastcgi_index" on each line. If you see one of those, parse the value and store it in an array.

SunShellNET
11-12-2009, 11:30 AM
For example,
I want to get the value of "root" which is "html"

Want to get the value of "fastcgi_pass" which is "127.0.0.1:9000"

I also would like to know how to replace those values into the file for saving it with new values


Something like THIS (http://php.net/manual/en/function.parse-ini-file.php)


Harris

loony
11-12-2009, 11:31 AM
try using google tizag.com/phpT/fileread.php be4 u start abusing members that r trying to help you

SunShellNET
11-12-2009, 11:35 AM
try using google tizag.com/phpT/fileread.php be4 u start abusing members that r trying to help you

I am not trying to abuse any of the member. But that guys seemed like he is in rush and typed that reply without reading and understanding my question or may be my english is bad :(

Anyways, I've explained more about it above, and you are also telling the same example. I know how to read the file, but I want to get values from the file, like php's ini parse



Harris

mattle
11-12-2009, 11:39 AM
Please read my question before making nonsense replies.
I know how to open file, but I want to get certain values only.

The title of your post is "PHP : Reading text files ?" It's not a stretch to assume you don't know how to read a text file from that. Don't be an ass to people who are trying to help you, because people like me who know the answer are just going to leave you hanging.

*plonk*

SunShellNET
11-12-2009, 11:41 AM
The title of your post is "PHP : Reading text files ?" It's not a stretch to assume you don't know how to read a text file from that. Don't be an ass to people who are trying to help you, because people like me who know the answer are just going to leave you hanging.

*plonk*

Alright. Never mind. It'll take some time, but I'll find it myself

Harris

loony
11-12-2009, 11:44 AM
well if u went thru that page and read it properly u would see it shows u how to retrieve data using fget

SunShellNET
11-12-2009, 11:45 AM
well if u went thru that page and read it properly u would see it shows u how to retrieve data using fget

Nop. That's not I wanted.

loony
11-12-2009, 11:51 AM
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$root = fgets($root);
$pass = fgets($fastcgi_pass );
$index = fgets($fastcgi_index );
fclose($fh);
echo $root;
echo $pass;
echo $index;

then change the txt file so that its a variable so $root = html;

SunShellNET
11-12-2009, 11:54 AM
then change the txt file so that its a variable so $root = html;

What does that mean ?
I can't change the file txt file content. Like it said, it is the configuration of Nginx Webserver. So if I change it then the web server may not work.

Harris

SunShellNET
11-12-2009, 12:04 PM
mattle , Just wanted to tell you that I got what I needed ( without your help :p )



<pre>
<?php
$file = 't.txt';
$handle = fopen($file, "r");
$string = fread($handle, filesize($file));
fclose($handle);

$pieces = preg_split('/ /', $string, -1, PREG_SPLIT_NO_EMPTY);
print_r($pieces);
?>

</pre>

output

Array
(
[0] => location
[1] => ~
[2] => \.php$
[3] => {

[4] => root
[5] => html;

[6] => fastcgi_pass
[7] => 127.0.0.1:9000;

[8] => fastcgi_index
[9] => index.php;

[10] => fastcgi_param
[11] => SCRIPT_FILENAME
[12] => /usr/share/nginx/html$fastcgi_script_name;

[13] => include
[14] => fastcgi_params;

[15] => }
)




And even better

$pieces = preg_split('/[ ;]/', $string, -1, PREG_SPLIT_NO_EMPTY);

semi column removed

Array
(
[0] => location
[1] => ~
[2] => \.php$
[3] => {

[4] => root
[5] => html
[6] =>

[7] => fastcgi_pass
[8] => 127.0.0.1:9000
[9] =>

[10] => fastcgi_index
[11] => index.php
[12] =>

[13] => fastcgi_param
[14] => SCRIPT_FILENAME
[15] => /usr/share/nginx/html$fastcgi_script_name
[16] =>

[17] => include
[18] => fastcgi_params
[19] =>

[20] => }
)



Harris

tim2718281
11-12-2009, 12:33 PM
This may suffice, but it will also print out lines containing, say, "root2" and so on.


<?php

$InputFile = $argv[1];
$handle = @fopen($InputFile, "r");
if ($handle) {
while (!feof($handle)) {
$InputLine = fgets($handle);
if (substr_count($InputLine, 'root') > 0) print $InputLine;
else if (substr_count($InputLine, 'fastcgi_pass') > 0) print $InputLine;
else if (substr_count($InputLine, 'fastcgi_index') > 0) print $InputLine;
}
fclose($handle);
}
?>