Hi,

I am making a script that takes the file contents of another chosen file on the same server, everything in the file is pulled into a textarea including the PHP in the file. I want to know how to then save all this data to a PHP file (temporary) as a preview for the user, and if they're happy, they can then click save which will write all code to the original file which the code was pulled from.

Thank you,

Daniel

Recommended Answers

All 5 Replies

Member Avatar for diafol
<?php  
$str = file_get_contents("index.php");
?>
<textarea><?php echo $str;?></textarea>

You can write it out using any of the File functions (e.g. file_put_contents) under a a temp name ending with a .php suffix. You should probably be putting it in a separate folder just for that purpose. You can give the user a link to that temp program file. If this is software that the user will load onto his/her own site and this function is well protected, then there may not be a security issue (at least not for you). If it is on your own site, then you need to carefully consider the security hole that you are opening.

commented: Needed to be said +8
Member Avatar for diafol

> If it is on your own site, then you need to carefully consider the security hole that you are opening.

Well said.

Thank you for your replies. I will consider security as I will be releasing this script free of charge, and the file put contents writes the php code as php code?

Meaning will it actually function as php or will it just show on the page it is written to?

Thanks again!

Member Avatar for diafol

file_get_contents: on your own site, I think you might get the code, on another remote site, you'll get the output. file_put_contents? I think you need to create a stream_context_create(). But, I've never tried that. If you own the site, you can use ftp functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.