First and foremost, thanks for being out there helping guys like me who are new to PHP. I need to know if it is possible to write to an HTML file using the "fwrite" function in PHP, but not to the end of the file as it is by default. What I want to do is to write a string to a specific location into the HTML file such as a div tag inside the body tag. Is this possible? Thanks. Juan Carlos
It may be possible, but I think JavaScript may work better for you. Could you explain what you're looking to accomplish? Is this going to be dynamic content?
You would need to open and read the file. Probably putting the file to a string would be the best way to deal with it. You then need to find and replace whatever it is you're looking for, and then write the file back to it's original location. A really rough overview: $fileName = 'myfile.txt'; $file = file_get_contents($fileName); $newFile = preg_replace('/[SOME PATTERN]/','REPLACEMENT',$file); //then write back to the file. if (!$handle = fopen($fileName, 'w')) { echo "Cannot open file ($fileName)"; exit; } if (fwrite($handle, $newFile) === false) { echo "Cannot write to file ($fileName)"; exit; } fclose($handle); PHP:
Altari, thanks for your reply. I'll try to explain my self briefly. -I'm creating a script from scratch to retrieve text and images from an html form.(this part works ok so far) -The script creates a txt file with the textfields data and uploads an image file to the server.(this also works) -The trick is to have the script automatically write into the html file where I want to display the text and image.( I haven't figured this out yet). I have been able to write into the html file with the script, but it writes at the end of the file (at the very end outside the appropriate tags). Again, I was thinking that maybe I can make the "fwrite" function write especifically to a div inside the body tag. Hope I was easy to understand.
Thanks a lot Jestep. I'm working with the example code that you provided right now, I hope I'm able to make it work. Thanks a million Juan Carlos