[eluser]Clooner[/eluser]
=======================================================
There is an updated version which can be found here
=======================================================
I used to upload my projects using a textmate bundle. This was handy because it happened automatically but still needed some manual labour since some things couldn't be overwritten and had to be moved first. I made this tool for uploading parts of my development project to the live site. This project gives the possibility to super easy upload your project directory to a ftp directory fully automatic without copying unnecessary files therefor no need for cleaning up after
Making this a one click deploy system.
Code: function Welcome()
{
parent::Controller();
$this->data=array();
$this->data['excludes']=array(
'.ftpssh_settings',
'.git',
'.htaccess',
'files',
'system',
'pictures',
'assets',
'application/plugins',
'application/config/config.php',
'application/config/database.php',
'.gitignore');
$this->data['all_excludes']=array('.','..','.DS_Store');
$this->data['remote_dir']='your_remote_directory';
$this->data['local_dir']='The_dir_you_want_to_upload';
$this->data['config']=array();
$this->data['config']['hostname'] = 'ftp.yourhost.com';
$this->data['config']['username'] = 'username';
$this->data['config']['password'] = 'password';
$this->data['config']['port'] = 21;
$this->data['config']['passive'] = FALSE;
// we want to continue on errors because creating directories usually makes trouble here
$this->data['config']['debug'] = FALSE;
}
function view()
{
$files=$this->get_dir_listing($this->data['local_dir'],'',$this->data['excludes'],$this->data['all_excludes']);
sort($files);
echo "<pre>\r\n";
print_r($files,FALSE);
echo "</pre>\r\n";
}
function transfer()
{
// extended time out! for the bigger project :P
set_time_limit (99999999);
$files=$this->get_dir_listing($this->data['local_dir'],'',$this->data['excludes'],$this->data['all_excludes']);
// needed for correctly creating directories!
sort($files);
$this->load->library('ftp');
echo "trying: connect ".$this->data['config']['hostname']." as ".$this->data['config']['username'];
if ($this->ftp->connect($this->data['config']))
{
echo " - success<br/>\r\n";
$number=count($files);
$i=0;
foreach ($files as $totransfer)
{
$i++;
echo "[".$i.'/'.$number."] ";
// we want to see what is happening
flush();
if (is_dir($this->data['local_dir'].$totransfer))
{
echo "trying: create directory ".$totransfer;
// if the following fails it is not a problem because it means the dir already exitsts
if ($this->ftp->mkdir($this->data['remote_dir'].$totransfer))
echo " - succes<br/>\r\n";
else
echo " - error<br/>\r\n";
} else
{
echo "trying: upload file ".$totransfer;
// do the actual upload of the file
if ($this->ftp->upload($this->data['local_dir'].$totransfer, $this->data['remote_dir'].$totransfer))
echo " - succes<br/>\r\n";
else
echo " - error<br/>\r\n";
}
}
//gracefully close!
$this->ftp->close();
echo "done...<br/>\r\n";
} else
{
echo " - error<br/>\r\n";
}
}
function get_dir_listing($path,$prefix='',$excludes=array(),$all_excludes=array())
{
$result=array();
if ($handle = opendir($path))
{
while (false !== ($file = readdir($handle)))
if (!in_array($file,$all_excludes) && !in_array($prefix.$file,$excludes))
{
if (is_dir($path.$file))
$result=array_merge($result,$this->get_dir_listing($path.$file.'/',$prefix.$file.'/',$excludes,$all_excludes));
$result[]=$prefix.$file;
}
closedir($handle);
return $result;
} else
return false;
}
How it works
1. You just set the configuration and excludes files. the all_excludes array will not transfer any file with that name.
2. Open your browser and go to welcome/transfer
Viewing
With the view function you will see the files that will be uploaded
[eluser]renownedmedia[/eluser]
So... Is this ran on the localhost and uploads to the live server, or is it ran on the live server and downloads from a holding FTP server?
[eluser]Clooner[/eluser]
[quote author="Thomas Hunter" date="1251232092"]So... Is this ran on the localhost and uploads to the live server, or is it ran on the live server and downloads from a holding FTP server?[/quote]
Yes, this is ran from localhost and uploads to a live server
[eluser]renownedmedia[/eluser]
Sounds really kick ass to me!
[eluser]Clooner[/eluser]
Extended version (Part 1/2)
Code: <?php
class Transfer extends Controller {
function Transfer()
{
parent::Controller();
$this->config->set_item('compress_output', FALSE); // make sure to disable this since we usually compres the output!
$this->data = array();
// basic configuration!
$this->data['excludes'] = array(
'.git',
'.htaccess',
'svr_index.php',
'assets/', // we don't want to copy the assets every time
'application/controllers/transfer.php', // we don't want to copy this controller
'svr_maintenance.php',
'svr_sub_index.php');
$this->data['prefix'] = array( // we will make sure a correct message is displayed first
array('src' => 'svr_maintenance.php', 'dest' => 'index.php'));
$this->data['suffix'] = array( // restore the correct index.php for the sites!
array('src' => 'svr_index.php', 'dest' => 'index.php'));
$this->data['all_excludes'] = array('.', '..', '.DS_Store');
$this->data['local_dir'] = 'PATH_TO_LOCAL_DIR'; // we could also use the basedir in the case...
$this->data['remote_dir'] = 'PATH_TO_REMOTE_DIR';
$this->data['config'] = array();
$this->data['config']['hostname'] = 'THE FTP HOST';
$this->data['config']['username'] = 'THE FTP USER';
$this->data['config']['password'] = 'THE FTP PASSWORD';
$this->data['config']['port'] = 21;
$this->data['config']['passive'] = FALSE;
// we want to continue on errors because creating directories usually makes trouble here
$this->data['config']['debug'] = FALSE;
}
function countlines()
{
$path = $this->data['local_dir'].'application/'; // only get it from the application directory!
$files = $this->get_dir_listing($path, '', array(), $this->data['all_excludes']);
$count = 0;
foreach ($files as $file)
if (!is_dir($path . $file))
$count+=$this->countfilelines($path . $file);
echo $count . ' lines of code';
}
function countfilelines($filepath)
{
$handle = fopen( $filepath, "r" );
$count = 0;
while( fgets($handle) )
$count++;
fclose($handle);
return $count;
}
function getfilesize()
{
$files = $this->get_dir_listing($this->data['local_dir'], '', $this->data['excludes'], $this->data['all_excludes']);
$count = 0;
foreach ($files as $file)
if (!is_dir($this->data['local_dir'] . $file))
$count += filesize($this->data['local_dir'] . $file);
echo round($count / 1024 / 1024,2) . ' size of files is in mb';
}
function index()
{
echo '<p><a href="' . base_url() . 'transfer/countlines">Count lines of code in app dir</a></p>';
echo '<p><a href="' . base_url() . 'transfer/getfilesize">Get file size of code</a></p>';
echo '<p><a href="' . base_url() . 'transfer/view">View files</a></p>';
echo '<p><a href="' . base_url() . 'transfer/execute">Transfer files</a></p>';
}
function view()
{
$files = $this->get_dir_listing($this->data['local_dir'], '', $this->data['excludes'], $this->data['all_excludes']);
sort($files);
echo "<pre>\r\n";
print_r($files, FALSE);
echo "</pre>\r\n";
}
[eluser]Clooner[/eluser]
Extended version part 2/2
Code: function execute()
{
// extended time out! for the bigger project :P
set_time_limit (99999999);
$files = $this->get_dir_listing($this->data['local_dir'], '', $this->data['excludes'], $this->data['all_excludes']);
// needed for correctly creating directories!
sort($files);
// add the prefix!
$files = array_merge($this->data['prefix'], $files);
// add the suffix!
$files = array_merge($files, $this->data['suffix']);
$this->load->library('ftp');
echo "trying: connect " . $this->data['config']['hostname'] . " as " . $this->data['config']['username'];
if ($this->ftp->connect($this->data['config']))
{
echo " - success<br/>\r\n";
$number = count($files);
// first copy
$i = 0;
foreach ($files as $totransfer)
{
$i++;
echo "[" . $i . '/' . $number . "] ";
// we want to see what is happening
flush();
if (!is_array($totransfer) && is_dir($this->data['local_dir'] . $totransfer))
{
echo "trying: create directory " . $totransfer;
// if the following fails it is not a problem because it means the dir already exitsts
if ($this->ftp->mkdir($this->data['remote_dir'] . $totransfer))
echo " - succes<br/>\r\n";
else
echo " - error<br/>\r\n";
} else
{
if (is_array($totransfer))
{
$srcfile = $totransfer['src'];
$dstfile = $totransfer['dest'];
}
else
{
$srcfile = $totransfer;
$dstfile = $totransfer;
}
echo "trying: upload file " . $srcfile;
// do the actual upload of the file
if ($this->ftp->upload($this->data['local_dir'] . $srcfile, $this->data['remote_dir'] . $dstfile))
echo " - succes<br/>\r\n";
else
echo " - error<br/>\r\n";
}
}
//gracefully close!
$this->ftp->close();
echo "done...<br/>\r\n";
} else
echo " - error<br/>\r\n";
}
function get_dir_listing($path,$prefix='',$excludes=array(),$all_excludes=array())
{
$result = array();
if ($handle = opendir($path))
{
while (false !== ($file = readdir($handle)))
if (!in_array($file, $all_excludes) && !in_array($prefix.$file, $excludes))
{
if (is_dir($path . $file))
$result=array_merge($result, $this->get_dir_listing($path . $file . '/', $prefix . $file . '/', $excludes, $all_excludes));
$result[] = $prefix . $file;
}
closedir($handle);
return $result;
} else
return false;
}
}
/* End of file transfer.php */
/* Location: ./application/controllers/transfer.php */
[eluser]louis w[/eluser]
This looks very cool. Thanks for sharing.
I have been tossing around an idea for a deploy-able service and this is a good start on how to get it out.
[eluser]marinaccio[/eluser]
Awesome, beats doing it by hand! Thank you!
|