Author Topic: Write to file  (Read 3100 times)

0 Members and 1 Guest are viewing this topic.

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Write to file
« on: October 04, 2010, 04:54:01 pm »
Here is a simple and quick way to write content to a file:

This will create or overwrite any existing file.
Code: [Select]
<?php
$FileHandle 
fopen("MyFile.txt"'w') or die("Cant open file!");
fwrite($FileHandle "This is a nice file!");
fclose($FileHandle);
?>


This will create or append any existing file:
Code: [Select]
<?php
$FileHandle 
fopen("MyFile.txt"'a+') or die("Cant open file!");
fwrite($FileHandle "This is a nice file!");
fclose($FileHandle);
?>

if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Zyrukas

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: Write to file
« Reply #1 on: November 26, 2010, 11:09:50 pm »
Old stuff, but it's cool, keep on :)

Offline iLike

  • /dev/null
  • *
  • Posts: 8
  • Cookies: 1
  • IRC Geek.
    • View Profile
Re: Write to file
« Reply #2 on: November 29, 2010, 07:23:55 pm »
The reason why the second one appends and the first one doesn't, is because of the
Code: [Select]
$FileHandle = fopen("MyFile.txt", 'a+') or die("Cant open file!"); line.
Notice the a+? That means: append & read.
w3schools has a list of all opening types (by the lack of better words, lol) available here

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Write to file
« Reply #3 on: November 29, 2010, 09:13:06 pm »
The reason why the second one appends and the first one doesn't, is because of the
Code: [Select]
$FileHandle = fopen("MyFile.txt", 'a+') or die("Cant open file!"); line.
Notice the a+? That means: append & read.
w3schools has a list of all opening types (by the lack of better words, lol) available here

r   Read only. Starts at the beginning of the file
r+   Read/Write. Starts at the beginning of the file
w   Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+   Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a   Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+   Read/Append. Preserves file content by writing to the end of the file
x   Write only. Creates a new file. Returns FALSE and an error if file already exists
x+   Read/Write. Creates a new file. Returns FALSE and an error if file already exists

One less click!
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Write to file
« Reply #4 on: November 30, 2010, 09:02:49 am »
Always nice to have these resources on a place you're much.
Keep up the good work, maybe make some topics including most of the common functions and your experiences using them (like : "I wont really use this one this one is laggy, better use ..."" . ")

I would like a topic on php optimization, (or i will google ^-^).
~Factionwars

Offline Nahid

  • NULL
  • Posts: 1
  • Cookies: 1
    • View Profile
Re: Write to file
« Reply #5 on: December 09, 2010, 05:10:37 am »
Code: [Select]
<?php file_put_contents($file,$data); ?>
« Last Edit: December 09, 2010, 05:12:27 am by Nahid »

Offline iLike

  • /dev/null
  • *
  • Posts: 8
  • Cookies: 1
  • IRC Geek.
    • View Profile
Re: Write to file
« Reply #6 on: December 12, 2010, 02:50:46 pm »
Code: [Select]
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. Plus, the fopen() call allows you to change the opening permissions more easily than the file_put_contents().
Nevertheless, file_put_contents is nice if you want to write something quickly :)