We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

generating files out of code

weikelbob
weikelbob asked
on
Medium Priority
182 Views
Last Modified: 2008-03-03
How do I take a bunch of html/php code and make a file that contains only that code?
Comment
Watch Question

Commented:
hi, where exactly do you get these html/php code?

Commented:
do you get them from other files, etc?

Author

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)

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
<?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>';

?>

Author

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

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

<?php

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

?>

Author

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

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

Author

Commented:
Thanks merwetta, the phpinfo() showed me the real directory path.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.