Jump to content

How to safely write a php file


Recommended Posts

I produce a series of php variables in a php strict (mostly come from mysql), and I want to save them as a php file. To be use for "include". What is the safest way to write them in a php file?


I tried to save them in the manner of saving a text file with fwrite as


$data='<?php $cat=array('array created in the text'); $string='some value';?>';
$fh = fopen("file.php", 'w');
fwrite($fh, $data);


Now, file.php is a normal php file and I can call it with "include"; but the problem is that this method is not safe. For example, if the string is "There's something", php gives error for the presence of ' in the string. I can skip ' and " by backslash; but this method is costy, as I need to perform this process for all strings to be saved. Moreover, there might be other source of errors too (that I have not encountered yet).


Is there a safe way for writing a php code to file?



Link to comment
Share on other sites

You can make an additional PHP script directory outside the web server's document root. Make sure the directory is writable by the web server. This way the files are always accessed by the web server but not by the web browser directly.

Link to comment
Share on other sites

The mysql query cache will cache the result for you -


The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.


The query cache can be useful in an environment where you have tables that do not change very often and for which the server receives many identical queries. This is a typical situation for many Web servers that generate many dynamic pages based on database content.


The query cache does not return stale data. When tables are modified, any relevant entries in the query cache are flushed.



Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.