Jump to content

Saving files in PHP


ScottRiley

Recommended Posts

Here's the situation.  I've developed a system where through entering text into boxes, a user can have a template displayed to them, containing the various values of the text boxes.  I have managed to get that to work nicely, and the html is generated as a preview, the user can then choose to submit this, which just sends the $html variable (stored as a hidden field in a form) to a PHP page.  I want this PHP page to save this variable as a new html file, is it possible to do such a thing?
Link to comment
Share on other sites

Look into [url=http://www.php.net/manual/en/function.fopen.php]fopen()[/url]

'w+'- Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, [b]attempt to create it.[/b]

Orio.
Link to comment
Share on other sites

[code]<?php
      $html=$_POST['html'];
      $newhtml='<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>'.$username.'</title>

<script type="text/javascript" language="JavaScript1.2" src="../_pgtres/stmenu.js"></script>
</head>

<body>

<div align="center">
  <center>
  '.$html.'
  </center>
</div>

  <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>

</body>

</html>';
$fp=fopen($username'.html','w+');
?>[/code]

This is where I get stuck, I want to include the content of $newhtml in the file created by fopen.
Link to comment
Share on other sites

You could use something like

while(file_exists($filename))
{
$filename = mt_rand() . ".html";
}

Then the page they created could be returned to them in a link... Or maybe if you stored pages vs usernames in a database they could look up alll the pages they created...
Link to comment
Share on other sites

I want the page to be stored on the FTP server, fopen would create the file, right?  I just don't know how to insert data into this file, I tried fwrite($fp, $newhtml), but that doesn't work, I doubt that's even the use of fwrite, I was just trying things out.  What I basically want is for the user to put whatever text they want in some boxes, and it generates the html for their advert.  After that, I want the advert to be saved onto the server, containing the html generated.
Link to comment
Share on other sites

please test this to see how it all works ok.
[code]

test.php
<?php

$html="<html><title>test</title><body><h1>hello all</h1></body></html>";

$a=fopen("test100.txt","a");

$b=fwrite($a,$html);

fclose($a);
?>
[/code]

test_result.php
[code]
<?php

$c=fopen("test100.txt","r");

while(!feof($c)){

$result=fgets($c);
}
echo $result;

fclose($c);
?>
[/code]
Link to comment
Share on other sites

[code=php:0]
<?
$html = $_POST['html'];
if($_POST['submit']) {
$file = "files/" . $username . "_" . mt_rand() . ".html";
while(file_exists($file)) {
$file = "files/" . $username . "_" . mt_rand . ".html";
}
$handle = fopen($file, "w+");
if(fwrite($handle, $html)) {
echo "Your file was successfully written to " . $file;
}
fclose($handle);
}
if(!$_POST['submit']) {
?>
<form action="" method=POST>
<textarea cols="25" rows="10" name="html"></textarea><BR>
<input type="submit" name="submit" value="Create">
<? }
?>
[/code]

Very simple script.  Creates a file after making sure it doesnt already exist and adds the form input into the file.  Itll create a file like bob_34820481234.html if the users name is bob... It will then tell the user what the name of the file is once its created... you could easily have it output one of the $_SERVER vars along with $file so that it would be the full url to the file...
Link to comment
Share on other sites

Gah, now I have a problem with the form that sends my html.
[code]<?php
$html= <a lot of html here>
echo('If you are happy with this advert, please click "Submit" below, if you want to change any values, close this window and resubmit the form');
echo('<form action="createad.php" method="post">
<input name="html" type="hidden" value="'.$html.'">
<input name="username" type="hidden" value="'.$username.'">
<input name="submit" type="submit" value="Submit">
</form>');
?>[/code]

What's happening is:

Its displaying the HTML, then it's displaying the 'if you are happy with....' message, then it repeats the html with `"/>` after it.  I understand this must be a problem with the echo statement for the form.  When this is sent to the createad PHP file, I ran a test where it echoed the username and HTML, the username came out fine, but the HTML was blank (obviously).  Can anyone see my mistake, its probably a really trivial one, like I've missed out a comma or something, but I've checked and it all seems right.
Link to comment
Share on other sites

[code]
<?php
$html= <a lot of html here>
echo('If you are happy with this advert, please click "Submit" below, if you want to change any values, close this window and resubmit the form');
?>
<form action="createad.php" method="post">
<input name="html" type="hidden" value="<?php echo $html ?>">
<input name="username" type="hidden" value="<?php echo $username ?>">
<input name="submit" type="submit" value="Submit">
</form>
[/code]
Link to comment
Share on other sites

try that ok.
[code]
<?php
$html= <a lot of html here>
echo"If you are happy with this advert, please click "Submit" below, if you want
to change any values, close this window and resubmit the form";
?>
<form action="createad.php" method="post">
<input name="html" type="hidden" value="<?php echo $html ?>">
<input name="username" type="hidden" value="<?php echo $username ?>">
<input name="submit" type="submit" value="Submit">
</form>
[/code]
Link to comment
Share on other sites

Nope, still just outputs this:

[IMG]http://www.gblocal.co.uk/southport/error.bmp[/img]

my $html variable is:

[code]$html='<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="619" id="AutoNumber6" height="943">
    <tr>
      <td width="297" rowspan="3" height="245">
  <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b>'.$title1.'</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b>&nbsp;</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0" align="justify">
      <font face="Arial" size="2"><b>'.$text1.'</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0" align="justify">&nbsp;</p>
  <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b>'.$title2.'</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0" align="justify">
      <font face="Arial" size="2">'.$text2.'</font></p>
      <p style="margin-top: 0; margin-bottom: 0"></td>
      <td width="15" rowspan="3" height="245">
      <p style="margin-top: 0; margin-bottom: 0"></td>
      <td valign="top" colspan="3" height="93" width="307">
      <p style="margin-top: 0; margin-bottom: 0" align="center">
      <font face="Arial">
        <img border="0" src="images/iss_squ_small.jpg" width="111" height="96"></font></p>
      <p style="margin-top: 0; margin-bottom: 0" align="center"><b>
      <font face="Arial" size="2">Contact Information and address details</font></b></td>
    </tr>
    <tr>
      <td width="67" valign="top" height="85">&nbsp;</td>
      <td width="200" valign="middle" height="85">
        <p style="margin-top: 0; margin-bottom: 0"><b>
        <font face="Arial" size="2">ISS
        Healthcare </font></b></p>
        <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">
        Weld Parade,</font></p>
        <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">&nbsp;Weld
        Road,</font></p>
        <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">Birkdale,</font></p>
        <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">Southport</font></p>
        <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">PR8 2DT</font></td>
      <td width="40" valign="top" height="85">&nbsp;</td>
    </tr>
    <tr>
      <td width="307" valign="top" colspan="3" height="67">
        <p style="margin-top: 0; margin-bottom: 0" align="center">
        <font face="Arial" size="2">Tel : 0870
        7500 631</font></p>
        <p style="margin-top: 0; margin-bottom: 0" align="center">
        <font face="Arial" size="2">Email :
        <a href="mailto:enquiries@isshealthcare.co.uk?subject=Website Enquiry - GBLocal">
        enquiries@isshealthcare.co.uk</a></font></p>
        <p style="margin-top: 0; margin-bottom: 0" align="center">
        <font face="Arial" size="2">Website
        <a target="_blank" href="http://www.isshealthcare.co.uk">www.isshealthcare.co.uk</a></font></p>
        <p style="margin-top: 0; margin-bottom: 0" align="center">&nbsp;</p>
        <p style="margin-top: 0; margin-bottom: 0" align="center">&nbsp;</p>
        </td>
    </tr>
    <tr>
      <td width="297" height="197" align="justify">
      <p align="center" style="margin-top: 0; margin-bottom: 0">
      <font face="Arial" size="2"><img border="0" src="images/iss_img1.jpg" width="237" height="158"></font></td>
      <td width="15" height="197" align="justify">
      <p style="margin-top: 0; margin-bottom: 0"></td>
      <td width="307" colspan="3" height="197" align="justify">
  <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b>'.$title3.'</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">'.$text3.'</font></td>
    </tr>
    <tr>
      <td width="619" colspan="5" height="166" align="justify">
  <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b>'.$title4.'</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0"><font size="2" face="Arial">
    '.$text4.'</font></p>
      <p style="margin-top: 0; margin-bottom: 0"></td>
    </tr>
    <tr>
      <td width="619" colspan="5" height="335" align="justify">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b>'.$title5.'</b></font></p>
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">'.$text5.'
      <p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
      <p style="margin-top: 0; margin-bottom: 0"></td>
    </tr>
  </table>
  </center>
</div>';[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.