Results 1 to 15 of 15
Thread: PHP : Reading text files ?
-
11-12-2009, 11:19 AM #1Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
PHP : Reading text files ?
Hi
I need a help.
This is the sample of Nginx web server configiraton.
Code: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
-
11-12-2009, 11:22 AM #2Web Hosting Guru
- Join Date
- Sep 2006
- Location
- Dallas, TX
- Posts
- 333
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.
-
11-12-2009, 11:26 AM #3Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
-
11-12-2009, 11:30 AM #4Web Hosting Guru
- Join Date
- Sep 2006
- Location
- Dallas, TX
- Posts
- 333
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.
-
11-12-2009, 11:30 AM #5Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
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
Harris
-
11-12-2009, 11:31 AM #6Newbie
- Join Date
- Nov 2009
- Posts
- 5
try using google tizag.com/phpT/fileread.php be4 u start abusing members that r trying to help you
-
11-12-2009, 11:35 AM #7Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
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
-
11-12-2009, 11:39 AM #8Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
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*
-
11-12-2009, 11:41 AM #9Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
-
11-12-2009, 11:44 AM #10Newbie
- Join Date
- Nov 2009
- Posts
- 5
what
well if u went thru that page and read it properly u would see it shows u how to retrieve data using fget
-
11-12-2009, 11:45 AM #11Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
-
11-12-2009, 11:51 AM #12Newbie
- Join Date
- Nov 2009
- Posts
- 5
$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;
-
11-12-2009, 11:54 AM #13Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
-
11-12-2009, 12:04 PM #14Junior Guru
- Join Date
- Nov 2009
- Posts
- 194
mattle , Just wanted to tell you that I got what I needed ( without your help :p )
PHP Code:<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>
Code: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
PHP Code:$pieces = preg_split('/[ ;]/', $string, -1, PREG_SPLIT_NO_EMPTY);
Code: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] => } )
-
11-12-2009, 12:33 PM #15Web Hosting Master
- Join Date
- Mar 2009
- Posts
- 2,222
This may suffice, but it will also print out lines containing, say, "root2" and so on.
Code:<?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); } ?>
Similar Threads
-
PHP cannot read Chinese Text Files
By Gigaron in forum Programming DiscussionReplies: 7Last Post: 11-19-2008, 03:14 AM -
Reading config files
By BostonGuru in forum Programming DiscussionReplies: 1Last Post: 11-20-2006, 01:25 AM -
php forms and arrays reading from text files
By alpha883 in forum Programming DiscussionReplies: 2Last Post: 06-06-2005, 05:02 AM -
PHP: Reading data from multiple text boxes?
By frogb0x in forum Programming DiscussionReplies: 2Last Post: 09-14-2004, 03:17 AM -
writing to and reading from files
By billie493 in forum Programming DiscussionReplies: 1Last Post: 03-20-2004, 01:46 PM