<?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>WLScripting.com &#187; File Manipulation</title>
	<atom:link href="http://www.wlscripting.com/tutorial/category/file-manipulation/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wlscripting.com</link>
	<description>PHP coding tutorials</description>
	<lastBuildDate>Mon, 19 Dec 2011 13:17:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-beta-1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Move file</title>
		<link>http://www.wlscripting.com/tutorial/51</link>
		<comments>http://www.wlscripting.com/tutorial/51#comments</comments>
		<pubDate>Thu, 01 May 2008 20:08:35 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[File Manipulation]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/51</guid>
		<description><![CDATA[Move a file from one location to another on your web site with php.]]></description>
			<content:encoded><![CDATA[<p>This tutorial / piece of code will show you how to move a file on your website to another location. For instance you have a web application to approve or deny file uploads. Using this function will make make it easier to process this task.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">moveFile</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-var">$newFolder</span><span class="hl-brackets">) {
        </span><span class="hl-comment">// Check if the move to directory actually exists if not return an error
	</span><span class="hl-reserved">if </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">is_dir</span><span class="hl-brackets">(</span><span class="hl-var">$newFolder</span><span class="hl-brackets">)) {
		</span><span class="hl-reserved">return </span><span class="hl-quotes">'</span><span class="hl-string">Move to folder </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$newFolder</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string"> does not exist</span><span class="hl-quotes">'</span><span class="hl-code">;
	</span><span class="hl-brackets">}
	
        </span><span class="hl-comment">// Make sure that are $newFolder variable ends with a /
	</span><span class="hl-var">$newFolder</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">in_array</span><span class="hl-brackets">(</span><span class="hl-identifier">substr</span><span class="hl-brackets">(</span><span class="hl-var">$newFolder</span><span class="hl-code">, -</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">/</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-special">\'</span><span class="hl-string">)) ? $newFolder : $newFolder.</span><span class="hl-quotes">'</span><span class="hl-code">/</span><span class="hl-quotes">'</span><span class="hl-string">);
	
        // Check to make sure the file you want to move actually exists
	if (is_file($file)) {
                // Get the filename of the file to be moved
		$filename = basename($file);
		rename($file, $newFolder.$filename);
		return </span><span class="hl-quotes">'</span><span class="hl-identifier">File </span><span class="hl-quotes">'</span><span class="hl-string">.$filename.</span><span class="hl-quotes">' </span><span class="hl-identifier">moved to </span><span class="hl-quotes">'</span><span class="hl-string">.$newFolder;
	}
	return </span><span class="hl-quotes">'</span><span class="hl-identifier">File </span><span class="hl-quotes">'</span><span class="hl-string">.$file.</span><span class="hl-quotes">' </span><span class="hl-identifier">does not exist</span><span class="hl-quotes">'</span><span class="hl-string">;
}
?&gt;</span></pre></div></div>
<p>Then to use the code you would use something like:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-identifier">moveFile</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">file1.txt</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">movehere/</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/51/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prevent direct file access</title>
		<link>http://www.wlscripting.com/tutorial/48</link>
		<comments>http://www.wlscripting.com/tutorial/48#comments</comments>
		<pubDate>Thu, 16 Aug 2007 17:52:51 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[File Manipulation]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/48</guid>
		<description><![CDATA[Protecting direct access to php files that do not need to be directly accessed.]]></description>
			<content:encoded><![CDATA[<p>Do you have PHP files that you do not want visitors to directly run but would still like to use them in your code? Here is a little trick picked up from the code in the phpBB forum code.</p>
<p>Create a file named config.php or some other file that you are going to include before any other code. In this file put the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">define</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">INMYSCRIPT</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>This code creates a defined variable INMYSCRIPT which can be named as anything that you want. Then on every other page in your script that you do not want to be ran on their own put the following: (This filename is code.php)</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">defined</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">INMYSCRIPT</span><span class="hl-quotes">'</span><span class="hl-brackets">)){
</span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">You cannot access this file directly.</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-reserved">die</span><span class="hl-code">;
</span><span class="hl-brackets">}

</span><span class="hl-comment">// Whatever other code you wish goes below here
</span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">This file is safe to be ran</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Now, instead of the file outputting any data to the browser that could be harmful, the only content output is: &#8220;You cannot access this file directly&#8221;. You could very easily create a re-direct script to transfer them to a specific page or you could modify the text to any other HTML or other content you wanted. </p>
<p>Open up your web browser and try accessing code.php. You should get that error message that we created. Now if you create one last file that we will name as test.php, place the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">require</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">config.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">require</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">code.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Now if you try accessing the test.php file from your web server you will be shown: &#8220;This file is safe to be ran&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/48/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Very basic file upload information</title>
		<link>http://www.wlscripting.com/tutorial/29</link>
		<comments>http://www.wlscripting.com/tutorial/29#comments</comments>
		<pubDate>Tue, 15 Aug 2006 02:00:29 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[File Manipulation]]></category>

		<guid isPermaLink="false">http://wlscripting.com/tutorial/29</guid>
		<description><![CDATA[Get very basic information about file uploads.]]></description>
			<content:encoded><![CDATA[<p>Here is a very basic code that will help you find all the information about a file being uploaded.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-default">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=windows-1250&quot;&gt;
  &lt;title&gt;Very Basic file upload info&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action=&quot;</span><span class="hl-inlinetags">&lt;?php </span><span class="hl-reserved">echo </span><span class="hl-var">$_SERVER</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">PHP_SELF</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">; </span><span class="hl-inlinetags">?&gt;</span><span class="hl-default">&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;input type=&quot;file&quot; name=&quot;uFile&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;upload&quot;&gt;
  &lt;/form&gt;
</span><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-var">$_FILES</span><span class="hl-brackets">) {
  </span><span class="hl-var">$ext</span><span class="hl-code"> = </span><span class="hl-identifier">explode</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">.</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">name</span><span class="hl-quotes">'</span><span class="hl-brackets">])</span><span class="hl-code">;
  </span><span class="hl-var">$extension</span><span class="hl-code"> = </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-var">$ext</span><span class="hl-brackets">)</span><span class="hl-code"> - </span><span class="hl-number">1</span><span class="hl-code">;
  </span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Type: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">type</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">&lt;br&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
  </span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Name: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">name</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">&lt;br&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
  </span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">tmp_name: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">tmp_name</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">&lt;br&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
  </span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Size: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">size</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">&lt;br&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
  </span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Extension: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$ext</span><span class="hl-brackets">[</span><span class="hl-var">$extension</span><span class="hl-brackets">]</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">&lt;br&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
  </span><span class="hl-identifier">move_uploaded_file</span><span class="hl-brackets">(</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">tmp_name</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">/path/to/uploads/</span><span class="hl-quotes">&quot;</span><span class="hl-code">.</span><span class="hl-var">$_FILES</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">uFile</span><span class="hl-quotes">'</span><span class="hl-brackets">][</span><span class="hl-quotes">'</span><span class="hl-string">name</span><span class="hl-quotes">'</span><span class="hl-brackets">])</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span><span class="hl-default">
  &lt;/body&gt;
&lt;/html&gt;</span></pre></div></div>
<p>The main key to this code is in the form. You MUST include the: </p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>enctype=&quot;multipart/form-data&quot;</pre></div></div>
<p>The next key part is the input type. You must have an input type as file instead of text.<br />
Then you can just use the $_FILES variable to get all the information you need.</p>
<p>Make sure that you change the &#8220;/path/to/uploads/&#8221; in the move_uploaded_file() line to the path of where the file will be uploaded to.</p>
<p>If you forget what information you can get from the $_FILES variable just do a print_r($_FILES) and you can see everything.</p>
<p>Hope you enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/29/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>File Include Check</title>
		<link>http://www.wlscripting.com/tutorial/26</link>
		<comments>http://www.wlscripting.com/tutorial/26#comments</comments>
		<pubDate>Tue, 01 Aug 2006 00:15:49 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[File Manipulation]]></category>

		<guid isPermaLink="false">http://wlscripting.com/tutorial/26</guid>
		<description><![CDATA[Check to see if your file is included or not.]]></description>
			<content:encoded><![CDATA[<p>Does your website have several files that are included? Do you not want to have them output anything if they are addressed directly? Here is a very simple script to do that:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">amIincluded</span><span class="hl-brackets">() {
  </span><span class="hl-var">$requestedURL</span><span class="hl-code"> = </span><span class="hl-identifier">parse_url</span><span class="hl-brackets">(</span><span class="hl-var">$_SERVER</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">REQUEST_URI</span><span class="hl-quotes">'</span><span class="hl-brackets">])</span><span class="hl-code">;
  </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">basename</span><span class="hl-brackets">(</span><span class="hl-var">$requestedURL</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">path</span><span class="hl-quotes">'</span><span class="hl-brackets">])</span><span class="hl-code"> != </span><span class="hl-identifier">basename</span><span class="hl-brackets">(</span><span class="hl-reserved">__FILE__</span><span class="hl-brackets">))
   </span><span class="hl-reserved">return true</span><span class="hl-code">;
  </span><span class="hl-reserved">return false</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>This amIincluded function gets the address of the page in your browser. Then it parses that into its parts and gets the filename. If that filename is the same as the filename of the file, it returns true otherwise it returns false.</p>
<p>A sample usage of this is:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">amIincluded</span><span class="hl-brackets">()) {
</span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Included</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-brackets">} </span><span class="hl-reserved">else </span><span class="hl-brackets">{
</span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Not Included</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/26/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Directory Size</title>
		<link>http://www.wlscripting.com/tutorial/25</link>
		<comments>http://www.wlscripting.com/tutorial/25#comments</comments>
		<pubDate>Thu, 27 Jul 2006 14:34:59 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[File Manipulation]]></category>

		<guid isPermaLink="false">http://wlscripting.com/tutorial/25</guid>
		<description><![CDATA[Get the size of a directory and all sub directories.]]></description>
			<content:encoded><![CDATA[<p>Creating a script to display the size of a directory is fairly simple. First off you need to know the path to the directory. After that is known you can use the following fuction to display the filesize:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">dirsize</span><span class="hl-brackets">(</span><span class="hl-var">$dir</span><span class="hl-brackets">) {
  </span><span class="hl-var">$dirs</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-var">$dir</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">while</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-reserved">empty</span><span class="hl-brackets">(</span><span class="hl-var">$dirs</span><span class="hl-brackets">)) { </span><span class="hl-comment">// Loop through all directories
      </span><span class="hl-var">$path</span><span class="hl-code"> = </span><span class="hl-identifier">array_shift</span><span class="hl-brackets">(</span><span class="hl-var">$dirs</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-reserved">foreach</span><span class="hl-brackets">(</span><span class="hl-identifier">glob</span><span class="hl-brackets">(</span><span class="hl-var">$path</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">/*</span><span class="hl-quotes">'</span><span class="hl-brackets">) </span><span class="hl-reserved">AS </span><span class="hl-var">$next</span><span class="hl-brackets">) {
        </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">is_dir</span><span class="hl-brackets">(</span><span class="hl-var">$next</span><span class="hl-brackets">)) {
          </span><span class="hl-var">$dirs</span><span class="hl-brackets">[]</span><span class="hl-code"> = </span><span class="hl-var">$next</span><span class="hl-code">; </span><span class="hl-comment">// Add another directory to the loop
        </span><span class="hl-brackets">} </span><span class="hl-reserved">else </span><span class="hl-brackets">{
          </span><span class="hl-var">$size</span><span class="hl-code"> += </span><span class="hl-identifier">filesize </span><span class="hl-brackets">(</span><span class="hl-var">$next</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Add filesize to count
        </span><span class="hl-brackets">}
      }
    }
  </span><span class="hl-reserved">return </span><span class="hl-var">$size</span><span class="hl-code">; </span><span class="hl-comment">// Return the total filesize of the directory
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>This function returns a string with the size of the directory in bytes. You can however use the <a href="http://wlscripting.com/tutorial/6">Convert byte count</a> function to produce a readable format.</p>
<p>An example of this code could be:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$path</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">.</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-reserved">echo </span><span class="hl-identifier">dirsize</span><span class="hl-brackets">(</span><span class="hl-var">$path</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// would produce something like 732952 or 715.79 KB using the Convert byte count function
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/25/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Convert byte count</title>
		<link>http://www.wlscripting.com/tutorial/6</link>
		<comments>http://www.wlscripting.com/tutorial/6#comments</comments>
		<pubDate>Fri, 14 Jul 2006 13:37:59 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[File Manipulation]]></category>

		<guid isPermaLink="false">http://wlscripting.com/?p=6</guid>
		<description><![CDATA[Convert a bytes into an easier to read format.]]></description>
			<content:encoded><![CDATA[<p>So  you have the byte count of a file but you don&#8217;t have the time to try converting it in your head to KB or MB. Using the following code this is easy.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">convertbytes</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">) {
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-code"> &lt; </span><span class="hl-number">1024</span><span class="hl-brackets">) {
</span><span class="hl-reserved">return </span><span class="hl-var">$data</span><span class="hl-code">.</span><span class="hl-quotes">&quot;</span><span class="hl-string"> bytes</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-brackets">} </span><span class="hl-reserved">elseif</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-code"> &lt; </span><span class="hl-number">1024000</span><span class="hl-brackets">) {
</span><span class="hl-reserved">return </span><span class="hl-identifier">round</span><span class="hl-brackets">((</span><span class="hl-var">$data</span><span class="hl-code"> / </span><span class="hl-number">1024</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">&quot;</span><span class="hl-string"> k</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-brackets">} </span><span class="hl-reserved">else </span><span class="hl-brackets">{
</span><span class="hl-reserved">return </span><span class="hl-identifier">round</span><span class="hl-brackets">((</span><span class="hl-var">$data</span><span class="hl-code"> / </span><span class="hl-number">1048576</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">&quot;</span><span class="hl-string"> MB</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-brackets">}
}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Just supply the filesize in the function and it will return the filesize in an easier to readable format.</p>
<p>An example of this could be:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">echo </span><span class="hl-identifier">convertbytes</span><span class="hl-brackets">(</span><span class="hl-identifier">filesize</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">/path/to/your/file.php</span><span class="hl-quotes">'</span><span class="hl-brackets">))</span><span class="hl-code">;
</span><span class="hl-reserved">echo </span><span class="hl-identifier">convertbytes</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">10000</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// would output 9.77 k
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/6/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

