Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 07-02-2006, 07:17 PM fwrite to php?
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
Hi

quick question ( i hope)
can you use fwrite to write to the end of a php document?
if not what could you use?
I am new to php, so if this is a dumb question i am sorry
atomicshockwave is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-02-2006, 07:19 PM Re: fwrite to php?
stOx's Avatar
Machine

Latest Blog Post:
Worlds Smallest Car - Peel P50
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
Trades: 0
yeah php can be used to write to any file.
should be carefull what is written to it though or you would be effectively letting people white thier own scripts on your server.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
stOx is offline
Reply With Quote
View Public Profile Visit stOx's homepage!
 
Old 07-02-2006, 07:26 PM Re: fwrite to php?
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
Quote:
Originally Posted by stOx
yeah php can be used to write to any file.
using fwrite?
Quote:
Originally Posted by stOx
should be carefull what is written to it though or you would be effectively letting people white thier own scripts on your server.
believe it or not, thats what i want
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 07-02-2006, 07:55 PM Re: fwrite to php?
atomicshockwave's Avatar
Experienced Talker

Posts: 38
Trades: 0
anouther thing...
fwrite adds the code to the end of the doc.
is there a way to add the content to anouther part of the doc? as in after line43 add content?

thanks
atomicshockwave is offline
Reply With Quote
View Public Profile
 
Old 07-02-2006, 11:26 PM Re: fwrite to php?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
When you write to the file, you will first need to fopen() the file, and depending on which mode you use, it will place the file pointer (where it writes the file data) at different places.

"r+" will place the pointer at the beginning of the file if you want to write content at the top.

"a" will place the pointer at the end of the file if you want to write content at the end.

More on this at: http://us2.php.net/manual/en/function.fopen.php

If you want to go by line numbers, you can first read the file into an array by using file(). Each line will be an array element, but the array keys will start at 0. If you want to add content after line 43, you would have to referr to key number 42 (which would be line 43).
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-03-2006, 05:56 AM Re: fwrite to php?
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
You can access specific parts in the page using the fseek methods.

http://uk.php.net/fseek

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 07-03-2006, 01:49 PM Re: fwrite to php?
daddy2five's Avatar
Skilled Talker

Posts: 77
Name: Daniel
Location: Stony Point , Noth Carolina
Trades: 0
OK, This is very interesting. Let's say I have a text doc. And it has alot of content, lets say 300 lines. And I wanted just a portion of this text doc to be copied to another file. How do i set the pointer to lets say line 150 and copy the content to lets say line 200 and put it in another text file. Is this possible ?
__________________
What is Yuwie?

Please login or register to view this content. Registration is FREE

Last edited by daddy2five; 07-03-2006 at 01:50 PM..
daddy2five is offline
Reply With Quote
View Public Profile Visit daddy2five's homepage!
 
Old 07-03-2006, 02:50 PM Re: fwrite to php?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Ibbo's suggestion would work, but here is how I would personally do it myself:

PHP Code:
<?php
  
  
// Read the original text document into an array
  
$file_contents file('text_document.txt');
  
  
$fwrite '';
  for (
$i 0$i count($file_contents); $i++) {
    
    
// Copy contents only on lines 150 - 200
    
if ($i >= 149 && $i <= 199$fwrite .= $file_contents[$i];
  }
  
  
// Write content in new text file
  
$fp fopen('new_document.txt''w');
  
fwrite($fp$fwrite);
  
fclose($fp);
  
?>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 07-03-2006 at 02:51 PM..
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-03-2006, 03:42 PM Re: fwrite to php?
daddy2five's Avatar
Skilled Talker

Posts: 77
Name: Daniel
Location: Stony Point , Noth Carolina
Trades: 0
Very nice! I tested it. It works. Mgraphic, you know your php! I am learing so much here! The for statement makes sense to me. I guess there are other ways of achieving the same thing, I'm going to check out some other ways of doing this. Thanks again.
__________________
What is Yuwie?

Please login or register to view this content. Registration is FREE
daddy2five is offline
Reply With Quote
View Public Profile Visit daddy2five's homepage!
 
Old 07-03-2006, 07:19 PM Re: fwrite to php?
Super Talker

Posts: 144
Trades: 0
sure u can. just use fopen with the 'a' flag. stands for writing only, and the pointer is placed at the end of the file. so anything u frwrite, will be appended to that file.

http://us3.php.net/manual/en/function.fopen.php
__________________
create.vibe

Please login or register to view this content. Registration is FREE
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 07-03-2006, 07:20 PM Re: fwrite to php?
Super Talker

Posts: 144
Trades: 0
woah that was weird, the forum showed me that were no replies... oh well
__________________
create.vibe

Please login or register to view this content. Registration is FREE
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Reply     « Reply to fwrite to php?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32321 seconds with 12 queries