FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Using php to change content of a .txt





Relentless
I am new to PHP and I am not familiar with its functions...

Is it possible to use a PHP based form to edit the contents of a txt file?

-----

Let me tell you the situation...

I am making a Clients Area and I am using PHP pages to import text via

<?php include("username.txt"); ?>

So instead of me having to change the whole web page for a new account I just edit the .txt file and it imports to the site. e.g. Username.txt

I use this for Forms & on site reference. To change every form for each user would take ages...

So this is easier for me. Obviously I have more .txt files to edit, Something like 20. With own individual name

-----
Unless anybody else has an easier way for importing data please let me know even I was to use one .txt file (doesn’t even have to be a text file) and change the <php include function>

Or another way to solve this is to have a PHP form which I type the future content of a selected .txt into the text box, click submit and the contents of the .txt file has changed…

-----
Hope somebody can help

Dan…
sonam
Are this txt files same with different names or different in content?

Sonam
Relentless
Thanks for your reply, (helping me again lol)

Responce:

both are diffrent,

I use txt file for diffrent things so normally hold one word or one sentance in each under a diffrent file name.

So they have diffrent names & diffrent contect, Ready for the PHP import script to disply the contents on the page...

----

I want to be able to change the content in the text files without, having to open the text files and change them myself.

And if there is a way to put all the text files into one and ask the php script to take each line or the text file or certain text, that would make my life easier.

But it doesn't have to be a text file if another format works better...

--
Dan
Relentless
Could this help?

http://www.tizag.com/phpT/fileappend.php

I don't understand php

--
Dan...
lszanto
Ok maybe i've understood this totally wrong but you want a client name stored with some data about the client? And if this is what your talking about might I suggest using a database? It would be alot more secure that a .txt file.
sonam
Quote:
So they have diffrent names & diffrent contect, Ready for the PHP import script to disply the contents on the page...


When I think different content I think something totaly different (e.g. one have story, one images, one news). If you have DB in txt file (like: name, surname, birth, etc) then we can do something if this entries follow one important rule - same line for the same think.

Sonam
Relentless
Don't worri guys its hard to explain what I have done without seeing it spose...

If you really are intresed in helping then please post back saying so... and I am not to fussed about that matter...

----
What I did want to know was if there was any PHP forms out there that when I enter text into a text area, on submit it changes the content of a select .txt file.

Would help if the PHP script was to delete excisting text in the document.

Thanks Dan.
Fire Boar
Then use file writing instead of file appending. Look up the fwrite function.

I would recommend that you use a database instead though - it's much more geared towards this sort of thing and won't clutter your filesystem up.
sonam
Quote:
What I did want to know was if there was any PHP forms out there that when I enter text into a text area, on submit it changes the content of a select .txt file.


This is quite simple to code but you must create one password protected directory (in CPanel or DirectAdmin), otherwise anyone who will reach this php file can change your data.

Sonam
mahirharoon
use fwrite or fileappend
Relentless
sonam wrote:
Quote:
What I did want to know was if there was any PHP forms out there that when I enter text into a text area, on submit it changes the content of a select .txt file.


This is quite simple to code but you must create one password protected directory (in CPanel or DirectAdmin), otherwise anyone who will reach this php file can change your data.

Sonam


Yes the php code will be in a passworded directory...

Please tell me how I should use the code...

I do not know how to write php even if i did find it lol

Dan...
sonam
First create one passworded directory in your root folder (public_html). Then just copy/paste this code below and save as... file (whatever name) with php extension. In third line type the name of folder where is your txt files (don't remove ../). Run your new file in explorer and you are done. Script will grab all your txt files in chosen folder. Also, script will exclude all other type of files and directories. OK, here is script:

Code:
<?php
session_start();
$dir = "../textfiles"; // type between quotes folder name where are stored your txt files
$chosen = @$_POST['select'];
$chosenfile = $dir . "/" . $chosen;

// open chosen file
if($chosen != "" && $_POST['findfile'] == "GO") {
   $file = file_get_contents($chosenfile);
   list($name, $rest) = explode(".", $chosen);
   $name = str_replace("_", " ", $name);
// change cols and rows below if you want bigger or smaller textbox
   $form = "<h2>$name</h2><textarea name=\"mytext\" cols=\"100\" rows=\"15\">" . $file . "</textarea><input type=\"hidden\" name=\"hidden\" value=\"$chosenfile\" /><br /><input type=\"submit\" name=\"change\" value=\"Change\" />&nbsp;&nbsp;&nbsp;<input type=\"submit\" name=\"cancel\" value=\"Cancel\" />";
   $_POST['findfile']= "";
   
// change chosen file
} elseif($_POST['hidden'] != "" && $_POST['change'] == "Change"){
$newtext = $_POST['mytext'];
$chosenfile = $_POST['hidden'];
$newdata = fopen($chosenfile, "wb");
   fwrite($newdata, $newtext);
   fclose($newdata);   
   $_POST['change'] = "";
   $form = $_SESSION['change_txt_form'];

// first time opened php read directory with txt files
} else {
    $openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   if($fileNames == "." || $fileNames == ".." || is_dir("$dir/$fileNames")) {
                continue;
           } else {
              list($mainName, $rest) = explode(".", $fileNames);   
             if($rest != txt) {
                 continue;
             } else {
                 $mainName = str_replace("_", " ", $mainName);
                 $txtfiles = @$txtfiles . "<option value=$fileNames >$mainName</option>";
             }            
           }
    }
$form = "<select name=\"select\">" . $txtfiles . "&nbsp;&nbsp;</select><input type=\"submit\" name=\"findfile\" value=\"GO\" />";
$_SESSION['change_txt_form'] = $form;
}
?>
<html>
<head>
<title>My txt files</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="txtfiles" method="post" action="">
  <?php echo $form; ?>
</form>
</body>
</html>


Sonam
Relentless
WOW i just tried the form out its great. Exactly what i wanted!

But can you add somthing for please?

Can i have a drop down menu of which user data I change?

every directory past "/user/" is a user account.

So somethin like /user/$account/files/

Thanks Dan
Dan...
Relentless
AH, problem.

I just tried editing a txt file.

and got this
Code:
Warning: fopen(/home/relentle/domains/myhost.frih.net/public_html/mydirectory/textfiles/username.txt) [function.fopen]: failed to open stream: Permission denied in /home/relentle/domains/myhost.frih.net/public_html/mydirectory/fileeditor.php on line 20

Warning: fwrite(): supplied argument is not a valid stream resource in /home/relentle/domains/myhost.frih.net/public_html/mydirectory/fileeditor.php on line 21

Warning: fclose(): supplied argument is not a valid stream resource in /home/relentle/domains/myhost.frih.net/public_html/mydirectory/fileeditor.php on line 22


I changed the folder permissions to 777 (to enable read & write)
sonam
You must also change txt files to 666 I think.

Quote:
Can i have a drop down menu of which user data I change?
every directory past "/user/" is a user account.
So somethin like /user/$account/files/


I don't understand exactly what do you mean. Did you want to tell me how you have few txt files in folder "files" for different user "$account"?

Sonam
Relentless
before the page where it asks me what file i wish to edit...

Can i a have a page which also scans for folders past /user/ and displays them in a drop down menu?

once i select that file it goes to /user/username/textfiles/

--

Example i have these directorys past /user/

Sonam
Relentle
example

When i open file editor.php
it will disply a dropdown menu. in the drop down menu will be
Sonam
Relentle
example

when i select

Relentle it goes to /user/Relentle/files/

then it displays the text files in /user/Relentle/files/

and this would be the same for sonam - /user/sonam/files/

and example /user/example/files/

Dont worri if you can't do this i can make another way of doing it. (just takes longer)

Thanks
Dan
sonam
I am PM to you new script. You don't need to touch anything. Put this script in some passprotected folder but not in "user" folder.

Sonam
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
How to get the tree structure done using PHP
Making a expression calculator using PHP.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.