<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Magic Book - Free PHP Scripts, Tutorials and Downloads &#187; File Upload</title>
	<atom:link href="http://www.phpmagicbook.com/category/file-upload/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmagicbook.com</link>
	<description>PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips</description>
	<lastBuildDate>Tue, 15 Jun 2010 13:22:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP File Upload Script &#8211; Globals Off</title>
		<link>http://www.phpmagicbook.com/php-file-upload-script-globals-off/</link>
		<comments>http://www.phpmagicbook.com/php-file-upload-script-globals-off/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 12:52:15 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Checks]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[filesize]]></category>
		<category><![CDATA[most wanted]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=308</guid>
		<description><![CDATA[It is easy to upload file when the Register_Globals are set to ON in the php.ini file (php configuration file) which is not recommended in any case for security reasons. When Globals are OFF then in this case I have tried this script and it works fine. Following is the script. Create two files. one [...]<p><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/">PHP File Upload Script &#8211; Globals Off</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>It is easy to upload file when the Register_Globals are set to ON in the php.ini file (php configuration file) which is not recommended in any case for security reasons. When Globals are OFF then in this case I have tried this script and it works fine. Following is the script. Create two files. one html containing form and the other php file containing php script. This script will need a directory named as &#8216;img&#8217; in root where this php script file is located according to the requirement of code. You can customize this code.</p>
<p><span id="more-308"></span></p>
<h4>HTML Form Code</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;upload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;
&lt;tr&gt;
&lt;td&gt;File Name &lt;/td&gt;
&lt;td&gt;
&lt;input name=&quot;uploadedfile&quot; type=&quot;file&quot; id=&quot;uploadedfile&quot; /&gt;    &lt;/td&gt;
&lt;td&gt;&lt;label&gt;
&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot; /&gt;
    &lt;/label&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;</pre></div></div>

<h4>PHP Upload Code</h4>

<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: #666666; font-style: italic;">// Check that file is transferring to PHP code or not</span>
<span style="color: #666666; font-style: italic;">// print_r($_FILES);</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// upload</span>
<span style="color: #000088;">$image1</span><span style="color: #339933;">=</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// img is our upload directory</span>
	<span style="color: #000088;">$sUploadDir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'img/'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sUploadedFile</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sUploadDir</span> <span style="color: #339933;">.</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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;">$sUploadedFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$sThumbnail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  File Uploaded Successfully&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/11/file-upload-php.rar' class="download">Download PHP File Upload Easy Way Script</a></p>
<p><strong>*** Important ***</strong></p>
<p>Never give a simple page to upload to your users if it is a website based on users. Always check that whether user is uploading image file, audio, video or script file. Check extension of file to be uploaded. Suppose you need jpg files to be uploaded. Set a check for script to upload just files with jpg extension and none other. If there is a bad user. He can upload a script file (php file) and damage your server. So security first. Checks are essential.</p>
<h4>Security Checks For File To Be Uploaded</h4>
<p><strong>Check Extension</strong></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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userfile_type</span> <span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;image/pjpeg&quot;</span> OR <span style="color: #000088;">$userfile_type</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;image/gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$msg</span><span style="color: #339933;">=</span><span style="color: #000088;">$msg</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;Your uploaded file must be of JPG or GIF. Other file types are unsupported&lt;BR&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$file_upload</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// at file_upload false you can exit from script</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Extract Base Name and Extension</strong></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: #000088;">$file_basename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// strip extention</span>
      <span style="color: #000088;">$file_ext</span>          <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #990000;">strripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// this will give us file extension to compare with our supported formats</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Check Extension</strong></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: #000088;">$extension</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'.jpg'</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><span style="color: #000088;">$extension</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is not a JPEG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>More Checks</strong></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: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'somefile.jpg'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// assuming you've already taken some other</span>
<span style="color: #666666; font-style: italic;">// preventive measures such as checking file</span>
<span style="color: #666666; font-style: italic;">// extensions...</span>
&nbsp;
<span style="color: #000088;">$result_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</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_array</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;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result_array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mime'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;image/jpeg&quot;</span><span style="color: #339933;">:</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;file is jpeg type&quot;</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: #0000ff;">&quot;image/gif&quot;</span><span style="color: #339933;">:</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;file is gif type&quot;</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;file is an image, but not of gif or jpeg type&quot;</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;">echo</span> <span style="color: #0000ff;">&quot;file is not a valid image file&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/">PHP File Upload Script &#8211; Globals Off</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/get-file-size-and-file-type-in-php/" title="Get File Size and File Type In PHP">Get File Size and File Type In PHP</a></li><li><a href="http://www.phpmagicbook.com/function-existence-check/" title="Function Existence Check Using PHP">Function Existence Check Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/htaccess-essential-tricks-for-blog-website-performance-a-guide/" title=".htaccess Essential Tricks For Blog/Website Performance &#8211; Guide">.htaccess Essential Tricks For Blog/Website Performance &#8211; Guide</a></li><li><a href="http://www.phpmagicbook.com/upload-max-filesize-large-file-uploads/" title="Upload Max Filesize &#8211; Large File Uploads in PHP">Upload Max Filesize &#8211; Large File Uploads in PHP</a></li><li><a href="http://www.phpmagicbook.com/find-screen-resolution-with-php/" title="Find Screen Resolution With PHP">Find Screen Resolution With PHP</a></li><li><a href="http://www.phpmagicbook.com/browser-check-with-php/" title="Browser Check With PHP">Browser Check With PHP</a></li><li><a href="http://www.phpmagicbook.com/php-file-append/" title="PHP &#8211; File Append">PHP &#8211; File Append</a></li><li><a href="http://www.phpmagicbook.com/php-file-delete-unlink-function/" title="PHP &#8211; File Delete &#8211; Unlink Function">PHP &#8211; File Delete &#8211; Unlink Function</a></li><li><a href="http://www.phpmagicbook.com/php-file-read/" title="PHP &#8211; File Read">PHP &#8211; File Read</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/php-file-upload-script-globals-off/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Upload Max Filesize &#8211; Large File Uploads in PHP</title>
		<link>http://www.phpmagicbook.com/upload-max-filesize-large-file-uploads/</link>
		<comments>http://www.phpmagicbook.com/upload-max-filesize-large-file-uploads/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 14:17:27 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[filesize]]></category>
		<category><![CDATA[ini]]></category>
		<category><![CDATA[settings]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=275</guid>
		<description><![CDATA[By default there is a restriction of 2 Megabytes file upload at a time for PHP. If we have to upload media files or other large files then we will have to change some settings in php.ini. Locate the php.ini file in the folder your upload script resides in. If none exists, install a new [...]<p><a href="http://www.phpmagicbook.com/upload-max-filesize-large-file-uploads/">Upload Max Filesize &#8211; Large File Uploads in PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>By default there is a restriction of 2 Megabytes file upload at a time for PHP. If we have to upload media files or other large files then we will have to change some settings in php.ini.</p>
<ul>
<li>Locate the php.ini file in the folder your upload script resides in. If none exists, install a new php.ini file from the PHP config icon in cPanel. Then, use the File Manager to rename php.ini.default to php.ini</li>
<li>In the php.ini file, locate the line called &#8216;upload_max_filesize = 2M&#8217;</li>
<li>Modify that entry to read &#8216;upload_max_filesize = 10M&#8217;, or whatever your script requires. (M = megabytes)</li>
</ul>
<p><span id="more-275"></span></p>
<p>PHP presents a very versatile and user friendly interface for handling file uploads. The default installation is not capable of working with files greater than 2 Mega Bytes size. Here are some more options for configuring PHP to handle large file transfers.</p>
<p><strong>php.ini settings</strong></p>
<p>The php.ini file contains all the configuration settings for PHP. These can be overridden by directives in apache .htaccess files or even with in the scripts themselves but for the moment let&#8217;s just concentrate on the ini file.</p>
<p>This file contains the following settings that we need to modify according to our needs.</p>
<ul>
<li>file_uploads</li>
<li>upload_max_filesize</li>
<li>max_input_time</li>
<li>memory_limit</li>
<li>max_execution_time</li>
<li>post_max_size</li>
</ul>
<p><strong>file_uploads</strong></p>
<p>If you set this off, uploading is disabled.</p>
<p><strong>upload_max_filesize and post_max_size</strong></p>
<p>Files are POSTed to the webserver in a format known as &#8216;multipart/form-data&#8217;. The post_max_size sets the upper limit on the amount of data that a script can accept in this manner. Ideally this value should be larger than the value that you set for upload_max_filesize.</p>
<p>It&#8217;s important to realize that <strong>upload_max_filesize</strong> is the sum of the sizes of all the files that you are uploading. <strong>post_max_size</strong> is the upload_max_filesize plus the sum of the lengths of all the other fields in the form plus any mime headers that the encoder might include.</p>
<p>According to the PHP documentation you can set a <strong>MAX_UPLOAD_LIMIT</strong> in your HTML form in a hidden field to suggest a limit to the browser but this is not 100% reliable way.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form&gt;
&lt;input name=&quot;MAX_FILE_SIZE&quot; type=&quot;hidden&quot; value=&quot;10737418240&quot; /&gt;
&lt;/form&gt;</pre></div></div>

<p><strong>memory_limit</strong></p>
<p>When the PHP engine is handling an incoming POST it needs to keep some of the incoming data in memory. Setting too high a value can be very dangerous because if several uploads are being handled concurrently all available memory will be used up and other unrelated scripts that consume a lot of memory might effect the whole server as well.</p>
<p><strong>max_execution_time and max_input_time</strong></p>
<p>These settings define the maximum life time of the script and the time that the script should spend in accepting input. If several mega bytes of data are being transfered max_input_time should be reasonably high. You can override the setting in the ini file for max_input_time by calling the <strong>set_time_limit()</strong> function in your scripts.</p>
<p>While working with uploading in PHP, consider above settings and options in mind and use these very carefully.</p>
<p><a href="http://www.phpmagicbook.com/upload-max-filesize-large-file-uploads/">Upload Max Filesize &#8211; Large File Uploads in PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/how-to-setup-vhosts-on-xamp/" title="How to Setup VHOSTS on Xamp">How to Setup VHOSTS on Xamp</a></li><li><a href="http://www.phpmagicbook.com/get-file-size-and-file-type-in-php/" title="Get File Size and File Type In PHP">Get File Size and File Type In PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/upload-max-filesize-large-file-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upload File &#8211; Send HTML Format Email with Attachment Using PHP</title>
		<link>http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/</link>
		<comments>http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 23:25:25 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[Email Systems]]></category>
		<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Email Attachment]]></category>
		<category><![CDATA[html email]]></category>
		<category><![CDATA[mime types]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=86</guid>
		<description><![CDATA[What this file does is uploads file in &#8216;temp&#8217; folder then emails that attachment with message to a predefined email address and finally removes that file from temp folder. The idea is to send email in rich format but with file attachment. Functionality - Determine File Type to be attached - Uploads file in temp [...]<p><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>What this file does is uploads file in &#8216;temp&#8217; folder then emails that attachment with message to a predefined email address and finally removes that file from temp folder. The idea is to send email in rich format but with file attachment.</p>
<h4>Functionality</h4>
<p>- Determine File Type to be attached<br />
- Uploads file in temp directory and deletes file after email with attachment has been sent<br />
- Emails uploaded file as an attachment</p>
<p><span id="more-185"></span></p>
<h4>Form Code</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    &lt;form name=&quot;filepost&quot; method=&quot;post&quot; action=&quot;file.php&quot; enctype=&quot;multipart/form-data&quot; id=&quot;file&quot;&gt;
    &lt;table width=&quot;300&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;
      &lt;tr valign=&quot;bottom&quot;&gt;
        &lt;td height=&quot;20&quot;&gt;Your Name:&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;input name=&quot;from&quot; type=&quot;text&quot; id=&quot;from&quot; size=&quot;30&quot;&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr valign=&quot;bottom&quot;&gt;
        &lt;td height=&quot;20&quot;&gt;Your Email Address:&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;frmtxt2&quot;&gt;&lt;input name=&quot;emaila&quot; type=&quot;text&quot; id=&quot;emaila&quot; size=&quot;30&quot;&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td height=&quot;20&quot; valign=&quot;bottom&quot;&gt;RFQ File:&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr valign=&quot;bottom&quot;&gt;
        &lt;td valign=&quot;bottom&quot;&gt;&lt;input name=&quot;filea&quot; type=&quot;file&quot; id=&quot;filea&quot; size=&quot;16&quot;&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td height=&quot;40&quot; valign=&quot;middle&quot;&gt;&lt;input name=&quot;Reset2&quot; type=&quot;reset&quot; id=&quot;Reset2&quot; value=&quot;Reset&quot;&gt;
            &lt;input name=&quot;Submit2&quot; type=&quot;submit&quot; value=&quot;Submit&quot; onClick=&quot;return CheckData45()&quot;&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/form&gt;</pre></div></div>

<h4>PHP Code</h4>

<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: #666666; font-style: italic;">// request variables</span>
<span style="color: #000088;">$from</span><span style="color: #339933;">=</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;from&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$emaila</span><span style="color: #339933;">=</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;emaila&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filea</span><span style="color: #339933;">=</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;filea&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filea</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">function</span> mail_attachment <span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attachment</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$fileatt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$attachment</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Path to the file</span>
	<span style="color: #000088;">$fileatt_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;application/octet-stream&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// File Type</span>
    <span style="color: #000088;">$start</span><span style="color: #339933;">=</span>	<span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachment</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: #339933;">-</span><span style="color: #cc66cc;">1</span> ? <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachment</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: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachment</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fileatt_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachment</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachment</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Filename that will be used for the file as the attachment</span>
&nbsp;
	<span style="color: #000088;">$email_from</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$from</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Who the email is from</span>
    <span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;New Attachment Message&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email_subject</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$subject</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// The Subject of the email</span>
	<span style="color: #000088;">$email_txt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Message that the email has in it</span>
&nbsp;
	<span style="color: #000088;">$email_to</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$to</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Who the email is to</span>
&nbsp;
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$email_from</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileatt</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileatt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$msg_txt</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> You have recieved a new attachment message from <span style="color: #006699; font-weight: bold;">$from</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$semi_rand</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mime_boundary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;==Multipart_Boundary_x<span style="color: #006699; font-weight: bold;">{$semi_rand}</span>x&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>MIME-Version: 1.0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;Content-Type: multipart/mixed;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot; boundary=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">{$mime_boundary}</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email_txt</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$msg_txt</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email_message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;This is a multi-part message in MIME format.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                <span style="color: #0000ff;">&quot;--<span style="color: #006699; font-weight: bold;">{$mime_boundary}</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                <span style="color: #0000ff;">&quot;Content-Type:text/html; charset=<span style="color: #000099; font-weight: bold;">\&quot;</span>iso-8859-1<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
               <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: 7bit<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
	<span style="color: #000088;">$email_txt</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">chunk_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email_message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;--<span style="color: #006699; font-weight: bold;">{$mime_boundary}</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                  <span style="color: #0000ff;">&quot;Content-Type: <span style="color: #006699; font-weight: bold;">{$fileatt_type}</span>;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                  <span style="color: #0000ff;">&quot; name=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">{$fileatt_name}</span><span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                  <span style="color: #666666; font-style: italic;">//&quot;Content-Disposition: attachment;\n&quot; .</span>
                  <span style="color: #666666; font-style: italic;">//&quot; filename=\&quot;{$fileatt_name}\&quot;\n&quot; .</span>
                  <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: base64<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                 <span style="color: #000088;">$data</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
                  <span style="color: #0000ff;">&quot;--<span style="color: #006699; font-weight: bold;">{$mime_boundary}</span>--<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email_to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email_subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</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;">$ok</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;File Sent Successfully.&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachment</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// delete a file after attachment sent.</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sorry but the email could not be sent. Please go back and try again!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;filea&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'temp/'</span><span style="color: #339933;">.</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filea'</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
mail_attachment<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$from</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;youremailaddress@gmail.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;subject&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;message&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;temp/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;filea&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;temp&#8217; folder to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upload-sendfile.rar' class="download">Upload And Send Email With Attachment Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/attach-send-multiple-files/" title="Upload Attach And Send Email Using PHP &#8211; Multiple File Attachments">Upload Attach And Send Email Using PHP &#8211; Multiple File Attachments</a></li><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/" title="Upload Image And Create Thumbnail Using PHP">Upload Image And Create Thumbnail Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/" title="Image Upload, Rename and Apply Restrictions Using PHP">Image Upload, Rename and Apply Restrictions Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/" title="Upload and eMail File Using PHP and Apply Restrictions">Upload and eMail File Using PHP and Apply Restrictions</a></li><li><a href="http://www.phpmagicbook.com/file-upload-with-renaming/" title="File Upload With Renaming Using PHP">File Upload With Renaming Using PHP</a></li><li><a href="http://www.phpmagicbook.com/php-email-headers-and-multiple-recipients-html-php-email/" title="PHP eMail &#8211; Headers And Multiple Recipients &#8211; HTML PHP Email">PHP eMail &#8211; Headers And Multiple Recipients &#8211; HTML PHP Email</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Upload Image And Create Thumbnail Using PHP</title>
		<link>http://www.phpmagicbook.com/upload-image-and-create-thumbnail/</link>
		<comments>http://www.phpmagicbook.com/upload-image-and-create-thumbnail/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 23:01:10 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Image Handling]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Thumbnail]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=84</guid>
		<description><![CDATA[This php script takes image file creates a thumbhnail and upload it into &#8216;thumb&#8217; directory and also upload origional image with rename in &#8216;images&#8217; directory. Functionality - Uploads image in the &#8216;images; directory and renames it - Uploads thumbnail in &#8216;thumbs&#8217; directory and renames it with thumb_filename - Determine thumbnail max width and height - [...]<p><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/">Upload Image And Create Thumbnail Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This php script takes image file creates a thumbhnail and upload it into &#8216;thumb&#8217; directory and also upload origional image with rename in &#8216;images&#8217; directory.</p>
<h4>Functionality</h4>
<p>- Uploads image in the &#8216;images; directory and renames it<br />
- Uploads thumbnail in &#8216;thumbs&#8217; directory and renames it with thumb_filename<br />
- Determine thumbnail max width and height<br />
- Keeps aspect ratio of thumbnail size<br />
- Allows jpg, jpeg and png image files<br />
- Determines Max File Size allowed<br />
- Displays thumbnail after uploading</p>
<p><span id="more-184"></span></p>
<h4>Form Code</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;newad&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;&quot;&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type=&quot;file&quot; name=&quot;image&quot; &gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;Upload image&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;</pre></div></div>

<h4>PHP Code</h4>

<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;">//define a maxim size for the uploaded images</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MAX_SIZE&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// define the width and height for the thumbnail</span>
<span style="color: #666666; font-style: italic;">// note that theese dimmensions are considered the maximum dimmension and are not fixed,</span>
<span style="color: #666666; font-style: italic;">// because we have to keep the image ratio intact or it will be deformed</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;WIDTH&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;150&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HEIGHT&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is the function that will create the thumbnail image from the uploaded image</span>
<span style="color: #666666; font-style: italic;">// the resize will be done considering the width and height defined, but without deforming the image</span>
<span style="color: #000000; font-weight: bold;">function</span> make_thumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_h</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//get image extension.</span>
<span style="color: #000088;">$ext</span><span style="color: #339933;">=</span>getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//creates the new image using the appropriate function from gd library</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jpeg&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$src_img</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_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: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$src_img</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//gets the dimmensions of the image</span>
<span style="color: #000088;">$old_x</span><span style="color: #339933;">=</span><span style="color: #990000;">imageSX</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$old_y</span><span style="color: #339933;">=</span><span style="color: #990000;">imageSY</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// next we will calculate the new dimmensions for the thumbnail image</span>
<span style="color: #666666; font-style: italic;">// the next steps will be taken:</span>
<span style="color: #666666; font-style: italic;">// 1. calculate the ratio by dividing the old dimmensions with the new ones</span>
<span style="color: #666666; font-style: italic;">// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable</span>
<span style="color: #666666; font-style: italic;">// and the height will be calculated so the image ratio will not change</span>
<span style="color: #666666; font-style: italic;">// 3. otherwise we will use the height ratio for the image</span>
<span style="color: #666666; font-style: italic;">// as a result, only one of the dimmensions will be from the fixed ones</span>
<span style="color: #000088;">$ratio1</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">/</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ratio2</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_y</span><span style="color: #339933;">/</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ratio1</span><span style="color: #339933;">&gt;</span><span style="color: #000088;">$ratio2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_y</span><span style="color: #339933;">/</span><span style="color: #000088;">$ratio1</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;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">/</span><span style="color: #000088;">$ratio2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// we create a new image with the new dimmensions</span>
<span style="color: #000088;">$dst_img</span><span style="color: #339933;">=</span><span style="color: #990000;">ImageCreateTrueColor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thumb_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// resize the big image to the new created one</span>
<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$src_img</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_h</span><span style="color: #339933;">,</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">,</span><span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output the created image to the file. Now we will have the thumbnail into the file named by $filename</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//destroys source and destination images.</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</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;">// This function reads the extension of the file.</span>
<span style="color: #666666; font-style: italic;">// It is used to determine if the file is an image by checking the extension.</span>
<span style="color: #000000; font-weight: bold;">function</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</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: #339933;">!</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ext</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This variable is used as a flag. The value is initialized with 0 (meaning no error found)</span>
<span style="color: #666666; font-style: italic;">//and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// checks if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//reads the name of the file the user submitted for uploading</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">=</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// if it is not empty</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// get the original name of the file from the clients machine</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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;">// get the extension of the file in a lower case format</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// if it is not a known extension, we will suppose it is an error, print an error message</span>
<span style="color: #666666; font-style: italic;">//and will not upload the file, otherwise we continue</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;jpg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;jpeg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Unknown extension!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</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: #666666; font-style: italic;">// get the size of the image in bytes</span>
<span style="color: #666666; font-style: italic;">// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server</span>
<span style="color: #000088;">$size</span><span style="color: #339933;">=</span><span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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: #339933;">;</span>
<span style="color: #000088;">$sizekb</span><span style="color: #339933;">=</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//compare the size with the maxim size we defined and print error if bigger</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sizekb</span> <span style="color: #339933;">&gt;</span> MAX_SIZE<span style="color: #339933;">*</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;You have exceeded the size limit!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//we will give an unique name, for example the time in unix time format</span>
<span style="color: #000088;">$image_name</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;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$extension</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//the new name will be containing the full path where will be stored (images folder)</span>
<span style="color: #000088;">$newname</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;images/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$image_name</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$copied</span> <span style="color: #339933;">=</span> <span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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;">$newname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//we verify if the image has been uploaded, and print error instead</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$copied</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Copy unsuccessfull!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</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: #666666; font-style: italic;">// the new thumbnail image will be placed in images/thumbs/ folder</span>
<span style="color: #000088;">$thumb_name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'images/thumbs/thumb_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$image_name</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// call the function that will create the thumbnail. The function will get as parameters</span>
<span style="color: #666666; font-style: italic;">//the image name, the thumbnail name and the width and height desired for the thumbnail</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">=</span>make_thumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newname</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_name</span><span style="color: #339933;">,</span>WIDTH<span style="color: #339933;">,</span>HEIGHT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//If no errors registred, print the success message and show the thumbnail image created</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;Thumbnail created Successfully!&lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;img src=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$thumb_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;images&#8217; and &#8216;thumbs&#8217; folders to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upload-img-create-and-show-thumb.rar' class="download">Upload Image Create and Show Thumbnail Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/">Upload Image And Create Thumbnail Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/" title="Upload File &#8211; Send HTML Format Email with Attachment Using PHP">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/" title="Image Upload, Rename and Apply Restrictions Using PHP">Image Upload, Rename and Apply Restrictions Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/" title="Upload and eMail File Using PHP and Apply Restrictions">Upload and eMail File Using PHP and Apply Restrictions</a></li><li><a href="http://www.phpmagicbook.com/file-upload-with-renaming/" title="File Upload With Renaming Using PHP">File Upload With Renaming Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/upload-image-and-create-thumbnail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Upload Using PHP and Get File Info</title>
		<link>http://www.phpmagicbook.com/file-upload-get-info/</link>
		<comments>http://www.phpmagicbook.com/file-upload-get-info/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 22:24:13 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=82</guid>
		<description><![CDATA[This script uploads the file in the same directory and give you some information about the file. Functionality - Uploads file in the same directory - Gives information like file name, size in bytes and file type - Determines Max File Size allowed PHP Code &#60;?PHP /** * Giombetti.com Snippets() * * @author Marc Giombetti [...]<p><a href="http://www.phpmagicbook.com/file-upload-get-info/">File Upload Using PHP and Get File Info</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This script uploads the file in the same directory and give you some information about the file.</p>
<h4>Functionality</h4>
<p>- Uploads file in the same directory<br />
- Gives information like file name, size in bytes and file type<br />
- Determines Max File Size allowed</p>
<p><span id="more-183"></span></p>
<h4>PHP Code</h4>

<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: #009933; font-style: italic;">/**
  * Giombetti.com Snippets()
  *
  * @author Marc Giombetti
  * @description: This Snipplets shows how to upload a File to the Server! Please dont forget to CHMOD the directory to 777
  *
  */</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$File</span><span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$File</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Your File:&lt;/b&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;File onto the server: <span style="color: #006699; font-weight: bold;">$Fichier</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Sended File: <span style="color: #006699; font-weight: bold;">$File_name</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Size : <span style="color: #006699; font-weight: bold;">$File_size</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Tipe : <span style="color: #006699; font-weight: bold;">$File_type</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$File</span><span style="color: #339933;">,</span> <span style="color: #000088;">$File_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;&lt;form ENCTYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>multipart/form-data<span style="color: #000099; font-weight: bold;">\&quot;</span> ACTION=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$HTTP_SERVER_VARS[PHPSELF]</span><span style="color: #000099; font-weight: bold;">\&quot;</span> METHOD=<span style="color: #000099; font-weight: bold;">\&quot;</span>POST<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;&lt;input TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>MAX_FILE_SIZE<span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>100000<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;Send this file: &lt;input NAME=<span style="color: #000099; font-weight: bold;">\&quot;</span>File<span style="color: #000099; font-weight: bold;">\&quot;</span> TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>file<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;&lt;input TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span> VALUE=<span style="color: #000099; font-weight: bold;">\&quot;</span>Send<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span><span style="color: #0000ff;">&quot;&lt;/form&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;upload&#8217; folder to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upload-info1.rar' class="download">Upload File and Get Info Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/file-upload-get-info/">File Upload Using PHP and Get File Info</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/get-file-size-and-file-type-in-php/" title="Get File Size and File Type In PHP">Get File Size and File Type In PHP</a></li><li><a href="http://www.phpmagicbook.com/php-file-append/" title="PHP &#8211; File Append">PHP &#8211; File Append</a></li><li><a href="http://www.phpmagicbook.com/php-file-delete-unlink-function/" title="PHP &#8211; File Delete &#8211; Unlink Function">PHP &#8211; File Delete &#8211; Unlink Function</a></li><li><a href="http://www.phpmagicbook.com/php-file-read/" title="PHP &#8211; File Read">PHP &#8211; File Read</a></li><li><a href="http://www.phpmagicbook.com/php-file-write/" title="PHP &#8211; File Write">PHP &#8211; File Write</a></li><li><a href="http://www.phpmagicbook.com/php-file-close/" title="PHP &#8211; File Close">PHP &#8211; File Close</a></li><li><a href="http://www.phpmagicbook.com/php-file-open/" title="PHP &#8211; File Open">PHP &#8211; File Open</a></li><li><a href="http://www.phpmagicbook.com/php-creating-a-file/" title="Creating A File Using PHP">Creating A File Using PHP</a></li><li><a href="http://www.phpmagicbook.com/quick-edit-little-file-based-php-cms/" title="Quick Edit &#8211; Little File Based PHP CMS">Quick Edit &#8211; Little File Based PHP CMS</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/file-upload-get-info/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Upload, Rename and Apply Restrictions Using PHP</title>
		<link>http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/</link>
		<comments>http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 22:15:11 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=80</guid>
		<description><![CDATA[This script accepts just images and uploads these into &#8216;images&#8217; directory. Functionality - Determines Max File Size (Size Restriction) - Accepts just jpg, jpeg, png or gif image files - Changes extension of image into lowercase - Stores the file in predefined directory at your server - Replaces names of files with random digits (Rename) [...]<p><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/">Image Upload, Rename and Apply Restrictions Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This script accepts just images and uploads these into &#8216;images&#8217; directory.</p>
<h4>Functionality</h4>
<p>- Determines Max File Size (Size Restriction)<br />
- Accepts just jpg, jpeg, png or gif image files<br />
- Changes extension of image into lowercase<br />
- Stores the file in predefined directory at your server<br />
- Replaces names of files with random digits (Rename)</p>
<p><span id="more-182"></span></p>
<h4>Form Code</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;newad&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;upload.php&quot;&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type=&quot;file&quot; name=&quot;image&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;Upload image&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;</pre></div></div>

<h4>PHP Code</h4>

<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;">//define a maxim size for the uploaded images in Kb</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MAX_SIZE&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.</span>
<span style="color: #000000; font-weight: bold;">function</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</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: #339933;">!</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ext</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//checks if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//reads the name of the file the user submitted for uploading</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">=</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//if it is not empty</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//get the original name of the file from the clients machine</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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: #666666; font-style: italic;">//get the extension of the file in a lower case format</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;jpg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;jpeg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;gif&quot;</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;">//print error message</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Unknown extension!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</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: #666666; font-style: italic;">//get the size of the image in bytes</span>
<span style="color: #666666; font-style: italic;">//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server</span>
<span style="color: #000088;">$size</span><span style="color: #339933;">=</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//compare the size with the maxim size we defined and print error if bigger</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span> <span style="color: #339933;">&gt;</span> MAX_SIZE<span style="color: #339933;">*</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;You have exceeded the size limit!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//we will give an unique name, for example the time in unix time format</span>
<span style="color: #000088;">$image_name</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;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$extension</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//the new name will be containing the full path where will be stored (images folder)</span>
<span style="color: #000088;">$newname</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;images/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$image_name</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//we verify if the image has been uploaded, and print error instead</span>
<span style="color: #000088;">$copied</span> <span style="color: #339933;">=</span> <span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</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;">$newname</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: #339933;">!</span><span style="color: #000088;">$copied</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Copy unsuccessfull!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//If no errors registred, print the success message</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;File Uploaded Successfully! Try again!&lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;images&#8217; folder to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upload-with-restrictions-rename.rar' class="download">Upload Image &#038; Apply Restrictions &#038; Rename Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/">Image Upload, Rename and Apply Restrictions Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/" title="Upload File &#8211; Send HTML Format Email with Attachment Using PHP">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/" title="Upload Image And Create Thumbnail Using PHP">Upload Image And Create Thumbnail Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/" title="Upload and eMail File Using PHP and Apply Restrictions">Upload and eMail File Using PHP and Apply Restrictions</a></li><li><a href="http://www.phpmagicbook.com/file-upload-with-renaming/" title="File Upload With Renaming Using PHP">File Upload With Renaming Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Upload and eMail File Using PHP and Apply Restrictions</title>
		<link>http://www.phpmagicbook.com/upload-file-email-file-restrict/</link>
		<comments>http://www.phpmagicbook.com/upload-file-email-file-restrict/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 21:54:47 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[Email Systems]]></category>
		<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=78</guid>
		<description><![CDATA[This is complete request form and it can be used to &#8216;Request A Quote&#8217; and have some material from user uploaded at your server. This is what script does. Functionality - Determines Max File Size (Size Restriction) - Determines Max Available Space for upload (Space Restriction) - Stores the file in predefined directory at your [...]<p><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/">Upload and eMail File Using PHP and Apply Restrictions</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This is complete request form and it can be used to &#8216;Request A Quote&#8217; and have some material from user uploaded at your server. This is what script does.</p>
<h4>Functionality</h4>
<p>- Determines Max File Size (Size Restriction)<br />
- Determines Max Available Space for upload (Space Restriction)<br />
- Stores the file in predefined directory at your server<br />
- Emails you a path to the link of recent uploaded file with other details</p>
<p><span id="more-181"></span></p>
<h4>Variables Used In Form</h4>
<p>$Name<br />
$Speciality<br />
$Email<br />
$Phone<br />
$Fax<br />
$Clinic<br />
$uploadedfile<br />
$info</p>
<h4>PHP Code For File Upload</h4>

<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: #000088;">$MaxFileSize</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2048000&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// max file size in bytes</span>
<span style="color: #000088;">$HDDSpace</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;1048576&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// max total size of all files in directory</span>
<span style="color: #000088;">$HDDTotal</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;445&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$OffExt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// add characters to strip out of filenames</span>
<span style="color: #000088;">$ThisFileName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the file name</span>
<span style="color: #000088;">$abspath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ThisFileName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// get the directory path</span>
<span style="color: #666666; font-style: italic;">// full path</span>
<span style="color: #000088;">$path</span><span style="color: #339933;">=</span><span style="color: #000088;">$abspath</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pathext</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;upload/&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</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>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;..&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'?'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'*'</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;">$temp</span><span style="color: #339933;">=</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #009900;">&#123;</span>
 <span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$snr</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #666666; font-style: italic;">// remove any % signs from the file name</span>
 <span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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;">/* if the file size is within allowed limits */</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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;">&lt;</span> <span style="color: #000088;">$MaxFileSize</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/* if adding the file will not exceed the maximum allowed total */</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HDDTotal</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$HDDSpace</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* put the file in the directory */</span>
<span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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;">$path</span><span style="color: #339933;">.</span><span style="color: #000088;">$pathext</span><span style="color: #339933;">.</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">.</span><span style="color: #000088;">$OffExt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The file &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">.</span><span style="color: #0000ff;">&quot; is Successfully uploaded &quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$myfrom</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Email</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$myemail</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;youremail@hotmail.com&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$todayis</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;l, F j, Y, g:i a&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$filename</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.sitename.com/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$pathext</span><span style="color: #339933;">.</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">.</span><span style="color: #000088;">$OffExt</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$filename</span><span style="color: #339933;">=</span><span style="color: #990000;">str_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;%20&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$subject</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Request From Website&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
		uploadedfile  	        : <span style="color: #006699; font-weight: bold;">$filename</span>
		Name          	        : <span style="color: #006699; font-weight: bold;">$Name</span>
		Email         	          : <span style="color: #006699; font-weight: bold;">$Email</span>
		Phone         	         : <span style="color: #006699; font-weight: bold;">$Phone</span>
		Fax           	          : <span style="color: #006699; font-weight: bold;">$Fax</span>
		Speciality    	         : <span style="color: #006699; font-weight: bold;">$Speciality</span>
		Clinic/Hospital          :<span style="color: #006699; font-weight: bold;">$Clinic</span>
		Comments/Request   :<span style="color: #006699; font-weight: bold;">$info</span>
		&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: <span style="color: #006699; font-weight: bold;">$myfrom</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myemail</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from</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;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The Filename: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$HTTP_POST_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploadedfile'</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: #339933;">.</span><span style="color: #0000ff;">&quot; is BLOCKED from being uploaded here.&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$MaxKB</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$MaxFileSize</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// show the max file size in Kb</span>
        <span style="color: #000088;">$msg</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">&quot;The file was greater than the maximum allowed, file size of <span style="color: #006699; font-weight: bold;">$MaxKB</span> and could not be uploaded.&quot;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$msg</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;upload&#8217; folder to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upload-mail-restrict.rar' class="download">Upload File With Restrictions And Email Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/">Upload and eMail File Using PHP and Apply Restrictions</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/" title="Upload File &#8211; Send HTML Format Email with Attachment Using PHP">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/" title="Upload Image And Create Thumbnail Using PHP">Upload Image And Create Thumbnail Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/" title="Image Upload, Rename and Apply Restrictions Using PHP">Image Upload, Rename and Apply Restrictions Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-with-renaming/" title="File Upload With Renaming Using PHP">File Upload With Renaming Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/upload-file-email-file-restrict/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>File Upload With Renaming Using PHP</title>
		<link>http://www.phpmagicbook.com/file-upload-with-renaming/</link>
		<comments>http://www.phpmagicbook.com/file-upload-with-renaming/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 21:30:17 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=76</guid>
		<description><![CDATA[This example takes a name and file. Uploads it to upload directory and rename it name_filename if file name is &#8216;name&#8217;. If the file name is cat and you have typed animal then script will rename the file as animal_cat. Form Code &#60;form method=&#34;post&#34; enctype=&#34;multipart/form-data&#34; action=&#34;uploadit.php&#34;&#62; &#60;input type=&#34;text&#34; name=&#34;name1&#34; id=&#34;name1&#34;&#62; &#60;input type=&#34;hidden&#34; name=&#34;action&#34; value=&#34;upload&#34;&#62; Upload [...]<p><a href="http://www.phpmagicbook.com/file-upload-with-renaming/">File Upload With Renaming Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This example takes a <strong>name</strong> and <strong>file</strong>. Uploads it to <strong>upload</strong> directory and rename it <strong>name_filename</strong> if file name is &#8216;name&#8217;. If the file name is cat and you have typed animal then script will rename the file as animal_cat.</p>
<p><span id="more-180"></span></p>
<h4>Form Code</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;uploadit.php&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;name1&quot; id=&quot;name1&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;upload&quot;&gt;
Upload image:
&lt;input type=&quot;file&quot; name=&quot;picThumbnail&quot;&gt;&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;upload&quot;&gt;
&lt;/form&gt;</pre></div></div>

<h4>PHP Code</h4>

<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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'picThumbnail'</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: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$sUploadDir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'upload/'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$sUploadedFile</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sUploadDir</span> <span style="color: #339933;">.</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'picThumbnail'</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'picThumbnail'</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;">$sUploadedFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$sThumbnail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'picThumbnail'</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: #339933;">;</span>
					<span style="color: #000088;">$sThumbnail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$sThumbnail</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$sFileName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sThumbnail</span><span style="color: #009900;">&#41;</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;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sUploadDir</span><span style="color: #339933;">.</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'picThumbnail'</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: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
						<span style="color: #990000;">rename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sUploadDir</span><span style="color: #339933;">.</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'picThumbnail'</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;">&#40;</span><span style="color: #000088;">$sUploadDir</span><span style="color: #339933;">.</span><span style="color: #000088;">$sFileName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;file uploaded successfully&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;upload&#8217; folder to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upoad-it.rar' class="download">Upoad File With Rename Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/file-upload-with-renaming/">File Upload With Renaming Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/" title="Upload File &#8211; Send HTML Format Email with Attachment Using PHP">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/" title="Upload Image And Create Thumbnail Using PHP">Upload Image And Create Thumbnail Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/" title="Image Upload, Rename and Apply Restrictions Using PHP">Image Upload, Rename and Apply Restrictions Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/" title="Upload and eMail File Using PHP and Apply Restrictions">Upload and eMail File Using PHP and Apply Restrictions</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/file-upload-with-renaming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 3.175 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-30 16:31:39 -->
