<?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>Web Development Blog &#187; php classes</title>
	<atom:link href="http://www.web-development-blog.com/archives/tag/php-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-development-blog.com</link>
	<description>Web development tutorials, SEO articles and PHP script resources</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:38:00 +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 Mail Scripts using SMTP transport, a guide for beginners</title>
		<link>http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/</link>
		<comments>http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 09:53:26 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[PHP scripts]]></category>
		<category><![CDATA[Reviews]]></category>
		<category><![CDATA[php classes]]></category>
		<category><![CDATA[phpmailer]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[swift mailer]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/?p=1049</guid>
		<description><![CDATA[PHP has a very simple mail function which is used very often for basic text mail messages. What if you need to attach files or if you need to send your e-mail messages via SMTP? Than it&#8217;s time to use a more advanced script. This is because the standard mail function has only limited standard [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>PHP has a very <a href="http://www.finalwebsites.com/forums/topic/php-mail-script">simple mail function</a> which is used very often for basic text mail messages. What if you need to attach files or if you need to send your e-mail messages via SMTP? Than it&#8217;s time to use a more advanced script. This is because the standard mail function has only limited standard capabilities. There are many reasons to use SMTP transport for sending e-mail messages from your web application, some of them are:</p>
<ul>
<li>Many <a href="http://www.webfaction.com/?affiliate=finalwebsites" rel="nofollow">shared hosting providers</a> doesn&#8217;t allow to use the PHP mail() function for security reasons</li>
<li>Your web application is more flexible if you use the Simple Message Transfer Protocol (SMTP). This way your e-mail function is not limited to the servers port or e-mail configuration anymore. </li>
<li>SMTP is much more powerful and secure (using SSL)</li>
</ul>
<p>In this article we will compare three of the bigger PHP projects which allow to send e-mail messages via SMTP, including attachments. <span id="more-1049"></span></p>
<ol>
<li>The Mail class included in the <strong>Zend Framework</strong> (http://framework.zend.com/)</li>
<li><strong>Swift Mailer</strong> (http://swiftmailer.org/)</li>
<li><strong>PHPMailer</strong> (http://phpmailer.worxware.com)</li>
</ol>
<p>We reviewed these three PHP classes because they&#8217;re written for PHP5 and they are updated frequently. For our review we tried the provided examples and the documentation. We&#8217;re sure that all three classes are very powerful and offer many functions for almost every type of web application. Because this review should help the beginning PHP developer, this article is sho-case for a few functions only.</p>
<h3>Our test case for this review</h3>
<p>For our example we tested all three classes to send a plain text mail message with a single image attachment, using SMTP transport with authentication. Here is the good news; as a more experienced PHP user, I was able to use the provided examples within several minutes for each of the classes.</p>
<h3>Zend Mail Class</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include_path'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.:/path2directory/ZendFramework/library/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Loader.php'</span><span style="color: #339933;">;</span>
Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Mail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Mail_Transport_Smtp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'auth'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'login'</span><span style="color: #339933;">,</span> 
              <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'smtpUser'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'smtpPassword'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$transport</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail_Transport_Smtp<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'smtp.server.com'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$at</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/logo.png'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$at</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'logo.png'</span><span style="color: #339933;">;</span>                                  
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBodyText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is the text inside the mail send by Zend_Mail using SMTP transport.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'you@mail.com'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Your Name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact@mailservice.us'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Your friend'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSubject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mail Subject'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$transport</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I don&#8217;t like the documentation from the Zend framework, you need to check many pages to get all the required code for the snippet above. You need to create a second object to send your message via SMTP. I&#8217;m missing the information on their site about how-to test the &#8220;send&#8221; function to create use a success or error message. Using the class is not very difficult, but installing the Zend Framework might be a hard job for the beginner. This class is a great solution for people already using the Zend Framework or where the the library is provided by the hosting provider.</p>
<h3>Swift Mailer</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Swift/lib/swift_required.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$transport</span> <span style="color: #339933;">=</span> Swift_SmtpTransport<span style="color: #339933;">::</span><span style="color: #004000;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'smtp.server.com'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUsername</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'smtpUser'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'smtpPassword'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mailer</span> <span style="color: #339933;">=</span> Swift_Mailer<span style="color: #339933;">::</span><span style="color: #004000;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$transport</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> Swift_Message<span style="color: #339933;">::</span><span style="color: #004000;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Wonderful Subject'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'you@mail.com'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Your Name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTo</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact@mailservice.us'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Your friend'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBody</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is the text of the mail send by Swift using SMTP transport.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$attachment</span> <span style="color: #339933;">=</span> Swift_Attachment<span style="color: #339933;">::</span><span style="color: #004000;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/logo.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'logo.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</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: #000088;">$numSent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mailer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sent <span style="color: #009933; font-weight: bold;">%d</span> messages<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$numSent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The snippet looks similar to the code from the Zend mail class, but you have to create 4 different objects:</p>
<ol>
<li>An object for the SMTP transport (Swift_SmtpTransport)</li>
<li>The object that will send the message (Swift_Mailer)</li>
<li>The message object for all mail parts (Swift_Message)</li>
<li>An object for the attachment (Swift_Attachment)</li>
</ol>
<p>The configuration/installation is much easier than for the Zend Framework, just include one single file and you&#8217;re ready to use the class. If you like this OOP (Object Oriented Programming) style used in this class, this script might be for you.</p>
<h3>PHPMailer</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PHPMailer5/class.phpmailer.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPMailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">IsSMTP</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'smtp.server.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SMTPAuth</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'smtpUser'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'smtpPassword'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'you@mail.com'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Your Name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAddress</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact@mailservice.us'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Your friend'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'PHPMailer Message'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This e-mail is sent through PHPMailer.'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/logo.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'logo.png'</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;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Mailer error: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ErrorInfo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message has been sent.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The coding style from this example looks very different from the two others and some PHP developer would say this is not really OOP code. Right PHPMailer doesn&#8217;t have one file for each method and for the example above you need to upload only two files. I like the PHPMailer class because of all the information you get on the project&#8217;s website. </p>
<p>The three examples above should help you to make the decision which script will work the best for you. Check the links below for the documentation related to the snippets I have used in this artikel:</p>
<ul>
<li><a href="http://framework.zend.com/manual/en/zend.mail.html" rel="nofollow">http://framework.zend.com/manual/en/zend.mail.html</a></li>
<li><a href="http://swiftmailer.org/docs/introduction" rel="nofollow">http://swiftmailer.org/docs/introduction</a></li>
<li><a href="http://phpmailer.worxware.com/index.php?pg=examplebsmtp" rel="nofollow">http://phpmailer.worxware.com/index.php?pg=examplebsmtp</a></li>
</ul>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/" rel="bookmark" title="September 1, 2009">Sending e-mails via SMTP with PHPmailer and Gmail</a></li>
<li><a href="http://www.web-development-blog.com/archives/jquery-contact-form-for-your-website/" rel="bookmark" title="January 11, 2010">jQuery Contact form for your website</a></li>
<li><a href="http://www.web-development-blog.com/archives/integrate-your-mailchimp-newsletter-subscription/" rel="bookmark" title="October 26, 2009">Integrate your MailChimp Newsletter Subscription</a></li>
</ul>
<p><!-- Similar Posts took 3.814 ms --></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Important Update for the PHP class Access User</title>
		<link>http://www.web-development-blog.com/archives/important-update-for-the-php-class-access-user/</link>
		<comments>http://www.web-development-blog.com/archives/important-update-for-the-php-class-access-user/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 22:06:44 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[PHP scripts]]></category>
		<category><![CDATA[finalwebsites.com]]></category>
		<category><![CDATA[access_user]]></category>
		<category><![CDATA[forum]]></category>
		<category><![CDATA[php classes]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/archives/important-update-for-the-php-class-access-user/</guid>
		<description><![CDATA[Today our partner site finalwebsites.com has announced an important update for the Access_user class. The update is a security update to make the script more safe against computer hacker. While there is not really a problem for existing, working applications, it&#8217;s advised to update applications very soon. The release information: This is an important (security [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>Today our partner site <a href="http://www.finalwebsites.com/portal">finalwebsites.com</a> has announced an important update for the Access_user class. The update is a security update to make the script more safe against computer hacker. While there is not really a problem for existing, working applications, it&#8217;s advised to update applications very soon.</p>
<h3>The release information:</h3>
<blockquote><p>This is an important (security related) update, in previous versions hackers can guess common and repeating passwords from user. While the &#8220;forgotten password&#8221; function was based on the password and the user id, it could be possible to change the password for some user (if the hacker knows the users id and the right password). The risk is not very high for most installations but could be work out some trouble. The new version doesn&#8217;t use the password for validation anymore. The login name (encrypted) is used together with some &#8220;secret&#8221; secret string. AU class user can replace the class file but need to update the method calls in the activate password script. You need to add the constant variable SECRET_STRING to the db_config.php file.</p></blockquote>
<p>Download the updated version from the PHP class <a href="http://www.finalwebsites.com/snippets.php?id=10">here</a>.</p>
<p>For further questions about the class post them to the official <a href="http://www.finalwebsites.com/forums/forum/access_user-class-support-forum">support forum</a>.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/" rel="bookmark" title="September 1, 2009">Sending e-mails via SMTP with PHPmailer and Gmail</a></li>
<li><a href="http://www.web-development-blog.com/archives/search-for-photos-using-php-and-the-flickr-api/" rel="bookmark" title="December 19, 2009">Search for photos using PHP and the flickr API</a></li>
<li><a href="http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/" rel="bookmark" title="October 11, 2007">Tutorial: FTP Upload via cURL</a></li>
</ul>
<p><!-- Similar Posts took 3.326 ms --></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/important-update-for-the-php-class-access-user/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a zip file from folders on the fly</title>
		<link>http://www.web-development-blog.com/archives/tutorial-create-a-zip-file-from-folders-on-the-fly/</link>
		<comments>http://www.web-development-blog.com/archives/tutorial-create-a-zip-file-from-folders-on-the-fly/#comments</comments>
		<pubDate>Mon, 24 Sep 2007 12:22:54 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[PHP scripts]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[compress]]></category>
		<category><![CDATA[php classes]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/archives/tutorial-create-a-zip-file-from-folders-on-the-fly/</guid>
		<description><![CDATA[For a future project I needed these days some easy to use zip or gzip class to create a zip file from files / folders inside a specified directory. A short search on Google has lead me to the Create ZIP File PHP class from Rochak Chauhan. I tested two other scripts before and must [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>For a future project I needed these days some easy to use <strong>zip</strong> or <strong>gzip class</strong> to create a zip file from files / folders inside a specified directory. A short search on Google has lead me to the <a href="http://olederer.users.phpclasses.org/browse/package/2322.html" rel="nofollo">Create ZIP File</a> PHP class from <a href="http://www.rochakchauhan.com/blog/" rel="nofollow">Rochak Chauhan</a>. I tested two other scripts before and must say that this script works great for single files if you add them manually.</p>
<p>To compress a whole directory with an unknown number of files into one zip file I created some class extension to get this job done.<br />
I used some directory functions (opendir, readdir) to get all the files from the specified directory. Inside each directory there are files and &#8220;real directories&#8221; and directory locations. We need only the real directories. We test each value with &#8220;is_file&#8221;, &#8220;is_dir&#8221; and we filter off the directory directions (&#8220;.&#8221; and &#8220;..&#8221;). After the &#8220;file&#8221; is identified, the files content is added to the &#8220;addFile&#8221; object and for other directories the method &#8220;get_file_from_folder&#8221; is called again.<span id="more-114"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> createDirZip <span style="color: #000000; font-weight: bold;">extends</span> createZip <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> get_files_from_folder<span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">,</span> <span style="color: #000088;">$put_into</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$fileContents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileContents</span><span style="color: #339933;">,</span> <span style="color: #000088;">$put_into</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'.'</span> and <span style="color: #000088;">$file</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'..'</span> and <span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$put_into</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_files_from_folder</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$put_into</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</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: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To get the files and sub directories from some directory we use this code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$createZip</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> createDirZip<span style="color: #339933;">;</span>
<span style="color: #000088;">$createZip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'themes/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$createZip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_files_from_folder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog/wp-content/themes/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'themes/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We need create an object from the extension we have just created. Next we need only to define the base directory (which is used in the zip file) and the path to the directory. With this we are able to output the zip file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fileName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'tmp/archive.zip'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fd</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileName</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fwrite</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #339933;">,</span> <span style="color: #000088;">$createZip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getZippedfile</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: #990000;">fclose</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$createZip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">forceDownload</span><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: #339933;">@</span><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Via the method &#8220;getZippedfile&#8221; all content is added to the file (archive.zip) and the method &#8220;forceDownload&#8221; will send the created zip file to the browser.</p>
<p>I think this simple function is very helpful if you like to offer the public some dynamic files inside a directory or just to &#8220;zip &#8211; mail &#8211; backup&#8221; some directories from your web server using CRON.</p>
<p><a href="http://www.freelancer.com/affiliates/olederer/" rel="nofollow"><img src="http://www.freelancer.com/img/affiliates/Freelancer_b2.gif" width=468 height=60 alt="Freelance Jobs" border=0></a></p>
<p>Check also this <a href="http://www.finalwebsites.com/forums/forum/discuss-our-web-development-tutorials" target="_blank">forum thread</a> if you need some help.</p>
<p><strong>Some important update! </strong></p>
<blockquote><p>After using the same script on two of my websites I got some complains from some (mac) users  with the problem that they can&#8217;t open the downloaded zip files. While the download via Firefox on Windows and Linux (even most of the time with IE) works fine is this not a the best solution. I checked some other classes but didn&#8217;t find a similar solution. My advice is to use the the <a href="http://www.info-zip.org/" target="_blank" rel="nofollow">zip application</a> via the command line:</p>
<p><strong> exec(&#8216;zip -r test.zip your_folder/&#8217;);</strong></p>
<p>This is not the same as in this tutorial but will work for most of the machines. Actually is this solution much better even for bigger files&#8230;</p></blockquote>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/create-custom-backups-from-your-website-using-curl/" rel="bookmark" title="July 30, 2008">Create custom backups from your website using cURL</a></li>
<li><a href="http://www.web-development-blog.com/archives/upload-in-modal-window-and-pass-values-with-jquery/" rel="bookmark" title="September 5, 2009">Upload in modal window and pass values with jQuery</a></li>
<li><a href="http://www.web-development-blog.com/archives/limit-the-number-of-downloads-per-client/" rel="bookmark" title="May 5, 2007">Limit the number of downloads per client</a></li>
</ul>
<p><!-- Similar Posts took 3.727 ms --></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/tutorial-create-a-zip-file-from-folders-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->