PHP Create and Save a file into a specified folder

Posted on 2011-10-29
Last Modified: 2012-06-27
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?

Question by:error77
    LVL 7

    Expert Comment

    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.
    LVL 107

    Expert Comment

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

    Author Comment

    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?

    Author Comment

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

    Accepted Solution

    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.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now