Tutorial: Automated Site Backup with PHP and FTP

January 25, 2009 No comments yet

In this tutorial we are going to make a PHP script that archives your website into a .tar file, then automatically moves it to an external FTP server for safe keeping. We can then setup a cron for this script which will execute it automatically every day/week/month , however frequent you want it. This will only work on Linux servers however that shouldn’t be a problem as most hosts run Linux.

First we’ll setup some variables to allow for easy editing.

$dir = '/path/to/file'; // Directory to backup.
$filename = 'backups/backup' . date("MdY") . '.tar'; //path to where the file will be saved.

$ftp_server = 'urlToFTPServer.Com'; //External FTP server
$ftp_user_name = 'ftpUsername'; //External FTP server username
$ftp_password = 'ftpPassword'; //External FTP server password

Now we need to archive the site. We’re going to wrap an if statement around it so if for some reason it doesn’t archive, the FTP part of the script won’t try and copy it over to another server. The system function executes a system command, here we are calling tar cvf which will archive our $dir (directory) and save it to the path specified in $filename.

if(system("tar cvf $filename $dir")){

We’ll then connect to the FTP…

Other jQuery Uses

January 19, 2009 No comments yet

We traditionally think that jQuery’s selector engine can only select elements/information off of the document that we are working on. And AJAX’s only use is to process web forms without the page needing to reload. While both of these are common uses, they are not the only uses. In this tutorial I’m going to show you a not-so-mainstream use of jQuery, but a very useful one all the same.

Take this standard web page. It pulls the latest news items from a database and displays them like so. That is the basic DOM of the page.

<head>
<title>Latest News</title>
</head>

<body>

# More Header, sidebar code here #

<p class="latest">Employees at the Karate Bank are getting the chop</p>
<p class="latest">NASA shares have sky-rocketed</p>
<p class="latest">Investors at the Sushi Bank feel they are getting a raw deal</p>

# More content, footer code here #

</body>

</html>
</pre>

If we wanted to highlight all of the news items on this page we can use jQuery’s each method to iterate through them and modify their CSS.

$("p.latest").each(function(){

	$(this).css({"background-color" : "yellow"});

});

But what if we wanted to pull that information from another page? Due to some restrictions in the software the website is…

Using the keyword "this" in PHP

January 8, 2009 1 comment

You may come across number of functions, variables inside a class, you
call each one them as object of the class.

The keyword “this” is used in a class in php , to call and use these objects as
shown in the example below.

HTTP Request without CURL

January 8, 2009 No comments yet

A short tutorial on how to make a simple object for HTTP request without the need of using CURL.
The way to accomplish that is using the function stream_context_create to prepare the string, and then you use fopen and stream_get_contents to get the response.

<?php

class Custom_Http_Request
{
    private $_url;
    private $_body;
    private $_method = 'POST';
    private $_headers = array();
    private $_response;
    private $_stream;

    public function __construct($url, $body)
    {
        if (empty($url) || empty($body)) {
            throw new Exception('Both URL and BODY are required '
                              . 'for fetching the request.');
        }

        $this->_url  = $url;
        $this->_body = $body;
    }

    public function setMethod($method)
    {
        if ('POST' == $method || 'GET' == $method) {
            $this->_method = $method;
            return $this;
        }

        throw new Exception('Invalid method set.');
    }

    public function addHeader(array $header)
    {
        if (!empty($header)) {
            $this->_headers[] = $header;
            return $this;
        }

        throw new Exception('The headers are empty.');
    }

    public function getMethod()
    {
        return $this->_method;
    }

    public function getBody()
    {
        return $this->_body;
    }

    public function getHeaders()
    {
        return $this->_headers;
    }

    public function getResponse()
    {
        if (is_null($this->_stream)) {
            $this->_openStream();
        }

        if (is_null($this->_response)) {
            $this->_response = @stream_get_contents($this->_stream);

            if (false === $this->_response) {
                throw new Exception('It is not possible to '
                                  . 'read from the response.');
            }
        }