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?
 
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>';

?>
0
 
markdocCommented:
hi, where exactly do you get these html/php code?
0
 
markdocCommented:
do you get them from other files, etc?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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)

0
 
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
0
 
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>';

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

<?php

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

?>
0
 
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);

?>
0
 
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'];

0
 
weikelbobAuthor Commented:
Thanks merwetta, the phpinfo() showed me the real directory path.
0
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.