How to create a file in php

Posted on 2003-03-28
Medium Priority
Last Modified: 2011-08-18
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???
Question by:Benit0
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

wide_awake earned 300 total points
ID: 8227647
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


LVL 15

Expert Comment

ID: 8227868
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"

Expert Comment

ID: 8228448
There is a function called touch()


$filename = "textfile.php";

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 15

Expert Comment

ID: 8228538
yo ! I learn something about PHP. Didn't know the *nix function made it's way to Windows platform.

Expert Comment

ID: 8229949
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";

  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+")

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

LVL 33

Expert Comment

ID: 11942733
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.

EE Cleanup Volunteer

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question