[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

generating files out of code

How do I take a bunch of html/php code and make a file that contains only that code?
0
weikelbob
Asked:
weikelbob
  • 4
  • 3
  • 3
2 Solutions
 
markdocCommented:
hi, where exactly do you get these html/php code?
0
 
markdocCommented:
do you get them from other files, etc?
0
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now