<?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>KADIMI &#187; PHP</title>
	<atom:link href="http://www.kadimi.com/en/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kadimi.com/en</link>
	<description>[Web developper, Linux addict, Technical translator...]</description>
	<lastBuildDate>Sun, 15 Jan 2012 10:48:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>PHP: remove an array item by value</title>
		<link>http://www.kadimi.com/en/php-remove-an-array-item-by-value-719</link>
		<comments>http://www.kadimi.com/en/php-remove-an-array-item-by-value-719#comments</comments>
		<pubDate>Fri, 01 Oct 2010 00:41:14 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=719</guid>
		<description><![CDATA[It isn&#8217;t quite obvious to remove an array item when all you know is the value of that item, &#8211; i.e. you don&#8217;t now the item index. This line of code is what you need: $the_array = array_values&#40;array_diff&#40;$the_array,array&#40;'the_value'&#41;&#41;&#41;;]]></description>
			<content:encoded><![CDATA[<p>It isn&#8217;t quite obvious to remove an array item when all you know is the value of that item, &#8211; i.e. you don&#8217;t now the item index.</p>
<p>This line of code is what you need:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$the_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_values</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$the_array</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_value'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/php-remove-an-array-item-by-value-719/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP with IMAP on CentOS</title>
		<link>http://www.kadimi.com/en/centos-php-imap-506</link>
		<comments>http://www.kadimi.com/en/centos-php-imap-506#comments</comments>
		<pubDate>Mon, 15 Mar 2010 10:46:35 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[CentOs]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[imap]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=506</guid>
		<description><![CDATA[I was playing with Drupal 6 and some UberCart ticket module when I came across an error message where Drupal told me that it needed the IMAP PHP extension to be installed. Because I&#8217;m a nice guy I will share this command with you: 1 &#91;me@it ~&#93;# yum install php-imap Now it&#8217;s installed, proof: 1 [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing with Drupal 6 and some UberCart ticket module when I came across an error message where Drupal told me that it needed the IMAP PHP extension to be installed.</p>
<p>Because I&#8217;m a nice guy I will share this command with you:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>me<span style="color: pink;">@</span>it ~<span style="color: #000000;">&#93;</span><span style="color: #008000;"># yum install php-imap</span></pre></td></tr></table></div>

<p>Now it&#8217;s installed, proof:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>me<span style="color: pink;">@</span>it ~<span style="color: #000000;">&#93;</span><span style="color: #008000;"># php --ri imap</span>
&nbsp;
imap
&nbsp;
IMAP c<span style="color: pink;">-</span>Client Version <span style="color: pink;">=&gt;</span> <span style="color: #804000;">2004</span>
SSL Support <span style="color: pink;">=&gt;</span> enabled
Kerberos Support <span style="color: pink;">=&gt;</span> enabled
<span style="color: #000000;">&#91;</span>me<span style="color: pink;">@</span>it ~<span style="color: #000000;">&#93;</span><span style="color: #008000;">#</span></pre></td></tr></table></div>

<p>If it wasn&#8217;t installed it would have shown you:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>me<span style="color: pink;">@</span>it ~<span style="color: #000000;">&#93;</span><span style="color: #008000;"># php --ri imap</span>
Extension <span style="color: #800000;">'imap'</span> not present.</pre></td></tr></table></div>

<p>But of course it isn&#8217;t working&#8230; yet, untill you restart the Apache server daemon</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>me<span style="color: pink;">@</span>it ~<span style="color: #000000;">&#93;</span><span style="color: #008000;"># service httpd restart</span></pre></td></tr></table></div>

<p>And because I&#8217;m a nice guy I will tell you where I found the first command before even you ask: <a href="http://www.php.net/manual/en/imap.setup.php">http://www.php.net/manual/en/imap.setup.php</a></p>
<p>The PHP manual is always a good place to start, oh&#8230; and don&#8217;t underestimate comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/centos-php-imap-506/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get current URL in PHP</title>
		<link>http://www.kadimi.com/en/get-current-url-function-486</link>
		<comments>http://www.kadimi.com/en/get-current-url-function-486#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:29:24 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=486</guid>
		<description><![CDATA[Here is a function for getting the URL of the page being processed in PHP, the function accepts one optional parameter $ignore_port_80 (boolean) to tell it whether it should ignore the port part of the url if the port used is 80: 1 2 3 4 5 6 7 8 9 10 11 12 13 [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a function for getting the URL of the page being processed in PHP, the function accepts one optional parameter <code>$ignore_port_80</code> (boolean) to tell it whether it should ignore the port part of the url if the port used is 80:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Function
</span><span style="color: #000000; font-weight: bold;">function</span> get_current_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ignore_port_80</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$is_https</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTPS'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'on'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$show_port</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_PORT'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">'80'</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$ignore_port_80</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span>
    <span style="color: #339933;">.</span> <span style="color: #0000ff;">'http'</span>
    <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$is_https</span> ? <span style="color: #0000ff;">'s'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">.</span> <span style="color: #0000ff;">'://'</span>
    <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_port</span> ? <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_PORT'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Examples
</span><span style="color: #b1b100;">echo</span> get_current_url<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;"># http://www.kadimi.com/en/some-title-123
</span><span style="color: #b1b100;">echo</span> get_current_url<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;"># same as above
</span><span style="color: #b1b100;">echo</span> get_current_url<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;"># http://www.kadimi.com:80/en/some-title-123</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/get-current-url-function-486/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Symfony Without PEAR</title>
		<link>http://www.kadimi.com/en/symfony-428</link>
		<comments>http://www.kadimi.com/en/symfony-428#comments</comments>
		<pubDate>Wed, 27 Jan 2010 11:05:31 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/test-428</guid>
		<description><![CDATA[A lot of web hosts have limited or nonexistent support for PEAR, the pear command will throw errors on most commands or not work at all, in order to install Symfony on such hosts you will need to download the Symfony package, copy Symfony files to your home directory and make them easily accessible from [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of web hosts have limited or nonexistent support for PEAR, the pear command will throw errors on most commands or not work at all, in order to install Symfony on such hosts you will need to download the Symfony package, copy Symfony files to your home directory and make them easily accessible from PHP scripts by modifying the include_path directive setting.</p>
<p>In order to complete these steps faster, we will prefer using SSH to download or edit files and to create folder instead of using FTP, FTP will take a lot of time compared to SSH&#8230; Let&#8217;s start.</p>
<h2>1 &#8211; Download and Install Symfony</h2>
<p>Login to your account using SSH, if you are already logged in make sure your current working directory is your home directory (~), then create a &#8220;php&#8221; folder where you will download the symfony package, we will use the same directory structure used on the Symfony documentation, this will allow you to follow the documentation examples without changing directory names etc.</p>
<p>Before issuing the following commands, grab the link of the latest symfony version from Symfony website, for the purposes of this tutorial you will have to download the  source .tgz file, the text  written in red font will be different depending on the Symfony version:</p>
<blockquote><p><code>you@yourdomain.com [/tmp]# cd<br />
you@yourdomain.com [~]# mkdir php<br />
you@yourdomain.com [~]# mkdir php/lib<br />
you@yourdomain.com [~]# mkdir php/lib/vendor<br />
you@yourdomain.com [~]# cd php/lib/vendor<br />
you@yourdomain.com [~/vendor]# wget http://www.symfony-project.org/get/symfony-<span style="color: #ff0000;">1.4.1</span>.tgz<br />
<span style="color: #999999;"> --06:15:44--  http://www.symfony-project.org/get/symfony-1.4.1.tgz<br />
=&gt; `symfony-1.4.1.tgz.2'<br />
Resolving www.symfony-project.org... 217.174.222.79<br />
Connecting to www.symfony-project.org|217.174.222.79|:80... connected.<br />
HTTP request sent, awaiting response... 200 OK<br />
Length: 3,210,838 (3.1M) [application/x-gzip]100%[===================================================================&gt;] 3,210,838      1.34M/s<br />
06:15:48 (1.34 MB/s) - `symfony-1.4.1.tgz.2' saved [3210838/3210838]<br />
</span>you@yourdomain.com [~/vendor]#  tar zxpf symfony-<span style="color: #ff0000;">1.4.1</span>.tgz<br />
you@yourdomain.com [~/vendor]#  mv symfony-<span style="color: #ff0000;">1.4.1</span> symfony<br />
you@yourdomain.com [~/vendor]# rm -fr symfony-*<br />
</code></p></blockquote>
<h2>2 &#8211; Test your Symfony installation</h2>
<p>You can test your installation and display the symfony version:</p>
<blockquote><p><code>you@yourdomain.com [~]# php -q  php/lib/vendor/symfony/data/bin/symfony -V<br />
<span style="color: #999999;"> symfony version 1.4.1 (/home/you/php/lib/vendor/symfony/lib)</span></code></p></blockquote>
<h2>3 &#8211; Tell PHP where to find Symfony files</h2>
<p>Symfony files are now available in your server, but you still need to tell php where to find them easily. For that you need to modify your PHP include_path and add the &#8220;php&#8221; folder.</p>
<p>We need to know the php folder absolute path, for this we will &#8220;cd&#8221; to the &#8220;php&#8221; directory and issue the &#8220;pwd&#8221; command:</p>
<blockquote><p><code>you@yourdomain.com [~]# cd ~/php<br />
you@yourdomain.com [~]# pwd<br />
/home3/you/php<br />
</code></p></blockquote>
<p>-or-</p>
<blockquote><p><code>you@yourdomain.com [~]# find ~/php -name php<br />
/home3/you/php<br />
</code></p></blockquote>
<p>There are different solutions for altering your PHP include_path, you will choose the one that meets your needs.<br />
[See: How to modify PHP include_path]</p>
<p>In this tutorial, I will assume that you are relying on a global php.ini file .</p>
<p>Open you php.ini file for edit</p>
<blockquote><p><code>you@yourdomain.com [~]# nano /x/y/z/php.ini<br />
</code></p></blockquote>
<p>Look for the include_path directive setting, something like this:</p>
<blockquote><p><code>include_path = ".:/usr/lib/php:/usr/local/lib/php"<br />
</code></p></blockquote>
<p>you can find it easily by using nano&#8217;s search featrure:</p>
<ul>
<li>Press CTRL+W</li>
<li>Type the search string and press RETURN, we are interested in the string <strong>include_path</strong></li>
<li>Press CTRL+W then RETURN until you find something that resembles the code above.</li>
</ul>
<p>Add the php absolute path to your php include_path like this</p>
<p><code>include_path = ".:/usr/lib/php:/usr/local/lib/php<strong>:/home3/you/php</strong>"</code></p>
<h3><span style="text-decoration: underline;">Conclusion</span></h3>
<p>At this point you can use symphony almost as if you installed it using the PEAR pear command. You can use the same method to install other PEAR packages. One more thing you can do is to alter your shell PATH to add the symfony command:</p>
<p>Open .bash_profile add the following line AT THE END OF THE FILE:</p>
<blockquote><p><code>PATH=$PATH:$HOME/php/lib/vendor/symfony/data/bin/<br />
export PATH</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/symfony-428/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check a file type efficiently in PHP?</title>
		<link>http://www.kadimi.com/en/file-type-265</link>
		<comments>http://www.kadimi.com/en/file-type-265#comments</comments>
		<pubDate>Fri, 07 Aug 2009 11:00:08 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=265</guid>
		<description><![CDATA[The Problem Do you know a method in PHP to check the type of an uploaded file without relying on extension or Mime-type because these two can be faked? Verifying the file type in PHP Relying on the Mime-type of a file submitted by a user is a bad practice, using the extension is even [...]]]></description>
			<content:encoded><![CDATA[<h4>The Problem</h4>
<p>Do you know a method in PHP to check the type of an uploaded file<br />
without relying on extension or Mime-type because these two can be faked?</p>
<h2>Verifying the file type in PHP</h2>
<p>Relying on the Mime-type of a file submitted by a user is a bad practice, using the extension is even worse!<br />
Some people use functions of the <strong>php fileinfo</strong> extension, these functions do not rely on file extensions but do rely on bytes sections of the file that can be easily faked.</p>
<p>You can retrieve the mime type with finfo_file() like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.gif&quot;</span>
&nbsp;
<span style="color: #000088;">$finfo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">finfo_open</span><span style="color: #009900;">&#40;</span>FILEINFO_MIME_TYPE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$file_mime_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">finfo_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finfo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">finfo_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finfo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file_mime_type</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &lt;strong&gt;Expected ouput:&lt;/strong&gt; image/gif</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So you can use the php fileinfo functions to determine if the file is what it claims to be, I mean that the file extension matches the result of <strong>finfo_file()</strong>, if it doesn&#8217;t, there is no need to go further, the file should be rejected from being stored on your server and from beig served to your website visitors.</p>
<p>If the extension matches the MIME type we will have to go further and check if the files is really a GIF/JPEG/ZIP file or a fake file with malicious code.</p>
<p>For that we need to have a set of functions for the MIME types we are interested in, for images for example (TIFF, PNG, GIF, JPEG&#8230;), there is a good function that uses the <strong>identify</strong> linux command, modify it to suit your needs</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> is_jpg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullpathtoimage</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/usr/bin/identify -format %m <span style="color: #006699; font-weight: bold;">$fullpathtoimage</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//using system() echos STDOUT automatically</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//identify returns an empty result if the file is not an image</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'JPEG'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Original code written by <a href="http://us3.php.net/manual/en/function.finfo-file.php#77481">Ryan Day</a></p>
<p>Since I do not use file uploads, I can not provide functions for other file types, but I will search for them and edit my post if I find any.</p>
<p>There&#8217;s another solution that should work for all types but I&#8217;m not sure about it, it uses the linux <strong>file</strong> command:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>php<span style="color: #339933;">&lt;/</span>code<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">function</span> file_mime_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullpathtoimage</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/usr/bin/file -bi <span style="color: #006699; font-weight: bold;">$fullpathtoimage</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">';'</span><span style="color: #339933;">,</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$mime_type</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Test</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>file_mime_type<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs: text/x-c++ which is ok</span>
<span style="color: #339933;">&lt;</span>code<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/file-type-265/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Print HTML code from JavaScript variable</title>
		<link>http://www.kadimi.com/en/html-code-variable-235</link>
		<comments>http://www.kadimi.com/en/html-code-variable-235#comments</comments>
		<pubDate>Wed, 05 Aug 2009 06:35:11 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=235</guid>
		<description><![CDATA[This also was about to drive me crazy so I will not let it go without writing about it.. Let&#8217;s say you have some long HTML code contained on a PHP variable, something like this 1 2 3 4 5 6 7 &#60;?php $some_div = ' &#60;div id=&#34;box&#34;&#62; &#60;h1&#62;Title&#60;/h1&#62; &#60;p&#62;Article contents + comments&#60;/p&#62; &#60;div&#62;'; ?&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>This also was about to drive me crazy so I will not let it go without writing about it..</p>
<p>Let&#8217;s say you have some long HTML code contained on a PHP variable, something like this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$some_div</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
&lt;div id=&quot;box&quot;&gt;
  &lt;h1&gt;Title&lt;/h1&gt;
  &lt;p&gt;Article contents + comments&lt;/p&gt;
&lt;div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now you want to use this HTML code as a JavaScript variable instead of printing it directly, you will tell me you&#8217;ll do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
/* &lt;![CDATA[ */
  var some_div = '<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$some_div</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>';
/* ]]&gt; */
&lt;/script&gt;</pre></td></tr></table></div>

<p>JavaScript doesn&#8217;t like unescaped new lines and special characters (mainly &#8220;, &lt;  and &gt;) too much, so the previous code won&#8217;t work, my quick solution for that is to do the following:</p>
<ul>
<li>Encode the html code in PHP using urlencode</li>
<li>Decode it within JavaScript</li>
</ul>
<p>When using PHP&#8217;s urlencode function, all whitespace characters will be replaced by + signs, so we&#8217;d better deal with those characters before encoding them,  otherwise, we won&#8217;t make a difference between spaces and real + signs, please read the comment on the PHP code below to understand:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// replace consecutive white space characters with a single space</span>
<span style="color: #000088;">$some_div_2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s+/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$some_div</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Explode (cut the string on the white space and put the resulting part on an array)</span>
<span style="color: #000088;">$some_div_2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$some_div_2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Encode the text contained on the array cells</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$some_div_2</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000088;">$some_div_2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Implode (reassemble) the array to a string again</span>
<span style="color: #000088;">$some_div_2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$some_div_2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;script type=&quot;text/javascript&quot;&gt;
/* &lt;![CDATA[ */
&nbsp;
  // Import the variable from PHP
  var some_div = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$some_div_2</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
  // Decode it
  some_div_2 = unescape(decodeURI(some_div));
&nbsp;
  // Display it
  document.writeln(some_div_2);
&nbsp;
/* ]]&gt; */
&lt;/script&gt;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/html-code-variable-235/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get file extension on PHP</title>
		<link>http://www.kadimi.com/en/get-file-extension-220</link>
		<comments>http://www.kadimi.com/en/get-file-extension-220#comments</comments>
		<pubDate>Tue, 04 Aug 2009 04:43:04 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=220</guid>
		<description><![CDATA[There are many ways to retrieve files extension with PHP, but we will have to choose one that feets our needs and gives more reliable results. When dealing with file extension you should know that a file extension is not always 3 characters long (ex:  foto.jpeg, index.html) and that it doesn&#8217;t start after the last [...]]]></description>
			<content:encoded><![CDATA[<p>There are many ways to retrieve files extension with PHP, but we will have to choose one that feets our needs and gives more reliable results.<br />
When dealing with file extension you should know that a file extension is not always 3 characters long (ex:  foto.jpeg, index.html) and that it doesn&#8217;t start after the last dot (.) in the file name (ex: program.tar.gz, config.php.inc).</p>
<p>Let&#8217;s write the <strong>filename_details</strong> function that we will use to retrieve the extension of the file module.tar.bz or any file you have:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> filename_details<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dots</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$filename_no_ext</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filename</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dots</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$temp_arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename_no_ext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp_arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$extension</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$filename_no_ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$temp_arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename_no_ext</span><span style="color: #339933;">,</span> <span style="color: #000088;">$extension</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>filename_details<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'module.tar.bz'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Output: 
Array
(
  [0] =&gt; module
  [1] =&gt; tar.bz
)
//*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/get-file-extension-220/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install PHP 5.2.6 on CentOs 5.x</title>
		<link>http://www.kadimi.com/en/centos-php-5-2-6-174</link>
		<comments>http://www.kadimi.com/en/centos-php-5-2-6-174#comments</comments>
		<pubDate>Wed, 24 Jun 2009 10:27:22 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[CentOs]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=174</guid>
		<description><![CDATA[Oddly enough, PHP 5.2.6 is still not available on the default CentOS repositories ([base], [updates], [addons], and [extras]), the latest PHP version available in those repos is PHP 5.1.6! In order to install PHP 5.2.6 you can add the CentOS testing repository and enable it then install or update PHP. To add the CentOS testing [...]]]></description>
			<content:encoded><![CDATA[<p>Oddly enough, <strong>PHP 5.2.6</strong> is still not available on the default CentOS repositories ([base], [updates], [addons], and [extras]), the latest PHP version available in those repos is PHP 5.1.6!</p>
<p>In order to install PHP 5.2.6 you can add the CentOS testing repository and enable it then install or update PHP.</p>
<p>To add the CentOS testing directory, create a file /etc/yum.repos.d/centos-test.repo and add this line:</p>
<blockquote><p><code>[c5-testing]<br />
name=CentOS-5 Testing<br />
baseurl=http://dev.centos.org/centos/5/testing/$basearch/<br />
enabled=1<br />
gpgcheck=1<br />
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing<br />
</code></p></blockquote>
<p>Then install or update PHP:</p>
<blockquote><p><code>[root@server ~]# yum install php</code></p></blockquote>
<p>or</p>
<blockquote><p><code>[root@server ~]# yum update php</code></p></blockquote>
<p>Then you can disable the testing repo by editing the file and setting the option &#8220;enabled&#8221; to &#8220;0&#8243;</p>
<blockquote><p><code>[c5-testing]<br />
name=CentOS-5 Testing<br />
baseurl=http://dev.centos.org/centos/5/testing/$basearch/<br />
enabled=0<br />
gpgcheck=1<br />
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/centos-php-5-2-6-174/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smarty error: Warning: preg_match() [function.preg-match]: Compilation failed: repeated subpattern is too long at offset 18454 in &#8230;</title>
		<link>http://www.kadimi.com/en/smarty-error-162</link>
		<comments>http://www.kadimi.com/en/smarty-error-162#comments</comments>
		<pubDate>Tue, 23 Jun 2009 17:50:42 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=162</guid>
		<description><![CDATA[Warning: preg_match() [function.preg-match]: Compilation failed: repeated subpattern is too long at offset 18454 in /var/www/html/[...]/Smarty/Smarty_Compiler.class.php on line 454 It looks that you are running an old version of Smarty, latest version as I write is 2.6.26, you can download it from here.]]></description>
			<content:encoded><![CDATA[<p><strong>Warning: preg_match() [function.preg-match]: Compilation failed: repeated subpattern is too long at offset 18454 in /var/www/html/[...]/Smarty/Smarty_Compiler.class.php on line 454</strong></p>
<p>It looks that you are running an old version of Smarty, latest version as I write is 2.6.26, you can download it from <a href="http://www.smarty.net/download.php" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/smarty-error-162/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check if a number is odd or even with PHP?</title>
		<link>http://www.kadimi.com/en/odd-even-45</link>
		<comments>http://www.kadimi.com/en/odd-even-45#comments</comments>
		<pubDate>Tue, 13 Jan 2009 18:37:20 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=45</guid>
		<description><![CDATA[In mathematics, one of the definitions of odd and even number is that they can be expressed like this (where n is the number and k is an integer): Odd numbers: n = 2k +1 Even numbers: n = 2k In PHP, we can check a number&#8217;s parity by checking the remainider of the division [...]]]></description>
			<content:encoded><![CDATA[<p>In mathematics, one of the definitions of odd and even number is that they can be expressed like this (where n is the number and k is an integer):</p>
<blockquote><p><strong>Odd numbers:</strong> n = 2k +1</p></blockquote>
<blockquote><p><strong>Even numbers:</strong> n = 2k</p></blockquote>
<p>In PHP, we can check a number&#8217;s parity by checking the remainider of the division of this number by 2 <span id="more-45"></span>using the modulus operator (%), if the remainder is 1, the number is odd, if the remainder is 0, the number is even, let&#8217;s write some functions for this:</p>
<blockquote>
<pre>&lt;?php
function is_odd($n){
  return (boolean) ($n % 2);
}

function is_even($n){
  if(is_odd($n))
    return FALSE;
  return TRUE;
}

$a = 1;
$b = 2;
$c = 3;

echo "$a is ",  is_odd($a) ? 'odd' : 'even' , '.&lt;br /&gt;';
echo "$b is ",  is_odd($b) ? 'odd' : 'even' , '.&lt;br /&gt;';
echo "$c is ",  is_even($c)? 'even' : 'odd' , '.&lt;br /&gt;';

?&gt;</pre>
</blockquote>
<p>The output in the browser will look like:</p>
<blockquote><p>1 is odd.<br />
2 is even.<br />
3 is odd.</p></blockquote>
<p>This is a simple way to do it, I used typecasting in the function <em>is_odd()</em> so it&#8217;s always return TRUE or FALSE, instead of 1 or 0.</p>
<h4>PHP modulus (%)</h4>
<p>Operands of modulus are converted to integers (by stripping the decimal part) before processing.</p>
<blockquote>
<pre>&lt;?php
echo    1 % 2;
echo 1.01 % 2;   // each of these three statements outputs 1.
echo 1.99 % 2;
?&gt;</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/odd-even-45/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

