Jump to content


Photo

Saving files in PHP


  • Please log in to reply
14 replies to this topic

#1 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 10 August 2006 - 01:48 PM

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?

#2 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:12 PM

Post your script, and I'll see if I can make something up for you.

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 10 August 2006 - 05:15 PM

Look into fopen()

'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, attempt to create it.

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 08:27 AM

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

This is where I get stuck, I want to include the content of $newhtml in the file created by fopen.

#5 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 08:30 AM

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...
Why doesn't anyone ever say hi, hey, or whad up world?

#6 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 08:45 AM

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.

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 09:01 AM

<?php
$fp=fopen($username'.html','a');
$a=fwrite($fp,$newhtml);
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 09:17 AM

please test this to see how it all works ok.

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

test_result.php
<?php

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

while(!feof($c)){

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

fclose($c);
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 09:18 AM

<?
$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">
<? }
?>

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...
Why doesn't anyone ever say hi, hey, or whad up world?

#10 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 09:34 AM

Thanks a lot guys  8)

#11 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 09:41 AM

Gah, now I have a problem with the form that sends my html.
<?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>');
?>

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.

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 09:46 AM

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 10:05 AM

For some reason, that doesn't work either, the exact same result.  I've resaved the file etc, I'm not that stupid, but the same thing happens.  Could it be a problem with my $html variable?

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 August 2006 - 10:08 AM

try that ok.
<?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>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 10:46 AM

Nope, still just outputs this:

http://www.gblocal.c...hport/error.bmp

my $html variable is:

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users