TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Need help editing/saving text files (http://www.talkphp.com/absolute-beginners/1731-need-help-editing-saving-text-files.html)

webosb 12-13-2007 09:17 PM

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.

ReSpawN 12-13-2007 09:46 PM

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

sketchMedia 12-13-2007 10:12 PM

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.

ReSpawN 12-13-2007 10:18 PM

Thats why you can use;
PHP Code:

$fh fopen($filename'a'); 


ReSpawN 12-13-2007 10:28 PM

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.

webosb 12-14-2007 04:45 PM

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?

Matt83 12-14-2007 06:43 PM

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


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

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0