<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Unreal Blog &#187; string</title>
	<atom:link href="http://www.thulasidas.com/tag/string/feed" rel="self" type="application/rss+xml" />
	<link>http://www.thulasidas.com</link>
	<description>Perception and Physics. Science and Spirituality. Life and Work. Money and Quantitative Finance.</description>
	<lastBuildDate>Mon, 06 Sep 2010 22:41:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to save a string to a local file in PHP?</title>
		<link>http://www.thulasidas.com/2009-01/how-to-save-a-string-to-a-local-file-in-php.htm</link>
		<comments>http://www.thulasidas.com/2009-01/how-to-save-a-string-to-a-local-file-in-php.htm#comments</comments>
		<pubDate>Tue, 27 Jan 2009 14:12:42 +0000</pubDate>
		<dc:creator>Manoj</dc:creator>
				<category><![CDATA[Topical]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.thulasidas.com/?p=893</guid>
		<description><![CDATA[This post is for those of you who write plugins for WordPress. If you are not geeky enough to do that, you can safely ignore this post. Other PHP web programmers find it useful as well, if you have trouble saving a string to a file on your client's computer without creating intermediate files at your server.]]></description>
			<content:encoded><![CDATA[<p>This post is the second one in my geek series.</p>
<p>While programming my Theme Tweaker, I came across this problem. I had a string on my server in my php program (the tweaked stylesheet, in fact), and I wanted to give the user the option of saving it to a file his computer. I would&#8217;ve thought this was a common problem, and all common problems can be solved by Googling. But, lo and behold, I just couldn&#8217;t find a satisfactory solution. I found my own, and thought I would share it here, for the benefit of all the future Googlers yet to come and go.</p>
<p>Before we go into the solution, let&#8217;s understand what the problem is. The problem is in the division of labor between two computers &#8212; one is the server, where your WordPress and PHP are running; the other is the client&#8217;s computer where the viewing is taking place. The string we are talking about is on the server. We want to save it in a file on the client&#8217;s computer. The only way to do it is by serving the string as an html reply.</p>
<p>At first glance, this doesn&#8217;t look like a major problem. After all, servers regularly send strings and data to clients &#8212; that&#8217;s how we see anything on the the browser, including what you are reading. If it was just any PHP program that wants to save the string, it wouldn&#8217;t be a problem. You could just dump the string into a file on the server and serve the file.</p>
<p>But what do you do if you don&#8217;t want to give the whole world a way of dumping strings to files on your server? Well, you could do something like this:</p>
<div style="background:#666;"><code><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000066;">header</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Disposition: attachment; filename="style.css"'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">;</span><br />
<span style="color: #000066;">header</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Content-Transfer-Encoding: ascii"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">;</span><br />
<span style="color: #000066;">header</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Expires: 0'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">;</span><br />
<span style="color: #000066;">header</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Pragma: no-cache'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">;</span><br />
<span style="color: #000066;">print</span> <span style="color: #ff0000">$stylestr</span> <span style="color: #66cc66;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></code></div>
<p>So, just put this code in your foo.php that computes the string $stylestr and you are done. But our trouble is that we are working in the WordPress plugin framework, and cannot use the header() calls. When you try to do that, you will get the error message saying that header is already done dude. For this problem, I found the ingenious solution in one of the plugins that I use. Forgot which one, but I guess it is a common technique. The solution is to define an empty iFrame and set its source to what the PHP function would write. Since iFrame expects a full HTML source, you are allowed (in fact, obliged) to give the header() directives. The code snippet looks something like:</p>
<div style="background:#666;"><code><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;iframe</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"saveCSS"</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"about:blank"</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"visibility:hidden;border:none;height:1em;width:1px;"</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/iframe&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"text/javascript"</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
var fram = document.getElementById("saveCSS");<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;</span>?php echo <span style="color: #ff0000;">'fram.src = "'</span> . $styleurl .<span style="color: #ff0000;">'"'</span> ;</span></code>?<span style="color: #000000; font-weight: bold;">&gt;</span></div>
<p>Now the question is, what should the source be? In other words, what is $styleurl? Clearly, it is not going to be a static file on your server. And the purpose of this post is to show that it doesn&#8217;t have to be a file on the server at all. It is a two-part answer. You have to remember that you are working within the WordPress framework, and you cannot make standalone php files. The only thing you can do is to add arguments to the existing php files, or the plugins you have created. So you first make a submit button as follows:</p>
<div style="background:#666;">
<code><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo $_SERVER[&quot;REQUEST_URI&quot;]?&gt;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br /><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br /><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;saveCSS&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Download the tweaked stylesheet to your computer&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Download Stylesheet&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br /><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></code>
</div>
<p>Note that the name attribute of the button is &#8220;saveCSS.&#8221; Now, in the part of the code that handles submits, you do something like:</p>
<div style="background:#666;"><code><span style="color: #000000; font-weight: bold;">&lt;?php</span></code><br />
<code><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$_POST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'saveCSS'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />&nbsp; &nbsp;<span style="color: #ff0000">$styleurl</span> <span style="color: #66cc66;">=</span> get_option<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'siteurl'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">.</span> <span style="color: #ff0000;">'/'</span> <span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;/wp-admin/themes.php?page=theme-tweaker.php&amp;save&quot;</span> <span style="color: #66cc66;">;</span></code><br />
?<span style="color: #000000; font-weight: bold;">&gt;</span></div>
<p>This is the $styleurl that you would give as the source of your iFrame, fram. Note that it is the same as your pluging page URL, except that you managed to add &#8220;?save&#8221; at the end of it. The next trick is to capture that argument and handle it. For that, you use the WordPress API function, add_action as:</p>
<div style="background:#666;"><code><span style="color: #000000; font-weight: bold;">&lt;?php</span></code><br />
<code><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'save'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />&nbsp; &nbsp;add_action<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'init'</span><span style="color: #66cc66;">,</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span><span style="color: #ff0000">$thmTwk</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'saveCSS'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br /><span style="color: #b1b100;">else</span><br />&nbsp; &nbsp;remove_action<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'init'</span><span style="color: #66cc66;">,</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span><span style="color: #ff0000">$thmTwk</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'saveCSS'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br /></code>?<span style="color: #000000; font-weight: bold;">&gt;</span></div>
<p>This adds a function saveCSS to the init part of your plugin. Now you have to define this function:</p>
<div style="background:#666;"><code><span style="color: #000000; font-weight: bold;">&lt;?php</span></code><br />
<code><span style="color: #000000; font-weight: bold;">function</span> saveCSS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />&nbsp; <span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Disposition: attachment; filename=&quot;style.css&quot;'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Content-Transfer-Encoding: ascii&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Expires: 0'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br /> &nbsp; <span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Pragma: no-cache'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #ff0000">$stylestr</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Whatever string you want to save&quot;</span><span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #000066;">ob_start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #000066;">print</span> <span style="color: #ff0000">$stylestr</span> <span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #000066;">ob_end_flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">;</span><br />&nbsp; <span style="color: #000066;">die</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">;</span><br /><span style="color: #66cc66;">&#125;</span><br /></code>?<span style="color: #000000; font-weight: bold;">&gt;</span></div>
<p>Now we are almost home free. The only thing to understand is that you <em>do</em> need the die(). If your function doesn&#8217;t die, it will spew out the rest of the WordPress generated stuff into your save file, appending it to your string $stylestr.</p>
<p>It may look complicated. Well, I guess it is a bit complicated, but once you implement it and get it running, you can (and do) forget about it. At least, I do. That&#8217;s why I posted it here, so that the next time I need to do it, I can look it up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thulasidas.com/2009-01/how-to-save-a-string-to-a-local-file-in-php.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
