Jump to content

Create a text file and save it as title name?


ztealmax

Recommended Posts

Hi if i want to create a new text file and edit it,  how do i save it as the title name of that text document?

right now i have this script:

edit_form.php
[code]<form action="edit_process.php" method="post">
<textarea rows="25" cols="40" name="content">
<?
$fn = "news/news.txt";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea><br>
<input type="submit" value="Change!">
</form>[/code]

edit_process.php
[code]<?
$fn = "news/news.txt";
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=read.php\" />\n";
?>[/code]
As you notice i use a predefined name in this, but instead of that i would like to use
A title as the save name:

for example if i have the edit box
i also would like a title box
and somehow get the titlebox name as the save file name

Is that possible?

//cheers
Consider you have a textbox named "title" and it is posted to edit_process.php via $_POST['title']

code:
[code=php:0]
$fn = "news/" . $_POST['title'];
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
[/code]

I am pretty sure if the file does not exist, fopen creates it. If not, then you will need to use file_exists and create the file before opening it.

[quote author=keeB link=topic=117014.msg477149#msg477149 date=1164998880]
Consider you have a textbox named "title" and it is posted to edit_process.php via $_POST['title']

code:
[code=php:0]
$fn = "news/" . $_POST['title'];
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
[/code]

I am pretty sure if the file does not exist, fopen creates it. If not, then you will need to use file_exists and create the file before opening it.


[/quote]

okej, ive done this so far:

edit_process.php
[code]<?
$fn = "news/" . $_POST['title'];
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=read.php\" />\n";
?>[/code]

edit_form.php
[code]<form action="edit_process.php" method="post">
<textarea rows="1" cols="40" name="title">
TITLE
</textarea>
<textarea rows="25" cols="40" name="content">
<?
$fn = "news/news.txt";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea><br>
<input type="submit" value="Change!">
</form>[/code]

how ever what should it say in [b]$fn = ""[/b] in edit_form.php

and how do i add .txt to the title name?
For example if i name the file testar, now it saves it as testar but i would like it to save it as testar.txt

//Thanx :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.