Jump to content


Photo

creating a new file


  • Please log in to reply
6 replies to this topic

#1 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:36 PM

how to i make php create a new file with a specific name and content?
"Syntax error" .. WHERE THE **** IS IT?!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 05:39 PM

Look in the manual at the section on filesystems. http://www.php.net/m....filesystem.php

Look at the fopen(), fwrite(), and fclose() topics.

Ken

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 10 July 2006 - 05:39 PM

fopen.

#4 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:41 PM

doesnt that just open a file? i need it to create the file!
"Syntax error" .. WHERE THE **** IS IT?!

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 05:42 PM

Look at the manual page. There are options to the fopen() function which will let you create a file.

Ken

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 10 July 2006 - 05:45 PM

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// 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";
}
?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:50 PM

o.. 'x+' ! thanks!
"Syntax error" .. WHERE THE **** IS IT?!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users