lifetalk Posted February 3, 2009 Share Posted February 3, 2009 I'll start with the basics.. I am trying to create an input form, that'll accept a username or a text, pass over that data to a PHP script. The PHP script needs to open an existing file on a server (it's an HTML file) and search for a specific text (the part where username is) and replace it with the username that was just posted to the script from the form. Then, close the file after editing it, create a copy, rename that copy and offer it for download to the end user. I figured this involved fopen, fwrite, fclose, preg_replace, copy, rename, POST to get it done.. but apparently, I am wrong. index.php: <html> <form method="POST" action="processme.php"> enter your username: <input type="text" name="username" /><br /> <input type="submit" name="submit" value="Yahoo!" /> </form> processme.php: <?php //define stuff $username = $_POST['username']; $filename = "generate.html"; $source = $filename; $copy = $username; $search = "user=(.*)"; $replace = "user=$username"; //open the file, and read it $handle = fopen($filename, "r+"); //search for the username, and replace it $new = preg_replace($search, $replace, $handle); //write to file fwrite($handle, $new); fclose($handle); //copy the file copy($source, $copy); //Rename file rename($copy, $username); ?> the file that needs to be edited, written to, and then cloned: <style type="text/css"> #container { width: 1024px; height: 1496px; overflow: hidden; border: 0px; } #container iframe { width: 1024px; height: 1576px; margin: -80px 0 0 0; } </style> </head> <body> <div align="center"> <img src="http://wwwstatic.megaporn.com/video/gui/logo_mprn.gif" /> </div> <div id="container"> <iframe src="http://megaporn.com/v/?c=profile_videos&user=lifetalk" scrolling="no" frameborder="0"></iframe> </div> </body> </html> Any ideas where I'm going wrong? Thanks, lifetalk P.S: I am not a pro. I can call myself a noob, for all I know! Quote Link to comment Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 What results are you getting? Quote Link to comment Share on other sites More sharing options...
printf Posted February 3, 2009 Share Posted February 3, 2009 change this... //search for the username, and replace it $new = preg_replace($search, $replace, $handle); //write to file fwrite($handle, $new); fclose($handle); to this... $size = filesize ( $filename ); //search for the username, and replace it $new = preg_replace($search, $replace, fread ( $handle, $size ) ); ftruncate ( $handle, $size ); rewind ( $handle ); //write to file fwrite($handle, $new); fclose($handle); Quote Link to comment Share on other sites More sharing options...
lifetalk Posted February 3, 2009 Author Share Posted February 3, 2009 What results are you getting? This is the error it returns Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\Program Files\VertrigoServ\www\new\processme.php on line 18 change this... //search for the username, and replace it $new = preg_replace($search, $replace, $handle); //write to file fwrite($handle, $new); fclose($handle); to this... $size = filesize ( $filename ); //search for the username, and replace it $new = preg_replace($search, $replace, fread ( $handle, $size ) ); ftruncate ( $handle, $size ); rewind ( $handle ); //write to file fwrite($handle, $new); fclose($handle); Thanks. Tried that code. The error now is Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\Program Files\VertrigoServ\www\new\processme.php on line 20 Processme.php is now: <?php //define stuff $username = $_POST['username']; $filename = "generate.html"; $source = $filename; $copy = $username; $search = "user=(.*)"; $replace = "user=$username"; //open the file, and read it $handle = fopen($filename, "r+"); $size = filesize($filename); //search for the username, and replace it $new = preg_replace($search, $replace, fread($handle, $size)); ftruncate($handle, $size); rewind($handle); //write to file fwrite($handle, $new); fclose($handle); //copy the file copy($source, $copy); //Rename file rename($copy, $username); ?> Any ideas? I'm totally stumped! Quote Link to comment Share on other sites More sharing options...
printf Posted February 3, 2009 Share Posted February 3, 2009 change... $search = "user=(.*)"; to $search = "/user=(.*)/i"; Quote Link to comment Share on other sites More sharing options...
lifetalk Posted February 3, 2009 Author Share Posted February 3, 2009 Works... No errors.. However, it sort of truncates quite a bit of essential code: Original Code (html): <html> <head> <style type="text/css"> #container { width: 1024px; height: 1496px; overflow: hidden; border: 0px; } #container iframe { width: 1024px; height: 1576px; margin: -80px 0 0 0; } </style> </head> <body> <div align="center"> <img src="http://wwwstatic.megaporn.com/video/gui/logo_mprn.gif" /> </div> <div id="container"> <iframe src="http://megaporn.com/v/?c=profile_videos&user=check" scrolling="no" frameborder="0"></iframe> </div> </body> </html> New code after running script: Resource id #3 <style type="text/css"> #container { width: 1024px; height: 1496px; overflow: hidden; border: 0px; } #container iframe { width: 1024px; height: 1576px; margin: -80px 0 0 0; } </style> </head> <body> <div align="center"> <img src="http://wwwstatic.megaporn.com/video/gui/logo_mprn.gif" /> </div> <div id="container"> <iframe src="http://megaporn.com/v/?c=profile_videos&user=eh </div> </body> </html> er="0"></iframe> </div> </body> </html> Processme.php Code: <?php //define stuff $username = $_POST['username']; $filename = "generate.html"; $source = $filename; $copy = $username; $search = "/user=(.*)/i"; $replace = "user=$username"; //open the file, and read it $handle = fopen($filename, "r+"); $size = filesize($filename); //search for the username, and replace it $new = preg_replace($search, $replace, fread($handle, $size)); ftruncate ( $handle, $size ); rewind ( $handle ); //write to file fwrite($handle, $new); fclose($handle); //copy the file copy($source, $copy); //Rename file rename($copy, $username . ".html"); ?> Thanks for all the help so far Quote Link to comment Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 Try.... $search = "/user=(.*?)\"/i"; $replace = "user=$username\""; Quote Link to comment Share on other sites More sharing options...
lifetalk Posted February 4, 2009 Author Share Posted February 4, 2009 Yup, thanks. That worked. Really appreciate it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.