Current location: Hot Scripts Forums » Programming Languages » PHP » write to text file


write to text file

Reply
  #1 (permalink)  
Old 07-01-08, 03:47 PM
winracer's Avatar
winracer winracer is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Georgia
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Rolleyes write to text file

how would you write to a text file with se input

what i want is for a user to type in website like http://example.com

and for it to write it to a text file
but i want the code to write a new text file for every url with the url name
like example-com.txt

any thoughs
Reply With Quote
  #2 (permalink)  
Old 07-02-08, 01:59 PM
trumnation trumnation is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
You could do it using php. If you want to go that route, I recommend you read up on the fopen and fwrite functions at php.net, as they are what you'll need to write to a text file. php.net will explain exactly what they do, how to use them, etc.

Hope that helps!
Reply With Quote
  #3 (permalink)  
Old 07-02-08, 02:06 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I see you are in the PHP forum, so you must have PHP.
Do you understand how $_POST and $_GET work?
__________________
Jerry Broughton
Reply With Quote
  #4 (permalink)  
Old 07-02-08, 02:59 PM
winracer's Avatar
winracer winracer is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Georgia
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
I have two php files now that i have made I can not get it to make new txt file each time any thought

1st one
PHP Code:

<html>
<body>
<form action="test.php" method="post">
Enter YouTuble URL:<input type="text" name="url"/>
<input type="submit" />
</form>
</body>
</html>

2nd one
<html>
<body>
Here is your YouTube url <?php echo $_POST["url"]; ?>.<br />

</body>
</html>
<?php
$filename 
'youtube.txt';
$somecontent $_POST["url"];

 
// Let's make sure the file exists and is writable first.
//if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle fopen($filename'a+')) {
echo 
"Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle$somecontent) === FALSE) {
echo 
"Cannot write to file ($filename)";
exit;
}

echo 
"Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

//} else {
//echo "The file $filename is not writable";
//}
?>

Last edited by Nico; 07-02-08 at 04:52 PM.
Reply With Quote
  #5 (permalink)  
Old 07-02-08, 03:17 PM
winracer's Avatar
winracer winracer is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Georgia
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
i would also like to do something like this.. put this in the middle some the codeto wite to the file $_POST["url"][/


somecontent = <object width="425" height="344">
<param name="movie" value="$_POST["url"]&hl=en&color1=0x006699&color2=0x54abd6">
</param>
<embed src="$_POST["url"]&hl=en&color1=0x006699&color2=0x54abd6" type="application/x-shockwave-flash" width="425" height="344">
</embed></object>
Reply With Quote
  #6 (permalink)  
Old 07-02-08, 04:15 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your going about this the wrong way.
Your opening the same file each time in append mode,
and appending the data to the file.
You want to open a file in write mode so it will create a new file each time.

Here is an example that will validate the URL before it creates the file.
If it doesn't validate, it won't create a file.
This will validate most common URL's but not all.
I am sure someone else can come up with a better regex.

Example:
PHP Code:

<html>
<head>
<style>
.td1_pad
{
 padding-right:10px;
 }
.td2_pad
{
 padding-top:10px;
 text-align:center;
 }
</style>
</head>
<body>
<?php
if(!empty($_POST["web_addr"]))
{
 if(
preg_match('^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$^',$_POST["web_addr"]))
 {
  
$file_name=str_replace(".","-",substr($_POST["web_addr"],7)).".txt";
  
$f=fopen($file_name,'w');
  
fwrite($f,$_POST["web_addr"]);
  
fclose($f);
  }
 else{echo 
"You didn't enter a web address.<br />";}
 }
?>
<form action="#" method="POST">
<table cellspacing="0" cellpadding="0">
 <tr>
  <td class="td1_pad">Enter web address here:</td><td><input type="text" name="web_addr" value="http://"></td>
 </tr>
 <tr>
  <td class="td2_pad" colspan="2"><input type="submit" value="Submit"></td>
 </tr>
</table>
</form>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 07-02-08 at 04:27 PM.
Reply With Quote
  #7 (permalink)  
Old 07-03-08, 12:07 PM
winracer's Avatar
winracer winracer is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Georgia
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
thanks. that works great. How would i add this in the text file where the $_POST["url"]& is the web address they typed in and the rest of the code is entered before and after. I gues then i could change the file from .txt to maybe .html.


<object width="425" height="344">
<param name="movie" value="$_POST["url"]&hl=en&color1=0x006699&color2=0x54abd6">
</param>
<embed src="$_POST["url"]&hl=en&color1=0x006699&color2=0x54abd6" type="application/x-shockwave-flash" width="425" height="344">
</embed></object>
Reply With Quote
  #8 (permalink)  
Old 07-03-08, 12:55 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If I understand you correctly, this should work, provided I didn't make any mistakes.
This will produce a .html file instead of a .txt file.
PHP Code:

<html>
<head>
<style>
.td1_pad
{
 padding-right:10px;
 }
.td2_pad
{
 padding-top:10px;
 text-align:center;
 }
</style>
</head>
<body>
<?php
if(!empty($_POST["web_addr"]))
{
 if(
preg_match('^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$^',$_POST["web_addr"]))
 {
  
$file_name=str_replace(".","-",substr($_POST["web_addr"],7)).".html";
  
$data="<object width='425' height='344'>
         <param name='movie' value='"
.$_POST["web_addr"]."&hl=en&color1=0x006699&color2=0x54abd6'></param>
         <embed src='"
.$_POST["web_addr"]."&hl=en&color1=0x006699&color2=0x54abd6' type='application/x-shockwave-flash' width='425' height='344'></embed>
         </object>"
;
  
$f=fopen($file_name,'w');
  
fwrite($f,$data);
  
fclose($f);
  }
 else{echo 
"You didn't enter a web address.<br />";}
 }
?>
<form action="#" method="POST">
<table cellspacing="0" cellpadding="0">
 <tr>
  <td class="td1_pad">Enter web address here:</td><td><input type="text" name="web_addr" value="http://"></td>
 </tr>
 <tr>
  <td class="td2_pad" colspan="2"><input type="submit" value="Submit"></td>
 </tr>
</table>
</form>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
  #9 (permalink)  
Old 07-03-08, 01:17 PM
winracer's Avatar
winracer winracer is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Georgia
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
thanks one last question I hope the http:// that a user inputs would look like

http://uk.youtube.com/watch?v=06gwgidNiic this get error writeing the file name.

maybe file name should be something else?

can i use a varable for file name?
Reply With Quote
  #10 (permalink)  
Old 07-03-08, 01:49 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by winracer View Post
thanks one last question I hope the http:// that a user inputs would look like

http://uk.youtube.com/watch?v=06gwgidNiic this get error writeing the file name.

maybe file name should be something else?

can i use a varable for file name?
What would you like the file name to be?
Other than the URL of course.
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
text box with scroll bar silvermane CSS 7 01-16-09 03:03 AM
Changing where Flash pulls text file - how? jdsmith8 Flash & ActionScript 4 08-02-06 10:12 AM
Can javascript be used to read and write to a text file kept on a webserver? gmb1994 JavaScript 4 11-23-04 10:50 AM
Excel document containing records, need to save as a text file to upload to mysql bearslife PHP 1 06-05-04 03:24 AM
How to write a php script to load a text file into a table automatically? xmxpcom PHP 2 02-12-04 08:37 AM


All times are GMT -5. The time now is 07:49 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.