?
Solved

How to create a file in php

Posted on 2003-03-28
7
Medium Priority
?
288 Views
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???
0
Comment
Question by:Benit0
[X]
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
7 Comments
 
LVL 3

Accepted Solution

by:
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

-Mark.

0
 
LVL 15

Expert Comment

by:VGR
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"
0
 

Expert Comment

by:littleiceland
ID: 8228448
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
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

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

Expert Comment

by:bljak
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";

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
 
LVL 33

Expert Comment

by:snoyes_jw
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.

snoyes_jw
EE Cleanup Volunteer
0

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