generating files out of code

How do I take a bunch of html/php code and make a file that contains only that code?
LVL 7
weikelbobAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markdocCommented:
hi, where exactly do you get these html/php code?
markdocCommented:
do you get them from other files, etc?
weikelbobAuthor Commented:
I'm auto-generating websites. They get a folder on the server. They fill out the forms and it needs to upload each php page to their folder. If they only fill out 5 pages, their folder would end up with 5 files, a 5-page website. I'm storing the information, like title, background color, text color, etc. in a MySQL database. Each page can be made from a html page with php inserting the title, background, etc. but I don't know how to dynamically place the files onto the server. All of it should be done without me (automatically)

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

markdocCommented:
well you could always use fopen() to create the files and scores of other functions to manipulate them.
http://www.php.net/manual/en/ref.filesystem.php
merwetta1Commented:
<?php

## so maybe you load a template:
$s = file_get_contents('/path/to/file.html');
## file_get_contents requires PHP >= 4.3

## then replace a few things using the form data:
$s = str_replace("%title%", $_POST['title'], $s);
$s = str_replace("%name%", $_POST['name'], $s);

## make the posted name safe for use as directory name by removing non-word chars
$name_safe = preg_replace('/\W/', '', $_POST['name']);

## create the directory for the user
$dir = '/path/to/users/'.$name_safe;
mkdir($dir, 0707);

## now create filename for user's home page
$filename = $dir.'/index.html';

## dump contents of $s to the file
ignore_user_abort(true);            ## prevent refresh/stop from screwing up file operations
$fh = @fopen($filename, 'w');
if ($fh)
{
      fwrite($fh, $s);
      fclose($fh);
} else
{
      ## handle file error here
}
ignore_user_abort(false);

## let 'em know where to go
$url = 'http://somedomain.com/users/'.$name_safe.'/';
echo 'Your new home page is at <a href="'.$url.'">'.$url.'</a>';

?>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weikelbobAuthor Commented:
My script won't make a directory:
I've cmoded users to 777:

<?php
include('includeme.php');

$_SESSION['username']="weikelbob@aol.com";
## so maybe you load a template:
$s = file_get_contents('page1.html');
## file_get_contents requires PHP >= 4.3

## then replace a few things using the form data:
$s = str_replace("%1title1%", $_SESSION['title'], $s);

  $query ="SELECT id FROM customer1 WHERE email= '" . $_SESSION['username'] . "'";
  $result = mysql_query ($query);
  $row = mysql_fetch_array($result);

## create the directory for the user
$dir = '/users/' . $row['id'];
mkdir($dir,0707);

## now create filename for user's home page
$filename = $dir.'/index.html';

## dump contents of $s to the file
ignore_user_abort(true);          ## prevent refresh/stop from screwing up file operations
$fh = @fopen($filename, 'w');
if ($fh)
{
     fwrite($fh, $s);
     fclose($fh);
} else
{
     ## handle file error here
}
ignore_user_abort(false);

## let 'em know where to go
$url = 'http://www.editable-over-time/users/' . $row['id'];
echo 'Your new home page is at <a href="'.$url.'">'.$url.'</a>';

?>
merwetta1Commented:
can you do a test of just mkdir? something like:

<?php

mkdir('/full/path/to/users/test',0777);

?>
weikelbobAuthor Commented:
It doesn't work. My domain is editable-over-time.com. I have a directory named users. I ran the following and nothing happened:

<?php

mkdir('/users/test',0777);

?>
merwetta1Commented:
I'm wondering about the directory you're trying, "/users/test". Most webservers only serve files out of some subdirectory and it would be unusual to serve web pages from a "users" directory off the root directory. I would expect the directory to be more like "/usr/www/users/test". Are you on a shared webserver? If so, then I'm almost sure that directory path is incomplete. Here's how you can find out...

Make a file called phpinfo.php with just this:
<?php
phpinfo();
?>

Put it in your "users" directory and call it up in a web browser. You should see all your PHP configuration info. Scroll down to see the value for SCRIPT_FILENAME. You should be using a path similar to what you see there, except that the "phpinfo.php" at the end will be replaced by "test" or a user name. Let me know how that works out.

FYI...you left out the ".com" from
$url = 'http://www.editable-over-time/users/' . $row['id'];

weikelbobAuthor Commented:
Thanks merwetta, the phpinfo() showed me the real directory path.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.