<?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>Blue Ember Design &#187; CodeIgniter</title>
	<atom:link href="http://www.blueemberdesign.com/blog/category/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blueemberdesign.com</link>
	<description>Web Design, Development, and Online Marketing for the San Francisco &#38; East Bay Area</description>
	<lastBuildDate>Mon, 24 May 2010 23:44:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>FTP Download with the CodeIgniter FTP Library</title>
		<link>http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/</link>
		<comments>http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 16:00:21 +0000</pubDate>
		<dc:creator>Garrett St. John</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[codeigniter ftp]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[ftp library]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.blueemberdesign.com/?p=999</guid>
		<description><![CDATA[I was recently working on a project that involved FTP and I thought it would be a great chance to get away from the built-in PHP functionality and use the CodeIgniter FTP library instead. I was pretty surprised to find that there was no download functionality in the base FTP library as of CI 1.7.2.  [...]


Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: Announcing the CodeIgniter Dreamhost API Library'>Announcing the CodeIgniter Dreamhost API Library</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/07/07/formigniter-easy-form-generation-codeigniter/' rel='bookmark' title='Permanent Link: FormIgniter: Easy Form Generation for CodeIgniter'>FormIgniter: Easy Form Generation for CodeIgniter</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I was recently working on a project that involved FTP and I thought it would be a great chance to get away from the <a href="http://php.net/manual/en/book.ftp.php">built-in PHP functionality</a> and use the <a href="http://codeigniter.com/user_guide/libraries/ftp.html">CodeIgniter FTP library</a> instead.</p>
<p>I was pretty surprised to find that there was <strong>no download functionality in the base FTP library </strong> as of CI 1.7.2.  I&#8217;ve extended the library to incorporate file download and will share that here.</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: #666666; font-style: italic;">// This codes goes in the 'application/libraries' folder with a</span>
<span style="color: #666666; font-style: italic;">// file name of MY_Ftp.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MY_Ftp <span style="color: #000000; font-weight: bold;">extends</span> CI_Ftp <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Constructor
	 **/</span>
	<span style="color: #000000; font-weight: bold;">function</span> MY_Ftp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span><span style="color: #004000;">CI_Ftp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Download a file from a FTP server
	 * @param	&lt;string&gt;	$rem_path	Remote path of the file to download
	 * @param	&lt;string&gt;	$loc_path	Local path destination for download
	 * @param	&lt;string&gt;	$mode		Transfer mode, defaults to auto
	 **/</span>
	<span style="color: #000000; font-weight: bold;">function</span> download<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rem_path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$loc_path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mode</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'auto'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// check for an active connection</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_is_conn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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>
&nbsp;
		<span style="color: #666666; font-style: italic;">// get remote folder/filename</span>
		<span style="color: #000088;">$rem_folder</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rem_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$rem_filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rem_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// if a local directory was passed handle differently.</span>
		<span style="color: #666666; font-style: italic;">// Otherwise, treat same as remote.</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;">$loc_path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$loc_folder</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_path</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;">$loc_filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rem_filename</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;">$loc_folder</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$loc_filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_path</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 that the loc path exists</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$loc_folder</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'.'</span>  <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">@</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_folder</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ftp_bad_loc_path'</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// check that loc path and file are writable</span>
		<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">@</span><span style="color: #990000;">is_writable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_folder</span><span style="color: #009900;">&#41;</span> OR <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_folder</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$loc_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">@</span><span style="color: #990000;">is_writable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_folder</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$loc_filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ftp_loc_not_write'</span><span style="color: #009900;">&#41;</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>
&nbsp;
		<span style="color: #666666; font-style: italic;">// store old paths</span>
		<span style="color: #000088;">$old_rem_path</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ftp_pwd</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$old_loc_path</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getcwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// move to the local path</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">@</span><span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$loc_folder</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ftp_bad_loc_path'</span><span style="color: #009900;">&#41;</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>
&nbsp;
		<span style="color: #666666; font-style: italic;">// move to the remote path</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$rem_folder</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">changedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rem_folder</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ftp_bad_rem_path'</span><span style="color: #009900;">&#41;</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>
&nbsp;
		<span style="color: #666666; font-style: italic;">// check that the file exists remotely</span>
		<span style="color: #000088;">$found_file</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">list_files</span><span style="color: #009900;">&#40;</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: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</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: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$f</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: #000088;">$f</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$rem_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$found_file</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</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: #000088;">$found_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ftp_bad_rem_file'</span><span style="color: #009900;">&#41;</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>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Set the mode if not specified</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'auto'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Get the file extension so we can set the upload type</span>
			<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getext<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rem_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_settype<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'ascii'</span><span style="color: #009900;">&#41;</span> ? FTP_ASCII <span style="color: #339933;">:</span> FTP_BINARY<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// download the file</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ftp_get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$loc_folder</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$loc_filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rem_filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">debug</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ftp_unable_to_download'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<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>
&nbsp;
		<span style="color: #666666; font-style: italic;">// move back to the root path</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">changedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_rem_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #339933;">@</span><span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_loc_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As a side note, you will need to tweak your language file to include all of the message codes (e.g., ftp_bad_loc_path) passed to _error() for proper error display in your app.</p>
<img src="http://www.blueemberdesign.com/?ak_action=api_record_view&id=999&type=feed" alt="" />

<p>Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: Announcing the CodeIgniter Dreamhost API Library'>Announcing the CodeIgniter Dreamhost API Library</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/07/07/formigniter-easy-form-generation-codeigniter/' rel='bookmark' title='Permanent Link: FormIgniter: Easy Form Generation for CodeIgniter'>FormIgniter: Easy Form Generation for CodeIgniter</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CodeIgniter File Upload: Changing the File Name</title>
		<link>http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/</link>
		<comments>http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 08:00:56 +0000</pubDate>
		<dc:creator>Garrett St. John</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[extending]]></category>
		<category><![CDATA[file name]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[files codeigniter]]></category>
		<category><![CDATA[upload library]]></category>

		<guid isPermaLink="false">http://www.blueemberdesign.com/?p=767</guid>
		<description><![CDATA[There has been some interest lately on our blog about how to change the file name of uploaded files with CodeIgniter.  We will review how to do this with the base File Upload library as well as extending the library to allow for a defined file name


Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Setting Disallowed File Types'>CodeIgniter File Upload: Setting Disallowed File Types</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/' rel='bookmark' title='Permanent Link: FTP Download with the CodeIgniter FTP Library'>FTP Download with the CodeIgniter FTP Library</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong>: As of version 1.7.2, CodeIgniter now has a built-in option to specify the uploaded file name.  Placed in the $config array, &#8216;file_name&#8217; sets the desired end name and should be provided without an extension.  The extension will be carried across from the original file.</p>
<p>&#8212;&#8212;</p>
<p>There has been some interest lately on our blog about how to change the file name of uploaded files with CodeIgniter.  We will review how to do this with the <strong>base File Upload library</strong> as well as <strong>extending the library</strong> to allow for a defined file name.</p>
<h2>Using the Base File Upload Library</h2>
<p>The base file upload library has a built-in option to &#8220;encrypt&#8221; the file name for a freshly uploaded file.  According to the <a href="http://codeigniter.com/user_guide/libraries/file_uploading.html">documentation</a>, that option is &#8216;encrypt_name&#8217; and takes a Boolean value.  If set to TRUE, the file name is renamed by running it through this set of functions:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_ext</span><span style="color: #339933;">;</span></pre></div></div>

<p>This essentially generates a nice random MD5 hash that will likely not collide with other files in your directory.  Most of the time this method for renaming will work just fine, but what if we want to specify the name rather than have it random?</p>
<h2>Extending the File Upload Library</h2>
<p>This method involves extending the base library which we have done before for <a href="http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/">other file upload customizations</a>.  In this method, the do_upload() function accepts a file name and applies it to the file.  Here is the code used to accomplish that outcome:</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> do_upload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'userfile'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <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;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_no_file_selected'</span><span style="color: #009900;">&#41;</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>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate_upload_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$error</span> <span style="color: #339933;">=</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;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">4</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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;">;</span>
&nbsp;
      <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_INI_SIZE</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_file_exceeds_limit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_FORM_SIZE</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_file_exceeds_form_limit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_PARTIAL</span>
           <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_file_partial'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_NO_FILE</span>
           <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_no_file_selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_NO_TMP_DIR</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_no_temp_directory'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_CANT_WRITE</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_unable_to_write_file'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// UPLOAD_ERR_EXTENSION</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_stopped_by_extension'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">default</span> <span style="color: #339933;">:</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_no_file_selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <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>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_size</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'size'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(.+?);.*$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_ext</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_extension</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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>
&nbsp;
    <span style="color: #666666; font-style: italic;">// check if a name has been specified, if so set it</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_name</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_prep_filename<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_name</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_ext</span><span style="color: #009900;">&#41;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_prep_filename<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</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: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_size</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_size</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_allowed_filetype</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_invalid_filetype'</span><span style="color: #009900;">&#41;</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>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_allowed_filesize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_invalid_filesize'</span><span style="color: #009900;">&#41;</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>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_allowed_dimensions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_invalid_dimensions'</span><span style="color: #009900;">&#41;</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>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clean_file_name</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">max_filename</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit_filename_length</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">max_filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove_spaces</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/\s+/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">orig_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">overwrite</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_filename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload_path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</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>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">@</span><span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_temp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload_path</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #009900;">&#41;</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: #339933;">@</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_temp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload_path</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_destination_error'</span><span style="color: #009900;">&#41;</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>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xss_clean</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">do_xss_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_image_properties</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload_path</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Much of this code is taken from the existing File Upload library, but is provided above for the sake of completeness.  Comments have been removed to shorten it up a bit, but can be found in the original Upload.php for further understanding.</p>
<p>The file name can be set in the controller by calling the do_upload() function as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">do_upload</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form_field_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'new_file_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With this extended File Upload library, the following circumstances exist:</p>
<ol>
<li>The file name provided in do_upload() <strong>should not</strong> have an extension. The extension from the original file will be applied.</li>
<li>If &#8216;encrypt_name&#8217; is set in $config, it will take precedence over the specified file name.</li>
</ol>
<p>Have any other tips for changing the file name for uploaded files in CodeIgniter?  Share it in the comments!</p>
<img src="http://www.blueemberdesign.com/?ak_action=api_record_view&id=767&type=feed" alt="" />

<p>Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Setting Disallowed File Types'>CodeIgniter File Upload: Setting Disallowed File Types</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/' rel='bookmark' title='Permanent Link: FTP Download with the CodeIgniter FTP Library'>FTP Download with the CodeIgniter FTP Library</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing the CodeIgniter Dreamhost API Library</title>
		<link>http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/</link>
		<comments>http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 21:00:51 +0000</pubDate>
		<dc:creator>Garrett St. John</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[api library]]></category>
		<category><![CDATA[codeigniter dreamhost]]></category>
		<category><![CDATA[codeigniter library]]></category>
		<category><![CDATA[dreamhost]]></category>
		<category><![CDATA[dreamhost api]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://www.blueemberdesign.com/?p=747</guid>
		<description><![CDATA[Today we are announcing the release of a Dreamhost API library for CodeIgniter.  The API lets you integrate common Dreamhost Panel activities into your web applications.  Enjoy!


Related Posts:<ol><li><a href='http://www.blueemberdesign.com/codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: CodeIgniter Dreamhost API Library'>CodeIgniter Dreamhost API Library</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Setting Disallowed File Types'>CodeIgniter File Upload: Setting Disallowed File Types</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>As we&#8217;ve said many times before, we are avid <a href="http://www.blueemberdesign.com/codeigniter-development/">CodeIgniter developers</a> at Blue Ember Design.  The framework is fluid and light-weight and the developer community is great.</p>
<p>We&#8217;ve been inspired many times by the extensive list of <a href="http://www.haughin.com/code/">CodeIgniter libraries provided by Elliot Haughin</a> and decided we ought to follow in his footsteps with a great new API library.</p>
<p>Today we are announcing the release of a <a href="http://www.blueemberdesign.com/codeigniter-dreamhost-api-library/">Dreamhost API library for CodeIgniter</a>.  The API lets you integrate common Dreamhost Panel activities into your web applications.  With this library, you can integrate tasks like announcement list management and Dreamhost PS resizing right into your CodeIgniter applications.  Pretty great!</p>
<p>We&#8217;d love to hear feedback about the library, how you are using it, and if you have any suggestions.  Enjoy!</p>
<p><a href="http://www.blueemberdesign.com/codeigniter-dreamhost-api-library/">Get the CodeIgniter Dreamhost API Library Here</a></p>
<img src="http://www.blueemberdesign.com/?ak_action=api_record_view&id=747&type=feed" alt="" />

<p>Related Posts:<ol><li><a href='http://www.blueemberdesign.com/codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: CodeIgniter Dreamhost API Library'>CodeIgniter Dreamhost API Library</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Setting Disallowed File Types'>CodeIgniter File Upload: Setting Disallowed File Types</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Issues with CodeIgniter File Upload</title>
		<link>http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/</link>
		<comments>http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 16:00:20 +0000</pubDate>
		<dc:creator>Garrett St. John</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[file path]]></category>
		<category><![CDATA[file type]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[mime type]]></category>

		<guid isPermaLink="false">http://www.blueemberdesign.com/?p=619</guid>
		<description><![CDATA[Last post we wrote about how to extend the File Upload Library to set disallowed file types rather than only the allowed file types. We saw a fair amount of interest in file upload for CodeIgniter and have decided to post some tips for troubleshooting the most common issues we've come across.


Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Setting Disallowed File Types'>CodeIgniter File Upload: Setting Disallowed File Types</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/' rel='bookmark' title='Permanent Link: FTP Download with the CodeIgniter FTP Library'>FTP Download with the CodeIgniter FTP Library</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Last post we wrote about how to extend the File Upload Library to <a href="http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/">set disallowed file types</a> rather than only the <strong>allowed file types</strong>.  We saw a fair amount of interest in file upload for CodeIgniter and have decided to post some tips for troubleshooting the most common issues we&#8217;ve come across.</p>
<h2>&#8220;The filetype you are attempting to upload is not allowed&#8221;</h2>
<p>Many times this is the appropriate error you would expect to see after a disallowed file type has been uploaded, however, we have noticed that there are a couple cases where files inaccurate fail.  Here are some potential issues and their solutions.</p>
<ul>
<li><strong>Option &#8216;allowed_types&#8217; Is Required</strong><br />
If no list of file types is provided in $config['allowed_types'], you will always get this error.  The documentation shows the options with a default value of &#8220;none&#8221;, but it fails to make it obvious that it is a required input.</li>
<li><strong>MIME Type Is Not Listed</strong><br />
Another potential solution to this problem could be that the extension is not listed in config/mime.php.  Any file type that should be allowed must exist in the $mime array or it will fail.</li>
<li><strong>MIME Type Variations</strong><br />
Different browsers often return different MIME types.  For this reason, config/mimes.php accepts multiple MIME types for a given file type.  Many times a variant of the main MIME type will need to be added to accommodate all browsers.</li>
</ul>
<h2>$data['file_path'] is an Absolute Path</h2>
<p>The $config array accepts &#8216;file_path&#8217; as either a relative or absolute path. It is definitely convenient to allow either as an input, however, the $data array returned after processing the file returns an <strong>absolute path</strong>.  This can certainly cause some issues if the expectation is that the returned &#8216;file_path&#8217; is in the same format as the &#8216;file_path&#8217; provided in the $config array.</p>
<h2>Accepting Multiple Files in a Form</h2>
<p>There is an <a href="http://codeigniter.com/forums/viewthread/110130/">ongoing discussion</a> with code segments in the CodeIgniter Forums on how to accept multi-file uploads.  There are a handful of great solutions available and lots of people supporting the topic.</p>
<p>What other issues have you come across with the File Upload Class in CodeIgniter?  How do you see it being able to be improved or extended to be the most useful?</p>
<img src="http://www.blueemberdesign.com/?ak_action=api_record_view&id=619&type=feed" alt="" />

<p>Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Setting Disallowed File Types'>CodeIgniter File Upload: Setting Disallowed File Types</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2010/02/23/ftp-download-codeigniter-library/' rel='bookmark' title='Permanent Link: FTP Download with the CodeIgniter FTP Library'>FTP Download with the CodeIgniter FTP Library</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>CodeIgniter File Upload: Setting Disallowed File Types</title>
		<link>http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/</link>
		<comments>http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 16:00:56 +0000</pubDate>
		<dc:creator>Garrett St. John</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[extend library]]></category>
		<category><![CDATA[file type]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[mime type]]></category>
		<category><![CDATA[upload class]]></category>
		<category><![CDATA[upload library]]></category>
		<category><![CDATA[uploading codeigniter]]></category>

		<guid isPermaLink="false">http://www.blueemberdesign.com/?p=589</guid>
		<description><![CDATA[Recently we were working with a client on a file upload page in CodeIgniter.  The client wanted to accept any file type except for those that could be considered malicious.  Seemed simply enough, but took some modification of of the default library.  Check out how we did it


Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: Announcing the CodeIgniter Dreamhost API Library'>Announcing the CodeIgniter Dreamhost API Library</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Recently we were working with a client on a file upload page in CodeIgniter.  The client wanted to accept any file type except for those that could be considered malicious (For example &#8211; Windows EXEs, VB scripts, etc.).  Seemed easy enough as we&#8217;ve done file uploading in CodeIgniter many times.</p>
<p>Initially we jumped into the <a href="http://codeigniter.com/user_guide/libraries/file_uploading.html">File Upload Class</a> and started to implement the page, but quickly discovered that the &#8216;allowed_types&#8217; preference is a required field by default.  That&#8217;s not exactly ideal when the list of allowed files is considerably larger than those that are disallowed.  The solution we chose was to extend the existing File Upload Class as follows:</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MY_Upload <span style="color: #000000; font-weight: bold;">extends</span> CI_Upload <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// declare disallowed types variable</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$disallowed_types</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// add in the 'disallowed_types' default during initialization</span>
  <span style="color: #000000; font-weight: bold;">function</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$defaults</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'max_size'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'max_width'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'max_height'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'max_filename'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'allowed_types'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'disallowed_types'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'file_temp'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'file_name'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'orig_name'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'file_type'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'file_size'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'file_ext'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'upload_path'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'overwrite'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'encrypt_name'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'is_image'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'image_width'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'image_height'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'image_type'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'image_size_str'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'error_msg'</span>        <span style="color: #339933;">=&gt;</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: #0000ff;">'mimes'</span>            <span style="color: #339933;">=&gt;</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: #0000ff;">'remove_spaces'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'xss_clean'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'temp_prefix'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;temp_file_&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</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;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'set_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$key</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// set disallowed filetypes</span>
  <span style="color: #000000; font-weight: bold;">function</span> set_disallowed_types<span style="color: #009900;">&#40;</span><span style="color: #000088;">$types</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disallowed_types</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;">$types</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;">// adapted to not require allowed_types and to check for disallowed types if it exists</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_allowed_filetype<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// if allowed file type list is not defined</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allowed_types</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> OR <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allowed_types</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 disallowed file type list is not defined</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disallowed_types</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> OR <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disallowed_types</span><span style="color: #009900;">&#41;</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>
&nbsp;
      <span style="color: #666666; font-style: italic;">// check for disallowed file types and return</span>
      <span style="color: #666666; font-style: italic;">// negated because is_disallowed_filetype returns opposite result as this function</span>
      <span style="color: #b1b100;">return</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_disallowed_filetype</span><span style="color: #009900;">&#40;</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;">// proceed as usual with allowed file type list check</span>
    <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">is_allowed_filetype</span><span style="color: #009900;">&#40;</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 for disallowed file types</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_disallowed_filetype<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// no file types provided</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disallowed_types</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> OR <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disallowed_types</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// search through disallowed for this file type</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disallowed_types</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$mime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mimes_types</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime</span><span style="color: #009900;">&#41;</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: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mime</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;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <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>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now, rather than using $config['allowed_types'], you will want to use $config['disallowed_types'] while initializing the File Upload Library.  Extensions should be listed in the same manner.</p>
<p><strong>NOTE: As is with the default File Upload library, disallowed MIME types must be defined in config/mimes.php.</strong></p>
<p>Feel free to use this revised library in your project freely.  We&#8217;d love to hear any feedback you might have to offer.</p>
<img src="http://www.blueemberdesign.com/?ak_action=api_record_view&id=589&type=feed" alt="" />

<p>Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: Announcing the CodeIgniter Dreamhost API Library'>Announcing the CodeIgniter Dreamhost API Library</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.blueemberdesign.com/blog/2009/07/29/codeigniter-file-upload-setting-disallowed-file-types/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>FormIgniter: Easy Form Generation for CodeIgniter</title>
		<link>http://www.blueemberdesign.com/blog/2009/07/07/formigniter-easy-form-generation-codeigniter/</link>
		<comments>http://www.blueemberdesign.com/blog/2009/07/07/formigniter-easy-form-generation-codeigniter/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 17:00:46 +0000</pubDate>
		<dc:creator>Garrett St. John</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[form validation]]></category>
		<category><![CDATA[formigniter]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[validation code]]></category>

		<guid isPermaLink="false">http://www.blueemberdesign.com/?p=437</guid>
		<description><![CDATA[We recently caught wind of a great web-based form generator for CodeIgniter, called FormIgniter. With the form validation built into CodeIgniter life is a whole lot easier than coding in straight PHP, but it can still be pretty tedious.  As FormIgniter puts it: To build a form with the form validator and form libraries you [...]


Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: Announcing the CodeIgniter Dreamhost API Library'>Announcing the CodeIgniter Dreamhost API Library</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>We recently caught wind of a great <strong>web-based form generator for CodeIgniter</strong>, called <a href="http://formigniter.org/formigniter">FormIgniter</a>.</p>
<p>With the form validation built into CodeIgniter life is a whole lot easier than coding in straight PHP, but it can still be pretty tedious.  As FormIgniter puts it:</p>
<blockquote><p>To build a form with the form validator and form libraries you need to enter the same value in multiple places; 3 times in the view, the label, the fields error message, and the field itself, again in the controller, once more in the model, and finally you need to setup the database schema to accept these values. This is repetitive and dull.</p></blockquote>
<p>We can definitely agree it is a bit tiresome.  Enter FormIgniter.  A simple way to automatically generate the MVC code and the SQL for a given form.  All validation code comes built into the resulting controller and forms are set up to re-populate on error.  A major time saver!</p>
<p>Often times there is a need for the use of sessions, AJAX, or custom callbacks when working with forms.  This functionality isn&#8217;t built into FormIgniter, but with some tweaks these features can be up and running in a fraction of the time.</p>
<p>Thanks to <a href="http://toomanytabs.com/">Ollie Rattue</a> for this great app!</p>
<img src="http://www.blueemberdesign.com/?ak_action=api_record_view&id=437&type=feed" alt="" />

<p>Related Posts:<ol><li><a href='http://www.blueemberdesign.com/blog/2009/08/03/common-issues-codeigniter-file-upload/' rel='bookmark' title='Permanent Link: Common Issues with CodeIgniter File Upload'>Common Issues with CodeIgniter File Upload</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/17/announcing-the-codeigniter-dreamhost-api-library/' rel='bookmark' title='Permanent Link: Announcing the CodeIgniter Dreamhost API Library'>Announcing the CodeIgniter Dreamhost API Library</a></li>
<li><a href='http://www.blueemberdesign.com/blog/2009/08/25/codeigniter-file-upload-changing-the-file-name/' rel='bookmark' title='Permanent Link: CodeIgniter File Upload: Changing the File Name'>CodeIgniter File Upload: Changing the File Name</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.blueemberdesign.com/blog/2009/07/07/formigniter-easy-form-generation-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
