PHP Create and Save a file into a specified folder

Hi all,

I need to create a php file with php. It will contain php, html and js code. It will and save it to a specifed folder programmatically.

Can anyone provide a simple sample code please?

Who is Participating?
Ray PaseurCommented:
The fopen() might need more accurate path information.  Check into the values you find with getcwd() and look into the output from phpinfo() near the bottom.

Looking at ID:37052083 I am wondering about the idea of saving multiple copies of a PHP script with different variable values.  Normally, programming is generalized -- made to work correctly on a set of variables.  The variables are given to the program script via GET or POST method requests.  In this way one program script can produce a variety of customized outputs, depending on the values of these external variables.  If you were working with HTML, which has no programmable aspects (it's not interpreted, just a string of characters) I could understand saving different copies, but with PHP I think the logical approach might be to write a program that works for the general case, and then send in the variables from an external source, perhaps in the URL.
HellmarkLinux Systems AdministratorCommented:
You say "It will and save it to a specifed folder programmatically.", but you do not specify what it will do.  Please provide more information.
Ray PaseurCommented:
You best friend here may be heredoc notation to create the string.  But this does not sound like anything we studied in computer science.  Where did you get the idea that you needed to do something like this?  Can you please step back from the technical description and just tell us in plain language what business question you are trying to solve?  Thanks, ~Ray
error77Author Commented:
Basically I have a php page which I could just copy to the folder BUT I need to slightly modify it each time I copy one to a folder. Things that change are user id etc ... so the php page will have it's variable values changed and saved as a new page.

Make sense now?
error77Author Commented:
I am trying this to start with:

$fh = fopen("/includes/myfile.txt", "w");

      die("unable to create file");

but all I get is "unable to create file" although the target directory is cmod 0777
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.