<?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>Computer file formats &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/computer-file-formats/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Data Compression with Diagram Encoding and Pattern Substitution</title>
		<link>/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/</link>
		<comments>/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/#respond</comments>
		<pubDate>Mon, 23 Jan 2012 14:58:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Coding theory]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[conventional compressing tool]]></category>
		<category><![CDATA[Data compression]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Information theory]]></category>
		<category><![CDATA[Lossy compression]]></category>
		<category><![CDATA[pattern substitution algorithm]]></category>
		<category><![CDATA[pattern substitution algorithms]]></category>
		<category><![CDATA[Pattern Substitution The pattern substitution algorithm]]></category>
		<category><![CDATA[Run-length encoding]]></category>
		<category><![CDATA[web hosting]]></category>

		<guid isPermaLink="false">/?p=2623</guid>
		<description><![CDATA[Overview Two variants of run-length encoding are the diagram encoding and the pattern substitution algorithms. The diagram encoding is actually a very simple algorithm. Unlike run-length encoding, where the input stream must consists of many repeating elements, as “aaaaaaaa” for instance, which are very rare in a natural language, there are many so called “diagrams” &#8230; <a href="/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Data Compression with Diagram Encoding and Pattern Substitution</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2012/02/06/computer-algorithms-data-compression-with-prefix-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Prefix Encoding">Computer Algorithms: Data Compression with Prefix Encoding </a></li>
<li><a href="/2012/01/09/computer-algorithms-data-compression-with-run-length-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Run-length Encoding">Computer Algorithms: Data Compression with Run-length Encoding </a></li>
<li><a href="/2012/01/16/computer-algorithms-data-compression-with-bitmaps/" rel="bookmark" title="Computer Algorithms: Data Compression with Bitmaps">Computer Algorithms: Data Compression with Bitmaps </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Overview</h2>
<p>Two variants of <a href="/2012/01/09/computer-algorithms-data-compression-with-run-length-encoding/" title="Computer Algorithms: Data Compression with Run-length Encoding">run-length encoding</a> are the diagram encoding and the pattern substitution algorithms. The diagram encoding is actually a very simple algorithm. Unlike run-length encoding, where the input stream must consists of many repeating elements, as <strong>“aaaaaaaa”</strong> for instance, which are very rare in a natural language, there are many so called “diagrams” in almost any natural language. In plain English there are some diagrams as <strong>“the”</strong>, <strong>“and”</strong>, <strong>“ing”</strong> (in the word “waiting” for example), <strong>“ a”</strong>, <strong>“ t”</strong>, <strong>“ e”</strong> and many doubled letters. Actually we can extend those diagrams by adding surrounding spaces. Thus we can encode not only “the”, but “ the “, which are 5 characters (2 spaces and 3 letters) with something shorter. In the other hand, as I said, in plain English there are two many doubled letters, which unfortunately aren’t something special for run-length encoding and the compression ratio will be small. Even worse the encoded text may happen to be longer than the input message. Let’s see some examples.</p>
<p>Let’s say we’ve to encode the message “successfully accomplished”, which consists of four doubled letters. However to compress it with run-length encoding we’ll need at least 8 characters, which doesn’t help us a lot.</p>
<pre>
// 8 chars replaced by 8 chars!?
input: 	"successfully accomplished"
output:	"su2ce2sfu2ly a2complished"
</pre>
<p>The problem is that if the input text contains numbers, “2” in particular, we’ve to chose an escape symbol (“@” for example), which we’ll use to mark where the encoded run begins. Thus if the input message is “2 successfully accomplished tasks”, it will be encoded as “2 su@2ce@2sfu@2ly a@2complished tasks”. Now the output message is longer!!! than the input string.</p>
<pre>
// the compressed message is longer!!!
input:	"2 successfully accomplished"
output:	"2 su@2ce@2sfu@2ly a@2complished tasks"
</pre>
<p>Again if the input stream contains the escape symbol, we have to find another one, and the problem is that it is often too difficult to find short escape symbol that doesn’t appear in the input text, without a full scan of the text.<span id="more-2623"></span></p>
<p>That is why run-length encoding isn’t a good solution when compressing plain text, where long runs rarely appear. Well, of course, there are exceptions. For example such an exception is the lossy text compression with run-length encoding. It is intuitively clear that compressing text with loss is rarely useful, especially when you’ve to decompress exactly the same text. However there are some cases that lossy compression may be useful. Such case can be removing spaces. Indeed the text <strong>“successfully      accomplished”</strong> brings us exactly the same information as <strong>“successfully accomplished”</strong>. In this case we can simply remove those spaces. Indeed we can use a marker to indicate the long run of spaces like <strong>“successfully@6 accomplished”</strong> in order to decompress the input string with absolutely no loss, but we can also throw those symbols away. This desision depends on the goal. Exactly with the same goal in mind we can remove new lines and tabs, only if we’re sure that the sense of the text is preserved. Yet again, a problem is that such long runs don’t happen to occur in random texts. That is why it’s better to use diagram encoding for plain text compression instead of run-length encoding.</p>
<h2>Few Questions</h2>
<p>After understanding the principles of the diagram encoding, let’s see some examples. In the example above it is better to replace doubled letters with something shorter. Let’s say # for “cc”, @ for “ss” and % for “ll”. Thus the input text will be compressed as “su#e@fu%y a#omplished”,  which is shorter. But yet again what will happen if the input message contains one of the substitutions? Also we can’t say if there are many doubled letters and enough reasonable substitutions for them. A better approach is to replace patterns. </p>
<figure id="attachment_2640" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/DiagramEncodingonTexts.png"><img src="/wp-content/uploads/2012/01/DiagramEncodingonTexts.png" alt="Compressing texts with diagram encoding" title="Compressing texts with diagram encoding" width="620" class="size-full wp-image-2640" srcset="/wp-content/uploads/2012/01/DiagramEncodingonTexts.png 683w, /wp-content/uploads/2012/01/DiagramEncodingonTexts-300x128.png 300w" sizes="(max-width: 683px) 100vw, 683px" /></a><figcaption class="wp-caption-text">Run-length encoding isn&#039;t a good approach for text compression, because long runs rarely appear in a natural language.</figcaption></figure>
<h2>Pattern Substitution</h2>
<p>The pattern substitution algorithm is a variant of the diagram encoding. As I said above in plain English a very commonly used pattern can be “ the “, which is five characters long. We can now replace it with something like “$%” for example. In this case the message <strong>“I send the message”</strong> will become <strong>“I send$%message”</strong>. However there are some obstacles to overcome.</p>
<p>The first problem is that we need to know the language and somehow to define commonly used patterns in a dictionary. What would happen with a message written in some language we don’t know nothing about. Let’s say &#8211; Latin like the example bellow.</p>
<blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras venenatis, sapien eget suscipit placerat, justo quam blandit mauris, quis tempor ante sapien sodales augue. Praesent ut mauris quam. Phasellus scelerisque, ante quis consequat tristique, metus turpis consectetur leo, vitae facilisis sapien mi eu sapien. Praesent vitae ligula elit, et faucibus augue. Sed rhoncus sodales dolor ut gravida. In quis augue ac nulla auctor mattis sed sed libero. Donec eget purus eget enim tempor porta vitae eget diam. Mauris aliquet malesuada ipsum, non pulvinar urna vestibulum ac. Donec feugiat velit vitae nunc cursus imperdiet. Donec accumsan faucibus dictum. Phasellus sed mauris sapien. Maecenas mi metus, tincidunt sed rhoncus nec, sodales non sapien.</p></blockquote>
<p>Clearly without knowing Latin it isn’t easy to define which are those commonly used patterns. The thing is that it&#8217;s better to use pattern substitution if you know in advance the set of words and characters.</p>
<p>The second problem is related to decompression. It is obvious that we need to define a dictionary and this dictionary must be used when decoding the message. It will be great also if we find more patterns longer than three characters. If not, the compression ratio will be low. Unfortunately such patterns aren’t very common in any natural language.</p>
<figure id="attachment_2643" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/PatternSubstitutiononTexts.png"><img src="/wp-content/uploads/2012/01/PatternSubstitutiononTexts.png" alt="Text compression with diagram encoding and pattern substitution" title="Text compression with diagram encoding and pattern substitution" width="620" class="size-full wp-image-2643" srcset="/wp-content/uploads/2012/01/PatternSubstitutiononTexts.png 683w, /wp-content/uploads/2012/01/PatternSubstitutiononTexts-300x128.png 300w" sizes="(max-width: 683px) 100vw, 683px" /></a><figcaption class="wp-caption-text">Diagram encoding and pattern substitution are far more suitable for text compression than run-length encoding. In fact, pattern substitution is very effective on compressing programming languages.</figcaption></figure>
<h2>Application</h2>
<p>It is interesting to answer the question, how to use diagram encoding or patter substitution to compress text in natural language, especially when we don’t know the language in detail? The answer hides in the question. We wont compress natural languages, but machine language. Exactly machine (programming) languages are limited to a smaller sets of words and symbols. Isn’t it true for any programing language? Like PHP, where words like <strong>“function”</strong>, <strong>“while”</strong>, <strong>“for”</strong>, <strong>“break”</strong>, <strong>“switch”</strong>, <strong>“foreach”</strong> happen to be often in use, or HTML with its defined set of tags. Perhaps the best example is CSS, where only the values of the properties can vary. CSS files also tend to have multiple new lines, tabs and spaces, which only humans read.</p>
<p>The question here is why should we compress those file types. It’s clear that after the compression they will be completely useless, both for humans and machines. Yes, that is true, but what if we have to store versions of those files into a DB. Kind of a backup. Imagine you’re working for a web hosting company that has to store daily versions of the sites it’s hosting. Thus the volume of stored information even for small companies hosting only few sites can be enormous. The problem is that compressing those files with some conventional compressing tool isn’t a good idea. Thus we’ve to save a copy of the entire site every day, but as we know the difference between daily versions of a site can be small. A version control system is another solution, but then you’ve to store the plain text of the files. </p>
<p>Perhaps a better approach is to compress the text using pattern substitution and then saving only differences &#8211; kind of version control, which can be done with “relative encoding”.</p>
<p>Using the above method we can save lots of disk space and in the same time we can compress/decompress easily. Another good thing is that you can save only changes to the initial files, like version control, which can also be compressed.</p>
<h2>Implementation</h2>
<p>The implementation of this algorithm is again on PHP and tries only to describe the main principles of compression. In this case I tried to compress a CSS file using the compression above. Although this example is quite primitive we can see some interesting facts. First of all you only need encoding and decoding dictionaries. Practically the encoding and decoding processes are equal, so you don’t need to implement two different functions. Here in this example a native PHP function is used &#8211; str_replace, because the purpose of this algorithm is not to describe pattern substitution techniques, but pattern substitution. It assumes that today’s programming languages have string manipulation functions for the purposes of this task.</p>
<pre lang="PHP">
$str = file_get_contents('large_style_file.css');

$encoding_dict = array(
	"\n" 		=> '$0',
	'text' 		=> '$1',
	'color' 	=> '$2',
	'display' 	=> '$3',
	'font' 		=> '$4',
	'width' 	=> '$5',
	'height'	=> '$6',	
	' '		=> '',
);

function replace_patterns($input, $dict) 
{
	foreach ($dict as $pattern => $replace) {
		$input = str_replace($pattern, $replace, $input);
	}
	
	return $input;
}

$result = replace_patterns($str, $encoding_dict);
</pre>
<p>By only replacing few CSS properties I achieved almost 40% of compression ratio (as shows the diagram bellow). The initial file is 202 KB, while compressed it&#8217;s only 131 KB. Of course, it all depends on the CSS file, but how about replacing all property names with shorter ones. Perhaps then the compression will be even better.</p>
<figure id="attachment_2647" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/chart_1.png"><img src="/wp-content/uploads/2012/01/chart_1.png" alt="CSS compression with pattern substitution" title="CSS compression with pattern substitution" width="600" height="371" class="size-full wp-image-2647" srcset="/wp-content/uploads/2012/01/chart_1.png 600w, /wp-content/uploads/2012/01/chart_1-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text"> </figcaption></figure>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2012/02/06/computer-algorithms-data-compression-with-prefix-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Prefix Encoding">Computer Algorithms: Data Compression with Prefix Encoding </a></li>
<li><a href="/2012/01/09/computer-algorithms-data-compression-with-run-length-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Run-length Encoding">Computer Algorithms: Data Compression with Run-length Encoding </a></li>
<li><a href="/2012/01/16/computer-algorithms-data-compression-with-bitmaps/" rel="bookmark" title="Computer Algorithms: Data Compression with Bitmaps">Computer Algorithms: Data Compression with Bitmaps </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Does SimpleXMLElement have toString() method? No, better use asXML()!</title>
		<link>/2011/10/10/php-does-simplexmlelement-have-tostring-method-no-better-use-asxml/</link>
		<comments>/2011/10/10/php-does-simplexmlelement-have-tostring-method-no-better-use-asxml/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 11:52:49 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[Open formats]]></category>
		<category><![CDATA[OSI protocols]]></category>
		<category><![CDATA[Technical communication]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">/?p=2362</guid>
		<description><![CDATA[SimpleXML is a PHP extension that &#8220;provides a very simple and easily usable toolset to convert XML to an object&#8221; [1]. Thus you can pass a link to an XML file and SimpleXML will return an object. $xml = simplexml_load_file('path_to_the_file'); Sometimes you&#8217;d need to dump or save the entire XML as a string, but there’s &#8230; <a href="/2011/10/10/php-does-simplexmlelement-have-tostring-method-no-better-use-asxml/" class="more-link">Continue reading <span class="screen-reader-text">PHP: Does SimpleXMLElement have toString() method? No, better use asXML()!</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/" rel="bookmark" title="PHP Strings: How to Get the Extension of a File">PHP Strings: How to Get the Extension of a File </a></li>
<li><a href="/2010/04/20/how-to-sanitize-user-input-in-php/" rel="bookmark" title="How to Sanitize User Input in PHP?">How to Sanitize User Input in PHP? </a></li>
<li><a href="/2010/07/14/media-rss-and-zf-part-2/" rel="bookmark" title="Media RSS and ZF &#8211; Part 2">Media RSS and ZF &#8211; Part 2 </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>SimpleXML is a PHP extension that &#8220;provides a very simple and easily usable toolset to convert XML to an object&#8221; [1]. Thus you can pass a link to an XML file and SimpleXML will return an object.</p>
<pre lang="php">$xml = simplexml_load_file('path_to_the_file');</pre>
<p>Sometimes you&#8217;d need to dump or save the entire XML as a string, but there’s no toString method! As you can see $xml is an instance of the SimpleXMLElement class.</p>
<pre lang="php">var_dump($xml); // object(SimpleXMLElement)...</pre>
<p>Actually if you take a closer look:</p>
<pre lang="php">$xml->toString();</pre>
<p>will return <strong>&#8220;Call to an undefined method toString()&#8221;</strong>, which is frustrating because the developers community is used to use toString() when converting an object into a string.</p>
<figure id="attachment_2395" style="width: 448px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/10/XML.jpg"><img src="/wp-content/uploads/2011/10/XML.jpg" alt="XML" title="XML" width="448" height="269" class="size-full wp-image-2395" srcset="/wp-content/uploads/2011/10/XML.jpg 448w, /wp-content/uploads/2011/10/XML-300x180.jpg 300w" sizes="(max-width: 448px) 100vw, 448px" /></a><figcaption class="wp-caption-text">PHP&#039;s SimpleXML doesn&#039;t have toString() method. asXML() is used instead!</figcaption></figure>
<h2>The solution</h2>
<p>In fact there’s a method doing exactly what’s needed. This is <a title="SimpleXMLElement::asXML()" href="http://www.php.net/manual/en/simplexmlelement.asxml.php" target="_blank">SimpleXMLElement::asXML</a></p>
<p>As described in the manual page: &#8220;SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element&#8221; [2].</p>
<p>Besides that it does exactly what&#8217;s needed it sounds irrelevant, because you&#8217;ve an XML object and the name &#8220;asXML&#8221; doesn&#8217;t describe correctly what&#8217;s expected.</p>
<pre lang="php">$xml->asXML() // ?!</pre>
<ol>
<li><a title="SimpleXMLElement::asXML" href="http://www.php.net/manual/en/intro.simplexml.php" target="_blank">SimpleXMLElement::asXML</a></li>
<li><a title="SimpleXML Introduction" href="http://www.php.net/manual/en/intro.simplexml.php" target="_blank">SimpleXML Introduction</a></li>
</ol>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/" rel="bookmark" title="PHP Strings: How to Get the Extension of a File">PHP Strings: How to Get the Extension of a File </a></li>
<li><a href="/2010/04/20/how-to-sanitize-user-input-in-php/" rel="bookmark" title="How to Sanitize User Input in PHP?">How to Sanitize User Input in PHP? </a></li>
<li><a href="/2010/07/14/media-rss-and-zf-part-2/" rel="bookmark" title="Media RSS and ZF &#8211; Part 2">Media RSS and ZF &#8211; Part 2 </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/10/php-does-simplexmlelement-have-tostring-method-no-better-use-asxml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Strings: How to Get the Extension of a File</title>
		<link>/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/</link>
		<comments>/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 06:25:32 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[elegant solution]]></category>
		<category><![CDATA[EXE]]></category>
		<category><![CDATA[Filename]]></category>
		<category><![CDATA[gif]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[path]]></category>

		<guid isPermaLink="false">/?p=2335</guid>
		<description><![CDATA[EXE or GIF or DLL or &#8230; Most of the code chunks I&#8217;ve seen about getting a file extension from a string are based on some sort of string manipulation. $filename = '/my/path/image.jpeg'; echo substr($filename, strrpos($filename, '.') + 1); Howerver there is a more elegant solution. $filename = '/my/path/image.jpeg'; echo strtolower(pathinfo($filename, PATHINFO_EXTENSION)); Thus you rely &#8230; <a href="/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/" class="more-link">Continue reading <span class="screen-reader-text">PHP Strings: How to Get the Extension of a File</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/10/04/some-php-tips-basename/" rel="bookmark" title="Some PHP Tips: basename()">Some PHP Tips: basename() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>EXE or GIF or DLL or &#8230;</h2>
<p>Most of the code chunks I&#8217;ve seen about getting a file extension from a string are based on some sort of string manipulation.<br />
<figure id="attachment_2351" style="width: 235px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/07/filename_extension.jpg"><img class="size-full wp-image-2351" title="filename_extension" src="/wp-content/uploads/2011/07/filename_extension.jpg" alt="Get the Filename Extension with PHP" width="235" height="235" srcset="/wp-content/uploads/2011/07/filename_extension.jpg 235w, /wp-content/uploads/2011/07/filename_extension-150x150.jpg 150w" sizes="(max-width: 235px) 100vw, 235px" /></a><figcaption class="wp-caption-text">If you want to get the filename extension with PHP is better to use pathinfo() than string manipulations</figcaption></figure></p>
<pre lang="php">$filename = '/my/path/image.jpeg';
echo substr($filename, strrpos($filename, '.') + 1);</pre>
<p>Howerver there is a more elegant solution.</p>
<pre lang="php">$filename = '/my/path/image.jpeg';
echo strtolower(pathinfo($filename, PATHINFO_EXTENSION));</pre>
<p>Thus you rely on PHP built in functions and it&#8217;s harder to overlook the exact string manipulation approach.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/10/04/some-php-tips-basename/" rel="bookmark" title="Some PHP Tips: basename()">Some PHP Tips: basename() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ffmpeg, libx264 and presets</title>
		<link>/2010/10/26/ffmpeg-libx264-and-presets/</link>
		<comments>/2010/10/26/ffmpeg-libx264-and-presets/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 12:11:00 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[Application software]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[FFmpeg]]></category>
		<category><![CDATA[Media technology]]></category>
		<category><![CDATA[NDs-mPeG]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Video codecs]]></category>
		<category><![CDATA[Video editing software]]></category>
		<category><![CDATA[web community]]></category>

		<guid isPermaLink="false">/?p=2019</guid>
		<description><![CDATA[When working with FFMPEG you can convert/encode in various formats, but in my case this should be an MP4 file, encoded with libx264. This combination is quite well known in the web community, because those videos are playable under almost any flash player and Apple products. Now the problem is that this comes with very &#8230; <a href="/2010/10/26/ffmpeg-libx264-and-presets/" class="more-link">Continue reading <span class="screen-reader-text">ffmpeg, libx264 and presets</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/04/12/an-ffmpeg-question-why-phps-exec-doesnt-return-the-command-output/" rel="bookmark" title="An FFMPEG Question &#8211; Why PHP&#8217;s exec() Doesn&#8217;t Return the Command Output">An FFMPEG Question &#8211; Why PHP&#8217;s exec() Doesn&#8217;t Return the Command Output </a></li>
<li><a href="/2010/11/12/how-to-make-mp4-progressive-with-qt-faststart/" rel="bookmark" title="How to Make MP4 Progressive with qt-faststart">How to Make MP4 Progressive with qt-faststart </a></li>
<li><a href="/2011/01/27/few-thoughts-on-web-video/" rel="bookmark" title="Few Thoughts on Web Video">Few Thoughts on Web Video </a></li>
<li><a href="/2010/03/05/video-sites-must-use-mp4-and-only-mp4/" rel="bookmark" title="Video sites must use &#8230; mp4 and only mp4!">Video sites must use &#8230; mp4 and only mp4! </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>When working with FFMPEG you can convert/encode in various formats, but in my case this should be an MP4 file, encoded with libx264. This combination is quite well known in the web community, because those videos are playable under almost any flash player and Apple products.</p>
<p>Now the problem is that this comes with very large list of options, which are difficult to setup, especially for newbies like me. Than everything&#8217;s difficult, once because the resulting quality is not always the same as the input file&#8217;s quality.</p>
<h2>The Solution</h2>
<p>Is to use presets. Thankfully ffmpeg with libx264 can be used with presets, i.e. NORMAL, HD etc. which means that you don&#8217;t have to setup a command by hand and the resulting quality is the same as the input quality.</p>
<p>Here&#8217;s a sample command using normal preset:</p>
<pre lang="php">ffmpeg -i source.mp4 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre normal -threads 0 -crf 22 output.mp4
</pre>
<p>here the source can be either mp4 or some other file format as FLV for instance.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/04/12/an-ffmpeg-question-why-phps-exec-doesnt-return-the-command-output/" rel="bookmark" title="An FFMPEG Question &#8211; Why PHP&#8217;s exec() Doesn&#8217;t Return the Command Output">An FFMPEG Question &#8211; Why PHP&#8217;s exec() Doesn&#8217;t Return the Command Output </a></li>
<li><a href="/2010/11/12/how-to-make-mp4-progressive-with-qt-faststart/" rel="bookmark" title="How to Make MP4 Progressive with qt-faststart">How to Make MP4 Progressive with qt-faststart </a></li>
<li><a href="/2011/01/27/few-thoughts-on-web-video/" rel="bookmark" title="Few Thoughts on Web Video">Few Thoughts on Web Video </a></li>
<li><a href="/2010/03/05/video-sites-must-use-mp4-and-only-mp4/" rel="bookmark" title="Video sites must use &#8230; mp4 and only mp4!">Video sites must use &#8230; mp4 and only mp4! </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/10/26/ffmpeg-libx264-and-presets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery: Setting Up a Vector Path Fill Color</title>
		<link>/2010/09/27/jquery-setting-up-a-vector-path-fill-color/</link>
		<comments>/2010/09/27/jquery-setting-up-a-vector-path-fill-color/#respond</comments>
		<pubDate>Mon, 27 Sep 2010 17:37:24 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[Open formats]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[Scalable Vector Graphics]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vector path]]></category>

		<guid isPermaLink="false">/?p=1997</guid>
		<description><![CDATA[It&#8217;s quite unusual to think of the jQuery&#8217;s attr() method as a generic method that can only change basic attributes as value, style, etc. However attr() can change whatever DOM element attribute. In this case you may know that the SVG path element can have a fill attribute, so you can simply setup a: $('path').attr('fill', &#8230; <a href="/2010/09/27/jquery-setting-up-a-vector-path-fill-color/" class="more-link">Continue reading <span class="screen-reader-text">jQuery: Setting Up a Vector Path Fill Color</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/09/13/looping-animation-with-javascript-and-raphael/" rel="bookmark" title="Looping Animation with JavaScript and Raphaël">Looping Animation with JavaScript and Raphaël </a></li>
<li><a href="/2010/04/06/jquery-what-about-this-title/" rel="bookmark" title="jQuery: what about this.title?">jQuery: what about this.title? </a></li>
<li><a href="/2010/03/31/jquery-get-the-id-of-the-current-element/" rel="bookmark" title="jQuery Get the Id of the Current Element">jQuery Get the Id of the Current Element </a></li>
<li><a href="/2010/06/22/jquery-get-the-selected-option-text/" rel="bookmark" title="jQuery: Get the Selected Option Text">jQuery: Get the Selected Option Text </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s quite unusual to think of the jQuery&#8217;s attr() method as a generic method that can only change basic attributes as value, style, etc. However attr() can change whatever DOM element attribute. In this case you may know that the SVG path element can have a fill attribute, so you can simply setup a:</p>
<pre lang="javascript">
$('path').attr('fill', '#ccc');
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/09/13/looping-animation-with-javascript-and-raphael/" rel="bookmark" title="Looping Animation with JavaScript and Raphaël">Looping Animation with JavaScript and Raphaël </a></li>
<li><a href="/2010/04/06/jquery-what-about-this-title/" rel="bookmark" title="jQuery: what about this.title?">jQuery: what about this.title? </a></li>
<li><a href="/2010/03/31/jquery-get-the-id-of-the-current-element/" rel="bookmark" title="jQuery Get the Id of the Current Element">jQuery Get the Id of the Current Element </a></li>
<li><a href="/2010/06/22/jquery-get-the-selected-option-text/" rel="bookmark" title="jQuery: Get the Selected Option Text">jQuery: Get the Selected Option Text </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/27/jquery-setting-up-a-vector-path-fill-color/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Media RSS and ZF &#8211; Part 2</title>
		<link>/2010/07/14/media-rss-and-zf-part-2/</link>
		<comments>/2010/07/14/media-rss-and-zf-part-2/#respond</comments>
		<pubDate>Wed, 14 Jul 2010 19:05:15 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Software architecture]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">/?p=1803</guid>
		<description><![CDATA[Here&#8217;s the promised chunk of code making the most simple bridge between Zend Framework and Media RSS. Step 1 Just add a simple action in a controller: class IndexController extends Zend_Controller_Action { public function indexAction() { $this->getResponse()->setHeader('Content-Type', 'application/xml'); $this->view->somevar = 'some value'; echo $this->view->render('index/index.xml'); $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); } } Step 2 After you&#8217;ve setup the xml &#8230; <a href="/2010/07/14/media-rss-and-zf-part-2/" class="more-link">Continue reading <span class="screen-reader-text">Media RSS and ZF &#8211; Part 2</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/13/zend-framework-and-media-rss/" rel="bookmark" title="Zend Framework and Media RSS">Zend Framework and Media RSS </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s the promised chunk of code making the most simple bridge between Zend Framework and Media RSS.</p>
<h2>Step 1</h2>
<p>Just add a simple action in a controller:</p>
<pre lang="php">
class IndexController extends Zend_Controller_Action
{
	public function indexAction()
	{
	    $this->getResponse()->setHeader('Content-Type', 'application/xml');
	    $this->view->somevar = 'some value';
	
	    echo $this->view->render('index/index.xml');
	
	    $this->_helper->layout()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender(true);
	}
}
</pre>
<h2>Step 2</h2>
<p>After you&#8217;ve setup the xml file as a view script &#8211; you can simply access it as a normal Zend Framework view:</p>
<pre lang="xml">
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:av="http://www.searchvideo.com/schemas/av/1.0">
  <channel>
   <title><?php echo $this->somevar ?></title>
   ...
   other media rss elements
   ...
</channel>
</rss>
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/13/zend-framework-and-media-rss/" rel="bookmark" title="Zend Framework and Media RSS">Zend Framework and Media RSS </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/14/media-rss-and-zf-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework and Media RSS</title>
		<link>/2010/07/13/zend-framework-and-media-rss/</link>
		<comments>/2010/07/13/zend-framework-and-media-rss/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 19:23:32 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Content syndication markup language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[possible solutions]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=1795</guid>
		<description><![CDATA[The Problem Since native RSS format is not designed to deliver media content, there is a need for newer format supporting video and audio. There is such format &#8211; Media RSS introduced by Yahoo! in 2004. Actually now RSS supports some king of pseudo audio and video with the enclosure tag. Although there is a &#8230; <a href="/2010/07/13/zend-framework-and-media-rss/" class="more-link">Continue reading <span class="screen-reader-text">Zend Framework and Media RSS</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/14/media-rss-and-zf-part-2/" rel="bookmark" title="Media RSS and ZF &#8211; Part 2">Media RSS and ZF &#8211; Part 2 </a></li>
<li><a href="/2010/04/28/zend_datesetoptions-and-format_type-in-zend-framework-1-10-3/" rel="bookmark" title="Zend_Date::setOptions and format_type in Zend Framework 1.10.3">Zend_Date::setOptions and format_type in Zend Framework 1.10.3 </a></li>
<li><a href="/2010/04/30/burn-feeds-in-zend-framework/" rel="bookmark" title="Burn Feeds in Zend Framework">Burn Feeds in Zend Framework </a></li>
<li><a href="/2010/08/23/can-twitter-replace-the-rss-feed-readers/" rel="bookmark" title="Can Twitter Replace the RSS Feed Readers">Can Twitter Replace the RSS Feed Readers </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>The Problem</h2>
<p>Since native RSS format is not designed to deliver media content, there is a need for newer format supporting video and audio. There is such format &#8211; <a title="Media Rss Yahoo!" href="http://video.search.yahoo.com/mrss" target="_blank">Media RSS</a> introduced by Yahoo! in 2004. Actually now RSS supports some king of pseudo audio and video with the enclosure tag.</p>
<p><a href="/wp-content/uploads/2010/07/radio-media.jpeg"><img class="aligncenter size-full wp-image-1804" title="Radio Set" src="/wp-content/uploads/2010/07/radio-media.jpeg" alt="Inserting Media In RSS with Media RSS" width="430" height="322" srcset="/wp-content/uploads/2010/07/radio-media.jpeg 430w, /wp-content/uploads/2010/07/radio-media-300x224.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>Although there is a standardized format, there is not a Zend Framework native module. There is however a <a title="Zend Framework and Media RSS" href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Feed_Writer_Extension_MediaRSS+-+Justin+Hart" target="_blank">proposal</a>, but that&#8217;s far not enough.</p>
<h2>Possible Solutions</h2>
<p>For me there are two possible solutions. The first is writing Zend_Feed_Mrss and extend the <a href="http://framework.zend.com/apidoc/core/Zend_Feed/Zend_Feed_Rss.html" target="_blank">Zend_Feed_Rss</a> functionality, but that appears to be a non-trivial task. The second solution is to write custom view returning XML, which I&#8217;m choosing for now. I hope I can paste some code soon!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/14/media-rss-and-zf-part-2/" rel="bookmark" title="Media RSS and ZF &#8211; Part 2">Media RSS and ZF &#8211; Part 2 </a></li>
<li><a href="/2010/04/28/zend_datesetoptions-and-format_type-in-zend-framework-1-10-3/" rel="bookmark" title="Zend_Date::setOptions and format_type in Zend Framework 1.10.3">Zend_Date::setOptions and format_type in Zend Framework 1.10.3 </a></li>
<li><a href="/2010/04/30/burn-feeds-in-zend-framework/" rel="bookmark" title="Burn Feeds in Zend Framework">Burn Feeds in Zend Framework </a></li>
<li><a href="/2010/08/23/can-twitter-replace-the-rss-feed-readers/" rel="bookmark" title="Can Twitter Replace the RSS Feed Readers">Can Twitter Replace the RSS Feed Readers </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/13/zend-framework-and-media-rss/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Download Files with Zend Framework</title>
		<link>/2010/05/25/download-files-with-zend-framework/</link>
		<comments>/2010/05/25/download-files-with-zend-framework/#comments</comments>
		<pubDate>Tue, 25 May 2010 14:06:13 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Graphics file formats]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Social Issues]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web application frameworks]]></category>

		<guid isPermaLink="false">/?p=1571</guid>
		<description><![CDATA[Download a File The title may sound unclear enough, but the task is simple. You&#8217;ve to make a file download process within a Zend Framework&#8217;s controller. Let&#8217;s assume we&#8217;ve the DownloadController setup:<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/" rel="bookmark" title="Models in Zend Framework &#8211; Initialize All Methods with init()">Models in Zend Framework &#8211; Initialize All Methods with init() </a></li>
<li><a href="/2010/04/07/upload-a-file-with-zend-framework/" rel="bookmark" title="Upload a File with Zend Framework">Upload a File with Zend Framework </a></li>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Download a File</h2>
<p>The title may sound unclear enough, but the task is simple. You&#8217;ve to make a file download process within a Zend Framework&#8217;s controller. Let&#8217;s assume we&#8217;ve the DownloadController setup:</p>
<pre lang="php">
<?php

class DownloadController extends Zend_Controller_Action
{
	public function indexAction()
	{}	
}
</pre>
<p>In PHP there are at least three simple lines of code that will do the job of downloading a file.</p>
<pre lang="php">
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="logo.jpg"');
readfile('images/logo.jpg');
</pre>
<p>Note that here there is a content-type header, which is important cause the browsers understands what kind of file is supposed to be downloaded. The second line suggests a name of the downloaded file and the third one returns the file to the client.</p>
<h2>Download a File ... within Zend Framework</h2>
<p>Those three lines wont work alone in a ZF application, because there's no view, but even if you create a .phtml (view) file for this action it won't work, because the header of the returned file is modified.</p>
<p>The question is how to possibly return the file for download, perhaps write some statistics to the database and if there's a problem (some permission issues for instance) return a message to the user.</p>
<h2>The Basic Solution</h2>
<p>The solution is simple. First make it work by disabling the view and possibly the layout for this action:</p>
<pre lang="php">
public function indexAction()
{
	header('Content-Type: image/jpeg');
	header('Content-Disposition: attachment; filename="logo.jpg"');
	readfile('images/logo.jpg');
	
	// disable the view ... and perhaps the layout
	$this->view->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
}
</pre>
<p>Than add some code where you can check the permissions. Just because there's no view for this action you can redirect to another - errorAction():</p>
<pre lang="php">
public function indexAction()
{
    if (userHasNoPermissions) {
        $this->view->msg = 'This file cannot be downloaded!';
        $this->_forward('error', 'download');
    }

    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename="logo.jpg"');
    readfile('images/logo.jpg');

    // disable layout and view
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
}
</pre>
<p>But that still will prompt you a file to download, so there should be a return statement that will return false:</p>
<pre lang="php">
public function indexAction()
{
    if (userHasNoPermissions) {
        $this->view->msg = 'This file cannot be downloaded!';
        $this->_forward('error', 'download');
        return FALSE;
    }

    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename="logo.jpg"');
    readfile('images/logo.jpg');

    // disable layout and view
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
}
</pre>
<p>So here's the complete source of DownloadController.php:</p>
<pre lang="php">
<?php

class DownloadController extends Zend_Controller_Action
{
	public function indexAction()
	{
	    if (userHasNoPermissions) {
	        $this->view->msg = 'This file cannot be downloaded!';
	        $this->_forward('error', 'download');
	        return FALSE;
	    }
	
	    header('Content-Type: image/jpeg');
	    header('Content-Disposition: attachment; filename="logo.jpg"');
	    readfile('images/logo.jpg');
	
	    // disable layout and view
	    $this->view->layout()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender(true);
	}	
	
	public function errorAction()
	{}
}
</pre>
<p>and the error.phtml:</p>
<pre lang="php">
<?php echo $this->msg ?>
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/" rel="bookmark" title="Models in Zend Framework &#8211; Initialize All Methods with init()">Models in Zend Framework &#8211; Initialize All Methods with init() </a></li>
<li><a href="/2010/04/07/upload-a-file-with-zend-framework/" rel="bookmark" title="Upload a File with Zend Framework">Upload a File with Zend Framework </a></li>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/05/25/download-files-with-zend-framework/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
