 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-13-2007, 08:17 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 35
Thanks: 21
|
Need help editing/saving text files
I'm trying to be able to save to a text file
say if my text file contained:
define("NAMEOFAPP", "SOME APP");
define("KEYWORDS", "Keyword,Stuff,Etc");
define("DESCRIPT", "This is my description.");
define("SOMETEXT", "Blah Blah Blah");
I want to know how I can save over these. Not sure how to do this as I only know how to read, write, and append to text files.
__________________
one outstanding employee does more and costs less than two adequate performers.
|
|
|
|
12-13-2007, 08:46 PM
|
#2 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 458
Thanks: 49
|
You can, with ease. There are a number of functions available for preforming actions on files.
This site is where I gained my starters knowledge for PHP about 4 years ago. 2003 I believe. Anyways, it has the simple, down-to-earth knowledge you need.
For now, that's how I am going to conclude this post, but I will write a tutorial for you. Hopefully the PHP Guru's here can give me some feedback as well. Being it my first tutorial for PHP - ever.
Hopefully you'll understand the basic and expand the script on your own. If not, let me know.
Oh yeah, on a final note; you have to click continue to go through every basic function of the file.
File Create
File Open
File Close
File Write
File Read
File Delete
File Append
File Truncate
File Upload
Mark
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
|
The Following User Says Thank You to ReSpawN For This Useful Post:
|
|
12-13-2007, 09:12 PM
|
#3 (permalink)
|
|
The Gregarious
Join Date: Oct 2007
Location: Manchester, UK
Posts: 718
Thanks: 29
|
I'm guessing you mean that you want to rewrite the file to have different values for each named constant?
if it was me i'd just truncate the exsiting file, then write to the file with the new constants
PHP Code:
$fh = fopen('file', 'w');
the 'w' places the file pointer at the beginning of the file and then truncates the file to zero length.
__________________
sudo chown -R us ./allyourbase
|
|
|
|
12-13-2007, 09:18 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 458
Thanks: 49
|
Thats why you can use;
PHP Code:
$fh = fopen($filename, 'a');
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
12-13-2007, 09:28 PM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 458
Thanks: 49
|
First, on your FTP, you're going to make a map. Name it whatever your want. I am not sure, but CHMOD it to 777 (all) for sure. Same goes for webosb.txt.
Then open the file index.php and if it returns succes, you're good. Else, you gotta fix 1. the filename or 2. the content.
If it totally isn't writeable, CHMOD everything to 777.
http://markernst.com/lollercopte/index.php
http://markernst.com/lollercopte/webosb.txt
PHP Code:
<?php
$filename = 'webosb.txt'; $wContent = "define('MY', 'MY');\n"; $wContent .= "define('FIRST', 'FIRST');\n"; $wContent .= "define('PHP', 'PHP');\n"; $wContent .= "define('TUTORIAL', 'TUTORIAL');\n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // First we're going to open $filename, defined at the top. // Then, with fopen, we're opening it with 'a' => append mode. // !$handle (if it can't be opened), display the error. // If it fails, display the error and exit. if (!$handle = fopen($filename, 'w')) { exit('Failed. Cannot open '.$filename.'.'); } // Attempt, if the upper IF is true, to write the $wContent into the $handle // If it fails, display the error and exit. if (!fwrite($handle, $wContent)) { exit('Failed. Cannot write to '.$filename.'.'); } // If both checks, check out, continue and display the succes message. echo 'Succes. Written; <br /><i>'.$wContent.'</i><br /> to <strong>'.$filename.'</strong>'; fclose($handle); } else { echo $filename.' is not writeable.'; } ?>
Oh yeah, change the 'w' to 'a' if you wish to write BELOW the existing text.
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
|
The Following User Says Thank You to ReSpawN For This Useful Post:
|
|
12-14-2007, 03:45 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 35
Thanks: 21
|
I forgot to mention that the text file has a ton of other stuff in it... I guess my question is how do i locate the lines that contain those listed above and save over them in the same spot without affect any other text.
I guess i'm looking for a find and replace method.
What i was trying to do was create an editable config.php -- where I can load the values into the form which seems easy but I'm having trouble replacing those values. Is this practical?
__________________
one outstanding employee does more and costs less than two adequate performers.
|
|
|
|
12-14-2007, 05:43 PM
|
#7 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
|
Ok, here is a quick simple solution, im pretty sure there must be a better way to do this but anyways hope it helps you:
On one side theres test.txt (CHMOD this file to 777)
Quote:
|
I added other random stuff just to simulate that there's more data than the one we are updating
|
Code:
define("NAMEOFAPP", "SOME APP");
define("KEYWORDS", "Keyword,Stuff,Etc");
define("DESCRIPT", "This is my description.");
define("SOMETEXT", "Blah Blah Blah");
$szVar = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
$iSum = 1 + 2;
define("SOMETHING","Another thing");
And then the little script:
PHP Code:
// Define an array with the new values you want to write. $aNewValues = array( "NAMEOFAPP" => "This is the new name of my APP", "KEYWORDS" => "OTHER Keywords,Stuff,Etc", "DESCRIPT" => "This is my NEW description.", "SOMETEXT" => "A NEW Blah Blah Blah", );
$fName = 'test.txt'; $fHandle = fopen($fName,"r"); $szContent = fread($fHandle,filesize($fName));
// Simple foreach with regEX to find/replace the content foreach ($aNewValues as $key => $value) {
$szNew = 'define("'.$key.'", "'.$value.'");'; $szContent = preg_replace("/define\(\"".$key."\"\,\s?\"[A-Z,\s.]*\"\)\;/i",$szNew,$szContent); };
//Save the new content and close $fHandle = fopen($fName,"w"); fwrite($fHandle,$szContent); fclose($fHandle);
Cheers
|
|
|
|
|
The Following User Says Thank You to Matt83 For This Useful Post:
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|