<?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>Php Development &#187; php</title>
	<atom:link href="http://www.maheshchari.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.maheshchari.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 17 Jan 2012 02:42:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>stop browser cache</title>
		<link>http://www.maheshchari.com/stop-browser-cache/</link>
		<comments>http://www.maheshchari.com/stop-browser-cache/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 16:57:58 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=237</guid>
		<description><![CDATA[How to stop the web browser caching the document All web documents, media and other web resources retrieved by a Web browser, are often saved, or cached, locally on the user&#8217;s hard drive. The next time that media or web resources is requested the Web browser may load the file from the cache instead of [...]]]></description>
			<content:encoded><![CDATA[<h1>How to stop the web browser caching the document</h1>
<p>All <strong>web  documents</strong>, <strong>media</strong> and other web <strong>resources</strong> retrieved by a <strong>Web browser</strong>, are often <strong>saved</strong>, or <strong>cached</strong>, <strong>locally</strong> on the user&#8217;s <strong>hard drive</strong>. The next time that <strong>media</strong> or <strong>web resources</strong> is requested the <strong>Web browser</strong> may load the file from the cache instead of downloading it over the <strong>internet</strong>. This might be <strong>desirable</strong> for a some web document content doesn&#8217;t change often but undesirable for documents are updated frequently with new content or information.<br />
All <strong>web servers</strong> sends to the browser with  some basic information about the document before it sent .in the above example we can see some of them.for full details please refer <a rel="nofollow" href="http://www.w3.org/Protocols/rfc2068/rfc2068" target="_blank">here</a> . we can use this server side <strong>headers</strong> to <strong>prevent</strong> browser <strong>caching</strong> .we learn here only <strong>PHP</strong> related headers.</p>
<h3>preventing browser cache with javascript.</h3>
<p>Here i am showing basic jQuery plugin that appends a random time to each links in document.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">nocachelink</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//itereate trough each element in given element collection</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> href<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>href.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//if query string is set or not for link</span>
			href<span style="color: #339933;">=</span>href<span style="color: #339933;">+</span><span style="color: #3366CC;">'?'</span><span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			href<span style="color: #339933;">=</span>href<span style="color: #339933;">+</span><span style="color: #3366CC;">'&amp;'</span><span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span>href<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update new href attribute</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>An example how to use it</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.nocache'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">nocachelink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>preventing browser cache with meta tags.</h3>
<p>to prevent cache by means of html meta tags add following tags to  document head section.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;&gt;
&lt;meta http-equiv=&quot;Expires&quot; content=&quot;-1&quot;&gt;
&lt;meta http-equiv=&quot;CACHE-CONTROL&quot; content=&quot;NO-CACHE&quot;&gt;</pre></div></div>

<h3>preventing browser cache with server side headers.</h3>
<p>we can also use HTTP headers by php also as below,remember you must add this lines to start of code.</p>

<div class="wp_syntax"><div class="code"><pre class="_1" style="font-family:monospace;">//  @-silenced or error suppress operater
	@header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
	@header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
	@header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
	@header( 'Pragma: no-cache' );</pre></div></div>

<h3>preventing browsers cache with ajax calls.</h3>
<p>we can also set headers with ajax response headers.</p>
<h3>preventing browser cache with other methods.</h3>
<p>By default all browser don&#8217;t cache <strong>SSL layered documents.</strong><br />
below example document won&#8217;t cached by browsers.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">https://www.yourdomain.com</pre></div></div>

<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=237&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/stop-browser-cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>multifile upload</title>
		<link>http://www.maheshchari.com/multifile-upload/</link>
		<comments>http://www.maheshchari.com/multifile-upload/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 12:09:45 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=179</guid>
		<description><![CDATA[Multiple file uploading tutorial with php We already discussed about increasing file upload size and simple file upload in my previous posts.now we discuss about the logic behind the multiple file upload. Here we create unlimited file fields on the fly with javascript ,so that there is no limit. uploader.php:-we process all the uploaded files [...]]]></description>
			<content:encoded><![CDATA[<h1>Multiple file uploading tutorial with php</h1>
<p>We already discussed about <a href="http://www.maheshchari.com/increase-php-file-upload-limit/">increasing file upload size</a> and <a href="http://www.maheshchari.com/simple-php-file-upload-tutorial/">simple file upload </a> in my previous posts.now we discuss about the logic behind the multiple file upload.<br />
Here we create unlimited file fields on the fly with javascript ,so that there is no limit.<br />
<strong>uploader.php:-</strong>we process all the uploaded files and show the results of uploaded files. remember to validate and filter the file types please view my previous post.</p>
<h3>Please Download Source Code To View it</h3>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2009/06/handling-multiple-file-upload-php.rar'>handling-multiple-file-upload-php</a></p>
<h3>Javascript Code</h3>

<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="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> add_file_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> container<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'file_container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> file_field<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
file_field.<span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'images[]'</span><span style="color: #339933;">;</span>
file_field.<span style="color: #660066;">type</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'file'</span><span style="color: #339933;">;</span>
container.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>file_field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> br_field<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'br'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
container.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>br_field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>HTML Code</h3>

<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="html" style="font-family:monospace;">&lt;form action=&quot;uploader.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; name=&quot;mutiple_file_upload_form&quot; id=&quot;mutiple_file_upload_form&quot;&gt;
  &lt;h1&gt;Advanced Multiple File Upload Script Example&lt;/h1&gt;&lt;div id=&quot;file_container&quot;&gt;
    &lt;input name=&quot;images[]&quot; type=&quot;file&quot;  /&gt;
    &lt;br /&gt;
  &lt;/div&gt;
  &lt;a href=&quot;javascript:void(0);&quot; onClick=&quot;add_file_field();&quot;&gt;Add another&lt;/a&gt;&lt;br /&gt;
  &lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<h3>PHP Code</h3>

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$number_of_file_fields</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$number_of_uploaded_files</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$number_of_moved_files</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$uploaded_files</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$upload_directory</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__file__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/uploaded/'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//set upload directory</span>
    <span style="color: #009933; font-style: italic;">/**
     * we get a $_FILES['images'] array ,
     * we procee this array while iterating with simple for loop 
     * you can check this array by print_r($_FILES['images']); 
     */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$number_of_file_fields</span><span style="color: #339933;">++;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//check if file field empty or not</span>
            <span style="color: #000088;">$number_of_uploaded_files</span><span style="color: #339933;">++;</span>
            <span style="color: #000088;">$uploaded_files</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$upload_directory</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$number_of_moved_files</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Number of File fields created <span style="color: #006699; font-weight: bold;">$number_of_file_fields</span>.&lt;br/&gt; &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Number of files submitted <span style="color: #006699; font-weight: bold;">$number_of_uploaded_files</span> . &lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Number of successfully moved files <span style="color: #006699; font-weight: bold;">$number_of_moved_files</span> . &lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;File Names are &lt;br/&gt;&quot;</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;">$uploaded_files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Download Source Code</h3>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2009/06/handling-multiple-file-upload-php.rar'>handling-multiple-file-upload-php</a><br />
<br/></p>
<h3>Screen Shots</h3>
<p><a href="http://www.maheshchari.com/wp-content/uploads/2009/06/handling-multiple-file-upload-example.jpg"><img src="http://www.maheshchari.com/wp-content/uploads/2009/06/handling-multiple-file-upload-example.jpg" alt="handling-multiple-file-upload-example" title="handling-multiple-file-upload-example" class="alignnone size-full wp-image-182" /></a><a href="http://www.maheshchari.com/wp-content/uploads/2009/06/handling-multiple-file-upload-result.jpg"><img src="http://www.maheshchari.com/wp-content/uploads/2009/06/handling-multiple-file-upload-result-300x130.jpg" alt="handling-multiple-file-upload-result" title="handling-multiple-file-upload-result" width="300" height="130" class="alignnone size-medium wp-image-183" /></a></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=179&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/multifile-upload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>email obfuscation</title>
		<link>http://www.maheshchari.com/email-obfuscation/</link>
		<comments>http://www.maheshchari.com/email-obfuscation/#comments</comments>
		<pubDate>Thu, 14 May 2009 11:47:37 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[anti spam]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=55</guid>
		<description><![CDATA[protecting E-mail Address on web pages with php, javascript from E-mail spammers E-mail Spam is flooding the Internet with many copies of the same message, in an attempt to force the message on people who would not otherwise choose to receive it.Most spam is Commercial advertising, often for dubious products, get-rich-quick schemes, or quasi-legal services. [...]]]></description>
			<content:encoded><![CDATA[<h1>protecting E-mail Address on web pages with php, javascript from E-mail spammers</h1>
<p>E-mail Spam is flooding the Internet with many copies of the same message, in an attempt to force the message on people who would not otherwise choose to receive it.Most spam is Commercial advertising, often for dubious products, get-rich-quick schemes, or quasi-legal services. Spam costs the sender very little to send &#8212; most of the costs are paid for by the recipient or the carriers rather than by the sender.</p>
<h3>How do they catch email address of users? Or how do they email harvesting?</h3>
<p>There are many ways to get users email address but here we talk about only two ways.</p>
<h3>From  web pages:</h3>
<p>Spammers have programs which spider through web pages, looking for email addresses, e.g. email addresses contained in mailto: HTML tags [those you can click on and get a mail window opened],a word contains @ symbol ,a word contains mailto:</p>
<h3>From various web contact or enquiry  forms.</h3>
<p>Some sites request various details via forms, e.g. guest books &amp; registrations forms.<br />
Spammers can get email addresses from those either because the form becomes available<br />
on the world wide web, or because the site sells / gives the emails list to others.<br />
some spammers use weak security web forms on certain websites to protect web form from spammers <a href="http://www.maheshchari.com/anti-spam-web-form/">please refer my post</a></p>
<h2>Preventing E-mail harvesting on web pages</h2>
<h3>e-mail Address munging or e-mail Obfuscation</h3>
<p>Address munging is the practice of disguising, or munging, to prevent it being automatically collected by spam bots.<br />
Some examples are</p>
<ul>
<li>no-one at example (dot) com</li>
<li>no-one@elpmaxe.com.invalid</li>
<li>moc.elpmaxe@eno-on</li>
<li>no-one@exampleREMOVEME.com.invalid</li>
<li>remove .invalid</li>
<li>no-one@exampleNOSPAM.com.invalid</li>
<li>n o &#8211; o n e @ e x a m p l e . c o m</li>
<li>no-one<em>@</em>example<em>.</em>com (as HTML)</li>
</ul>
<p>Even though the email harvesting reduced in measurable amount, with this method the main user has to predict the original. Now days some spam bots also overcomes this method.</p>
<h2>Using javascript </h2>
<p>Most of the spam bots don’t execute the javascript and they use regular expression to catch the address.<br />
So we can advance the method of munging with following examples</p>
<h3>For static web pages e-mail Obfuscation </h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'user'</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> at <span style="color: #339933;">=</span> <span style="color: #3366CC;">'@'</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> domain <span style="color: #339933;">=</span> <span style="color: #3366CC;">'example.com'</span><span style="color: #339933;">;</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span> <span style="color: #339933;">+</span> at <span style="color: #339933;">+</span> domain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>‘m’<span style="color: #339933;">+</span>’y’<span style="color: #339933;">+</span>’m’<span style="color: #339933;">+</span>’a’<span style="color: #339933;">+</span>’i’<span style="color: #339933;">+</span>’l’<span style="color: #339933;">+</span>’ <span style="color: #339933;">&amp;</span>#<span style="color: #CC0000;">64</span><span style="color: #339933;">;</span>’<span style="color: #339933;">+</span>’y’<span style="color: #339933;">+</span>’o’<span style="color: #339933;">+</span>’u’<span style="color: #339933;">+</span>’d’<span style="color: #339933;">+</span>’o’<span style="color: #339933;">+</span>’m’<span style="color: #339933;">+</span>’a’<span style="color: #339933;">+</span>’i’<span style="color: #339933;">+</span>’n’<span style="color: #339933;">+</span>’ <span style="color: #339933;">&amp;</span>#<span style="color: #CC0000;">46</span>’<span style="color: #339933;">+</span>’c’<span style="color: #339933;">+</span>’o’<span style="color: #339933;">+</span>’m’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #006600; font-style: italic;">//In the above example we use html equivalent ASCII chars &amp;#64 for  symbol and &amp;#46 for ‘.’ Character.</span></pre></td></tr></table></div>

<h3>
For dynamic web pages with PHP server side language e-mail Obfuscation</h3>

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> safe_asciimail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$length</span><span style="color: #339933;">=</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #b1b100;">return</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email</span><span style="color: #339933;">=</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email_array</span><span style="color: #339933;">=</span><span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ascii_email_array</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span>count<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$c</span><span style="color: #339933;">=</span><span style="color: #000088;">$email_array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ascii_email_array</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&amp;#'</span><span style="color: #339933;">.</span><span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</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: #009900;">&#125;</span>
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&lt;script&gt; document.write(&quot;'</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;">$ascii_email_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;);&lt;/script&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//We can call this method as follow </span>
<span style="color: #666666; font-style: italic;">//safe_asciimail(‘mymail@yourdomain.com’);</span></pre></td></tr></table></div>

<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=55&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/email-obfuscation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>simple PHP file upload tutorial</title>
		<link>http://www.maheshchari.com/simple-php-file-upload-tutorial/</link>
		<comments>http://www.maheshchari.com/simple-php-file-upload-tutorial/#comments</comments>
		<pubDate>Sat, 09 May 2009 06:19:34 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=48</guid>
		<description><![CDATA[A simple PHP file upload tutorial This PHP file upload tutorial is concentrated on mainly for beginners. Generally we face many problems in file uploading. Like checking fie extensions on client and server side, processing file as our requirement. For this we need two files called uploader.html and uploader.php In uploader.html we do form validation [...]]]></description>
			<content:encoded><![CDATA[<h1>A simple PHP file upload tutorial</h1>
<p>This PHP file upload tutorial is concentrated on mainly for beginners. Generally we face many problems in file uploading. Like checking fie extensions on client and server side, processing file as our requirement.<br />
For this we need two files called<strong> uploader.html</strong> and <strong>uploader.php</strong><br />
In <strong>uploader.html</strong> we do form validation and checking file extensions.<br />
In <strong>uploader.php</strong> we do check for user submitted file extensions.</p>
<h3>Some observations&lt;</h3>
<ul>
<li> Always-set form method to POST</li>
<li> Always-set form encodedtype to multipart/form-data</li>
<li> Check file type on client side and server side also.</li>
<li> Increase the script time limit and memory limit to upload large file.</li>
<li> Don’t use web method (this method) to upload larger than 500mb,instead use ftp upload interface.</li>
</ul>
<p>Generally the default maximum upload file size less than 8mb.<br />
To upload the larger or bigger files read <a href="http://www.maheshchari.com/increase-php-file-upload-limit/">increase file upload limit</a></p>
<h3>Add a simple Form to page as shown below</h3>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form id=&quot;uploadform&quot; action=&quot;uploader.php&quot; enctype=&quot;multipart/form-data&quot; method=&quot;post&quot;&gt;
  &lt;label&gt;Upload File
  &lt;input id=&quot;filefield&quot; name=&quot;filefield&quot; type=&quot;file&quot; /&gt;
  &lt;/label&gt;
  &lt;label&gt;
  &lt;input id=&quot;Upload&quot; name=&quot;Upload&quot; type=&quot;submit&quot; value=&quot;Upload&quot; /&gt;
  &lt;!--
  This hidden input will force the  PHP max upload size.
  it may work on all servers.
   --&gt;
  &lt;input name=&quot;MAX_FILE_SIZE&quot; type=&quot;hidden&quot; value=&quot;100000&quot; /&gt;
  &lt;/label&gt;
&lt;/form&gt;</pre></div></div>

<h3>Add validation script to page as shown below</h3>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">function</span> validatefile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> myform<span style="color: #339933;">=</span>document.<span style="color: #660066;">uploadform</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">//reference to form </span>
		<span style="color: #003366; font-weight: bold;">var</span> file_field_value<span style="color: #339933;">=</span>myform.<span style="color: #660066;">filefield</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//access form value with dom model</span>
		<span style="color: #003366; font-weight: bold;">var</span> error<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>file_field_value<span style="color: #339933;">==</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				error<span style="color: #339933;">+=</span><span style="color: #3366CC;">&quot;Please Select A file.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #003366; font-weight: bold;">var</span> allowed_extensions<span style="color: #339933;">=</span><span style="color: #009966; font-style: italic;">/(jpg|jpeg|bmp|giff|doc|docx|pdf)$/</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//check for allowed extension it is simple regular expression</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>file_field_value.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>allowed_extensions<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				error<span style="color: #339933;">+=</span><span style="color: #3366CC;">&quot;Please select only jpeg or jpg or bmp or giff or doc or pdf .&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">!=</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  <span style="color: #006600; font-style: italic;">//check for any errors</span>
				<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">//if error alert error</span>
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">//return to form don't submit to action page</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// every thing going fine return true allow form to submit the data.</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>Check file status and process the page on server side.</h3>

<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
</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Upload'</span><span style="color: #009900;">&#93;</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;">//check if form submitted</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;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filefield'</span><span style="color: #009900;">&#93;</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;">//check for image submitted</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filefield'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// check for error re file</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;filefield&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//every thing fine file successfully uploaded to server</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'File not uploaded.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// exit script</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>now a simple validation and uploading the file</h3>

<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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You can&quot;t access this file directly'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//avoid direct accessing to this file.</span>
&nbsp;
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Upload'</span><span style="color: #009900;">&#93;</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;">//check if form submitted</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;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filefield'</span><span style="color: #009900;">&#93;</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;">//check for image submitted</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filefield'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// check for error re file</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;filefield&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$file</span><span style="color: #339933;">=</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filefield'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//every thing fine. file successfully uploaded to server</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'File not uploaded.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// exit script</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000088;">$upload_directory</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'uploaded/'</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$ext_str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;gif,jpg,jpeg,mp3,tiff,bmp,doc,docx,ppt,pptx,txt,pdf&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$allowed_extensions</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;">$ext_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$max_file_size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10485760</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//10 mb remember 1024bytes =1kbytes</span>
 <span style="color: #000088;">$overwrite_file</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">/* 
 upload directory check 
  */</span>
 <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_directory</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;">//check if upload directory exists or not</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mkdir</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_directory</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;">//if directory doesn't exists try to create it,if fails warn user</span>
                    <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">chmod</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_directory</span><span style="color: #339933;">,</span> <span style="color: #208080;">0777</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//change file permisson to write,read,execute</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$status</span> <span style="color: #339933;">=</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$status</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">//if can't make a directory warn the user and exit</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'There is no uploade directory or i can&quot; create the upload directory'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 
check allowed extensions here
 */</span> 
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</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: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//get file extension from last sub string from last . character</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ext</span><span style="color: #339933;">,</span> <span style="color: #000088;">$allowed_extensions</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'only'</span><span style="color: #339933;">.</span><span style="color: #000088;">$ext_str</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' files allowed to upload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// exit the script by warning</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 
check file size of the file if it exceeds the specified size warn user
 */</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'size'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;=</span><span style="color: #000088;">$max_file_size</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'only the file less than '</span><span style="color: #339933;">.</span><span style="color: #000088;">$max_file_size</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'mb  allowed to upload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// exit the script by warning</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 
check if the file already exists or not in the upload directory
 */</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$overwrite_file</span> and <span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the file  '</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' already exists.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// exit the script by warning</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$upload_directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The file can&quot;t moved to target directory..'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//file can't moved with unknown reasons likr cleaning of server temperory files cleaning</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/* 
Hurrey we uploaded a file to server successfully.
 */</span></pre></td></tr></table></div>

<h3>Download Source Code</h3>
<p><a href="http://www.maheshchari.com/wp-content/uploads/2009/05/simple_php_file_uploade_by_maheshchari.rar">Download Source Code Link(3kb)</a></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=48&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/simple-php-file-upload-tutorial/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

