My Blog

contains PHP and other web related content. (Sometimes there are some off topic things - don't freak out!)

Posts Tagged ‘web tools’

Moving files from one webserver to another using PHP script

Tuesday, October 13th, 2009

A while back, a colleague mentioned to me that he was moving his site from one webserver to another. He hated having to FTP everything down, then reupload it. Invariably, we’ve all done this – and forgot to apply proper file permissions, etc.

Enter The Single PHP Script

As a proof of concept, I developed the following PHP script. Currently, the ftp credentials and path are hardset. It simply grabs all the files in the specified directory including child directories, opens an FTP connection and puts them up there. It finishes by applying the proper file permissions. Without further rambling, here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * PHP Migrate Site
 * Proof of Concept
 * 
 * This class is used to migrate files including their permissions from one server 
 * to another via FTP
 * 
 * @author Aaron Saray (http://aaronsaray.com)
 */
class php_migrate_site
{
    /**
     * @var string the full filename of this file
     */
    protected $_self;
 
    /**
     * @var string The base directory of our files to move
     */
    protected $_basedir;
 
    /**
     * @var array the array of SPLFileInfo objects from self::$_basedir
     */
    protected $_files = array();
 
    /**
     * @var resource The connection to the ftp server for the new files
     */
    protected $_ftpConnection;
 
    /**
     * Constructor sets time limit, gets self, sets credentials
     */
    public function __construct()
    {
        set_time_limit(0);
 
        $this->_self = __FILE__;
 
        $this->_basedir = 'C:/DEVELOPMENT/local';
        $this->_ftp = array('username'=>'test', 'password'=>'', 'hostname'=>'localhost');
    }
 
    /**
     * main public method to launch process
     */
    public function go()
    {
          $this->_loadFiles();
          $this->_makeFTPConnection();
          $this->_copyFiles();
    }
 
    /**
     * Loads all of the files from basedir using a recursive directory iterator from SPL
     */
    protected function _loadFiles()
    {
        $this->_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->_basedir),RecursiveIteratorIterator::SELF_FIRST);
    }
 
    /**
     * Connects to ftp server and stores connection to self::$_ftpConnection
     */
    protected function _makeFTPConnection()
    {
        $this->_ftpConnection = ftp_connect($this->_ftp['hostname']) or die('could not connect');
        ftp_login($this->_ftpConnection, $this->_ftp['username'], $this->_ftp['password']);
    }
 
    /**
     * Copies files from local to ftp - applies permissions to them.
     */
    protected function _copyFiles()
    {
        foreach ($this->_files as $file) {
            $localFileName = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename();
            $remoteFileName = str_ireplace($this->_basedir, '', $localFileName);
 
            /** don't move self **/
            if ($localFileName == $this->_self) continue;
 
            if ($file->isDir()) {
                ftp_mkdir($this->_ftpConnection, $remoteFileName);
            }
            else {
                ftp_put($this->_ftpConnection, $remoteFileName, $localFileName, FTP_BINARY);
            }
 
            $chmod = substr(sprintf('%o', $file->getPerms()), -4);
            ftp_chmod($this->_ftpConnection, $chmod, $remoteFileName);
        }
    }
}
 
/** create new instance and run php_migrate_site::go() method **/
$migrate = new php_migrate_site();
$migrate->go();

How it can be better

There are a number of ways to make this better – in case anyone has the time to do it!

User Friendly

The first obvious thing is user friendly interface options. The script should present a form and ask for the credentials. It should submit to itself to start the verification process.

Error Checking

It should check to make sure PHP has the FTP module enabled. It should also connect to the ftp server first to verify credentials.

More FTP Options

The passive and active options should be able to be set. It should also allow for the target destination to be in a subfolder. Some hosts require it to be in public_html – so you’d have to chdir there first.

Database

Well of course! It would also be cool to have it migrate the database. I know this can be done but I didn’t even try to do a proof of concept with this. There are a number of different types of connections for MySQL servers – so it could be difficult to find the proper way to do this.

All in all, I’m happy. Thought it would be a great addition to the open source software section of my blog if I got around to it!

Anti Spam Solution for small / medium business?

Monday, September 22nd, 2008

One of the biggest internal battles I have is whether to host my email locally on my 16mb/2mb network, source it out to the web host, or use a specialized webmail company. I like that feeling of control, the fact that I know I could set up any anti spam and virus filtering service I wanted if I fully controlled my e-mail. However, if my internet service goes down, or I have a power outage, no email! I’ve always liked to tinker with my own servers as well – but I think getting a full rack mount solution is just outside of my budget. ;)

Solution for Small to Medium sized Businesses

Enter Perimetec. Perimetec’s combination of products and services was one of the main reasons why I spent so much time on their site when I first visited it. First off, their Email Spam Blocker service looks pretty cool. For $2.50 a month per mailbox, I can get usage of this service -basically an account on their Barracuda appliance. (Looks like you can get a discount if you sign up for a year.) Not only will this provide me with top of the line spam filtering, it will even queue email if my service goes down. (Hrm… Maybe I should start looking at that whole closet set up I have again… can that old 450mhz box handle my mail load?). Add-ons to the service include DNS management and Webmail.

Products for those with their own systems

If you’re more on the ball than I am, and have everything all set to go in your closet/computer room/server farm, then check out Perimetec’s Barracuda Anti Spam products. Touting what appears to be the full line of Barracuda products, it looks like Perimetec’s in a good place to offer good deals on their line.

What would I do?

Lets be honest – free trials are awesome – but they’re usually short. Too short. Perhaps you’ve not even had a chance to use a Barracuda product before either. Whether you’re just a small business or looking to get your own system, sign up for the service first. For $2.50 a month on an email account, you can check out the type of service your own appliance will give you. Then, when you’re ready to take beef up your Email Spam Protection, definitely consider upgrading with Perimetec.

  • twitter loader

Follow me on twitter: @aaronsaray

The views on this website are my own and do not reflect the opinions of my employer or clients.
Creative Commons License Home | Open Source | Book | Music | Art | Bio | Resume | Contact
My Baby