TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-13-2007, 09:17 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default 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.
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 12-13-2007, 09:46 PM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
The Following User Says Thank You to ReSpawN For This Useful Post:
webosb (12-14-2007)
Old 12-13-2007, 10:12 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-13-2007, 10:18 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Thats why you can use;
PHP Code:
$fh fopen($filename'a'); 
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-13-2007, 10:28 PM   #5 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
The Following User Says Thank You to ReSpawN For This Useful Post:
webosb (12-14-2007)
Old 12-14-2007, 04:45 PM   #6 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

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?
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 12-14-2007, 06:43 PM   #7 (permalink)
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default

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
__________________
http://www.mattvarone.com
Matt83 is offline  
Reply With Quote
The Following User Says Thank You to Matt83 For This Useful Post:
webosb (12-14-2007)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:08 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design