<?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>TangoRangers.com's Blog &#187; apache</title>
	<atom:link href="http://blog.tangorangers.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tangorangers.com</link>
	<description>Misc crap and such</description>
	<lastBuildDate>Fri, 30 Jul 2010 01:13:05 +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>Using PHP to upload an image and rename it.</title>
		<link>http://blog.tangorangers.com/2009/01/using-php-to-upload-an-image-and-rename-it/</link>
		<comments>http://blog.tangorangers.com/2009/01/using-php-to-upload-an-image-and-rename-it/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 05:37:53 +0000</pubDate>
		<dc:creator>DaijoubuKun</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.tangorangers.com/?p=40</guid>
		<description><![CDATA[If your like me you have scoured the internet trying to find a simple php script that will let a user upload an image and then rename the image to something unique. Well I have just the solution. First, the only real requirement is PHP and a web server (I use Apache). Create a new [...]]]></description>
			<content:encoded><![CDATA[<p>If your like me you have scoured the internet trying to find a simple php script that will let a user upload an image and then rename the image to something unique.  Well I have just the solution.</p>
<p>First, the only real requirement is PHP and a web server (I use Apache).</p>
<p>Create a new file, lets call it &#8216;image_upload.php&#8217;<br />
At the bottom of the page we are going to put in a basic form to give a browse and upload button.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;./image_upload.php&quot;</span> enctype<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;multipart/form-data&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
Select Image to upload
<span style="color: #339933;">&lt;</span>input name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thefile&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;upload&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The form name isn&#8217;t important. The action is.  That is the name of the file that needs to be accessed when the upload button is clicked.<br />
Input type is very important, and so is the name.</p>
<p>Now that we have a starting point go to the top of the file.  Just to make it easy I&#8217;m going to paste the entire code here then explain it part by part.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$tmp_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'thefile'</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;">$newarray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'thefile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$thecount</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$newarray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fileprefix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$newarray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$thecount</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;../img/<span style="color: #006699; font-weight: bold;">$fileprefix</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To start the if statement at the top is to ensure that the &#8216;submit&#8217; button was hit before running the script.<br />
The &#8216;<em>$tmp_name</em>&#8216; variable is used to get rid of that damn array.<br />
<em>$newarray</em> takes the file&#8217;s real name (see note below) and explodes the array into several smaller arrays by the &#8216;.&#8217;. For example, if the file uploaded it called &#8216;best.friends.pic.jpg&#8217; it breaks that name up to &#8216;best&#8217;, &#8216;friends&#8217;, &#8216;pic&#8217;, and &#8216;jpg&#8217;.  Trust me, this is important (If you don&#8217;t understand how arrays work I recommend doing some research and learning).<br />
The next line I use the <em>count</em> function. This will help in the event there is more than one &#8216;.&#8217; in the name of the file being uploaded.<br />
Now to create the new name of the file.  <em>$fileprefix</em> equals the time of the upload.  This works well, it will grab the time from the server and use it for the file name.  Then it adds a &#8216;.&#8217;, then adds the suffix of the file (example: jpg)<br />
Time to move the uploaded file.  In my example I added the directory before the newly created file name. This will go back 1 directory and then to &#8216;<em>img/</em>&#8216;.<br />
Lastly, we use the &#8216;<em>exit();</em>&#8216; function.  This is so that after the file has been uploaded the script stops.  If you remove the function it will display the upload form again.</p>
<p>NOTE:  There is a reason (altho I don&#8217;t know it) on why there are 2 arrays.  It seems to me that the first (<em>$_FILES['thefile']['tmp_name']</em>) holds 2 parts, a pointer for &#8216;<em>thefile</em>&#8216; and the temp name php gives files before we do something with it, and the second (<em>$_FILES['thefile']['name']</em>) contains the original name of the file.</p>
<p>btw, make sure apache has full access to the folder you are uploading images to.<br />
For example, if this is on your own server check /etc/apache.conf (or httpd.conf) for the user and group apache runs under.  This is usually either &#8216;<em>nobody</em>&#8216; or &#8216;<em>apache</em>&#8216;.<br />
If you have SSH into your server (or are sitting infront of it) goto the directory your files are being sent to and type</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chown</span> apache.apache img<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-R</span></pre></div></div>

<p>This will give both user and group &#8216;<em>apache</em>&#8216; access to write files.</p>
<p>p.s.  I will be creating another script here that deals with multiple picture uploads.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tangorangers.com/2009/01/using-php-to-upload-an-image-and-rename-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
