• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

How to create a file in php

Hi everyone,

OK, I know how to write or read into a file, how to copy a file, but does it have a way to create a file, like a .txt or a .php???
0
Benit0
Asked:
Benit0
1 Solution
 
wide_awakeCommented:
You can create a temp file by using the tempnam or tmpfile functions.  If you want to create a specific file name, just fopen it and write the contents.  If you fopen a file for writing  that doesn't exist, fopen will create it for you.

see: http://www.php.net/manual/en/function.fopen.php

-Mark.

0
 
VGRCommented:
yes, to create a file is just the same as writing to it, even if it did pre-exist.

Just open it in "w" or "wb"
0
 
littleicelandCommented:
There is a function called touch()

usage:

$filename = "textfile.php";
touch($filename);

just make sure you have the access rights to the folder where you create your file.

Read further on http://www.php.net/manual/en/function.touch.php
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VGRCommented:
yo ! I learn something about PHP. Didn't know the *nix function made it's way to Windows platform.
0
 
bljakCommented:
try this

$myText = "here comes example text",
$myPath = "/path/to/file/"; //in Windows use C:\path\to\file\ and add / and \ at end
$myFile = "myfile.txt";

if(file_exist($myPath.$myFile))
{
  echo "file exists";
  //can add code below else statement but use "a" if you want to add this text to existing file without overwriting previous text
} else {
  //opens file for writing, will create if not existing
  $fp = fopen($myPath.$myFile, "w+")
  fwrite($fp,$myText);
  fclose($fp);
}

thats all, didn't write any error handles since i assume www user has rights to write into dir

//bljak
0
 
snoyes_jwCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: wide_awake {http:#8227647}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

snoyes_jw
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now