Jump to content


Photo

working with .php files.


  • Please log in to reply
5 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 03 October 2006 - 12:21 PM

Ok. say I have a file.
I need to take that file, work with it, and save it, as another copy using php.

I use get_file_contents, or file open to get the file open, then I can make my changes, then I Have to resave it, is this with file copy or something?

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 01:06 PM

does this help?

$infile = fopen("myfile.txt", "r") // as readonly
while (!feof($infile))
{
$buffer = fgets($infile, 2048); // or $buffer = fread($infile.....
}
fclose($infile);

// do whatever you want

$outfile = fopen("mynewfile.txt", "w") // open writeable
fwrite($outfile, $buffer)
fclose ($outfile);

cheers,
tdw
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 03 October 2006 - 01:23 PM

$infile = fopen("myfile.txt", "r") // as readonly
while (!feof($infile))
{
 $buffer = fgets($infile, 2048); // or $buffer = fread($infile.....
}
fclose($infile);

// do whatever you want

$outfile = fopen("mynewfile.txt", "w") // open writeable
fwrite($outfile, $buffer)
fclose ($outfile);
Now to break this down.
So the open, that is opening the file as read only.  I got that part.
Then
while (!feof($infile))
{
 $buffer = fgets($infile, 2048); // or $buffer = fread($infile.....
}
I can look up end of file function on php.net, but what would the difference be in fgets and file_get_contents
Which would be better Also what is the 2048,
I appreciate the help so far, thanks.
It looks like the top part opens the file, saves the info in a variable.
then closes the file
then it attempts to oepn another file, if one doesn't exist then obviously it creates it, then it writes that information to it, and that's it right.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 01:38 PM

hi

well fread reads a total amount up to length, and fgets reads it line by line

so if you want to read it all in one go you can use
$buffer = fread($infile, filesize("myfile.txt")); // or use a variable for the filename

or to only read a portion of it you can specify the number of bytes to read: (2kbs in this example)
$buffer = fread($infile, 2048); // or use a variable for the filename

or to read the file line by line so you can deal with each line while reading it, you use fgets and specify the number of bytes to read (in that line): (2kbs in this example)
$buffer = fgets($infile, 2048); // or use a variable for the filename

i dont know about get_file_contents

and then yes, the "w" in fopen means will attempt to create it, so check your permissions in the folder

$outfile = fopen("mynewfile.txt", "w")

cheers,
tdw
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 03 October 2006 - 01:40 PM

I will play with this, I have to come up with something rather wierd, it has to be a cms added into each one of those pages, but now I can build the framework for the filehandling, and go from there, thanks

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 03 October 2006 - 01:46 PM

Well I did some modifications, I am going to have to instead carry all of this information over into paypal's IPN, and then do all this stuff there I think.  I will have to store the text, and template they choose into a session, and let the stuff that comes back from paypal deal with it.  I can only hope that when paypal sends the ipn back to the script that I can have the sessions available to do what I need.  If not, I can pass it all through paypal, I think that would be the best time to do all of this, so I can have a full system built.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users