ScottRiley Posted August 10, 2006 Share Posted August 10, 2006 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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/ Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Share Posted August 10, 2006 Post your script, and I'll see if I can make something up for you. Link to comment https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72601 Share on other sites More sharing options...
Orio Posted August 10, 2006 Share Posted August 10, 2006 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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72608 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 [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"> </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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72924 Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 You could use something likewhile(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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72926 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72933 Share on other sites More sharing options...
redarrow Posted August 11, 2006 Share Posted August 11, 2006 [code]<?php$fp=fopen($username'.html','a');$a=fwrite($fp,$newhtml);?>[/code] Link to comment https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72944 Share on other sites More sharing options...
redarrow Posted August 11, 2006 Share Posted August 11, 2006 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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72950 Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 [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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72951 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 Thanks a lot guys 8) Link to comment https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72969 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72976 Share on other sites More sharing options...
redarrow Posted August 11, 2006 Share Posted August 11, 2006 [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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72979 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 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? Link to comment https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72988 Share on other sites More sharing options...
redarrow Posted August 11, 2006 Share Posted August 11, 2006 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 https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-72990 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 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"> </p> <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2"><b> </b></font></p> <p style="margin-top: 0; margin-bottom: 0"> </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"> </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"> </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"> </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"> 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"> </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:[email protected]?subject=Website Enquiry - GBLocal"> [email protected]</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"> </p> <p style="margin-top: 0; margin-bottom: 0" align="center"> </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"> </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"> </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"> </p> <p style="margin-top: 0; margin-bottom: 0"><font face="Arial" size="2">'.$text5.' <p style="margin-top: 0; margin-bottom: 0"> </p> <p style="margin-top: 0; margin-bottom: 0"></td> </tr> </table> </center></div>';[/code] Link to comment https://forums.phpfreaks.com/topic/17131-saving-files-in-php/#findComment-73002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.