I think, therefore I am. I am, therefore I sail

Category: PHP Page 4 of 11

PHP: Recursively remove a directory and all files and folder contained within

The rmdir function of PHP cannot remove a directory if it has anything inside of it, whether it be files or folders. The following method can be used to recursively remove all the files and folders from the directory, and will remove the directory itself. To use it simply copy the code into your application and call rrmdir with the path to the directory you would like to remove.

/**
 * Recursively removes a folder along with all its files and directories
 * 
 * @param String $path 
 */
function rrmdir($path) {
     // Open the source directory to read in files
        $i = new DirectoryIterator($path);
        foreach($i as $f) {
            if($f->isFile()) {
                unlink($f->getRealPath());
            } else if(!$f->isDot() && $f->isDir()) {
                rrmdir($f->getRealPath());
            }
        }
        rmdir($path);
}

PHP: Recursively create directories

The mkdir function in PHP is great for creating directories, but what if you need to create a new directory structure? For example, you run mkdir(“/this/is/a/path”) but only “/this” exists. mkdir will fail because /this/is/ does not exist. The way to solve this is to pull the path apart and check each segment to ensure that it exists and create it if it does not exist.I created a recursive mkdir function and would like to share it with you. To get this to work simply copy the function into your code and call rmkdir instead of PHP’s mkdir.

function rmkdir($path) {
    $path = str_replace("\\", "/", $path);
    $path = explode("/", $path);
    
    $rebuild = '';
    foreach($path AS $p) {
        
        if(strstr($p, ":") != false) { 
            echo "\nFound : in $p\n";
            $rebuild = $p;
            continue;
        }
        $rebuild .= "/$p";
        echo "Checking: $rebuild\n";
        if(!is_dir($rebuild)) mkdir($rebuild);
    }
}

PHP 5: Recursively move or copy files

I was looking for a quick code example on recursively moving files with PHP and I only saw code snippets using PHP 5. I like using the new and powerful features of PHP 5 when doing file system interactions so I wrote two quick PHP 5 functions that will move or copy directories and files recursively. That means your entire file tree will be moved or copied. The following is the code you can use for reference with your own recursive move or copy functions.

Move

/**
 * Recursively move files from one directory to another
 * 
 * @param String $src - Source of files being moved
 * @param String $dest - Destination of files being moved
 */
function rmove($src, $dest){

    // If source is not a directory stop processing
    if(!is_dir($src)) return false;

    // If the destination directory does not exist create it
    if(!is_dir($dest)) { 
        if(!mkdir($dest)) {
            // If the destination directory could not be created stop processing
            return false;
        }    
    }

    // Open the source directory to read in files
    $i = new DirectoryIterator($src);
    foreach($i as $f) {
        if($f->isFile()) {
            rename($f->getRealPath(), "$dest/" . $f->getFilename());
        } else if(!$f->isDot() && $f->isDir()) {
            rmove($f->getRealPath(), "$dest/$f");
            unlink($f->getRealPath());
        }
    }
    unlink($src);
}

Copy

/**
 * Recursively copy files from one directory to another
 * 
 * @param String $src - Source of files being moved
 * @param String $dest - Destination of files being moved
 */
function rcopy($src, $dest){

    // If source is not a directory stop processing
    if(!is_dir($src)) return false;

    // If the destination directory does not exist create it
    if(!is_dir($dest)) { 
        if(!mkdir($dest)) {
            // If the destination directory could not be created stop processing
            return false;
        }    
    }

    // Open the source directory to read in files
    $i = new DirectoryIterator($src);
    foreach($i as $f) {
        if($f->isFile()) {
            copy($f->getRealPath(), "$dest/" . $f->getFilename());
        } else if(!$f->isDot() && $f->isDir()) {
            rcopy($f->getRealPath(), "$dest/$f");
        }
    }
}

Deploying with the Cloud 9 IDE

(Updated: Nov 2, 2012)

Stephen Owens wrote up a great intro to using the Cloud 9 IDE to build on a development environment and utilize a modified version of the above code to then migrate to the production server. See his comment below for details.

A quick look at the upcoming packaging tool in the Windows Azure SDK for PHP

Note: The Windows Azure SDK for PHP referenced in this post has been released and there is official documentation available at http://azurephp.interoperabilitybridges.com/articles/packaging-applications please be sure to also check there as the content in this post may become outdated.

The current Windows Azure Command-line tools for PHP can be sometimes difficult and frustrating to work with. Luckily the Interoperability team at Microsoft has listened and is working hard to make Windows Azure development for PHP developers easier. The upcoming Windows Azure SDK for PHP begins to consolidate the various PHP toolkits into one package. This package will have a brand new and way easier to use packaging tool!

I am going to provide you with a brief intro to this tool and how to use it. Take note that this code is still in development and should be used with extreme care. There is no guarantee that it will work until the official release of the new Windows Azure SDK for PHP.

Get a copy of the Windows Azure SDK for PHP from Subversion

Since the new SDK has not yet been officially release we will need to get the most up to date copy from the Subversion repository on CodePlex.

Fire up your favorite tool and pull from this URL:
https://PHPAzure.svn.codeplex.com/svn

Find the new Package tool

Open the folder the checkout is in and navigate to the trunk folder. You will notice that there is now a bin folder. Inside that folder you will find several batch scripts. These will comprise the new set of command-line tools. Watch the Azure PHP website for tutorials on using each tool when the new SDK is officially released.

For now all we care about is the Package.bat file.

Setup a project

*Added* You can download the HelloAzure sample project from GitHub at https://github.com/blobaugh/Windows-Azure-Sample-Kit-4-PHP/tree/master

With the old command-line tools it was difficult to control your .csdef and .cscfg files. With the new tools in the SDK it is a breeze. The following is the structure you will need for a project with a single web role named WebRole.

  • ProjectDir
    • WebRole/
    • ServiceDefinition.csdef
    • ServiceConfiguration.cscfg

As a quick demo copy a .csdef and .cscfg from a previous project and inside the WebRole folder create an index.php file containing your favorite version of Hello World.

Build the package

Here is the part that will amaze you with its ease! Open a command prompt and navigate to the bin dir for the Windows Azure SDK for PHP which you checked out from Subversion. In that directory you will run this command (substitute the paths for yours of course)

Package.bat create –in=”Path\to\your\package” –out=”Path\to\output\package\files” –dev:true

  • -in – The path to your project
  • -out – The path to place the output files from the packager. This will be the .cscfg and .cspkg, etc
  • -dev – Run the local development fabric? Values are true or false

If it worked your web browser should have opened to http://localhost:81 and displayed your application. Check the output directory you specified and you will find the files you upload through the Windows Azure Portal to get your application running on Windows Azure.

Wrap up

Keep a close eye on both the Open @ Microsoft blog and the Azure PHP website for an announcement of the new Windows Azure SDK for PHP, along with tutorials on using the new tools.

Windows Azure PHP Documentation

I was asked to stop by the SQL Server Jumpin camp yesterday and talk to PHP developers about building and migrating applications to Windows Azure. One of the frustrations I heard was a lack of documentation around PHP on Windows Azure. The complaint was that documentation is hard to find because it is scattered around blogs all across the internet and much of it is outdated. I feel the frustrations there!

I want to let you know that the Interoperability Team at Microsoft has been working hard not only at improving PHP on Windows Azure, but also at documenting usage and providing a comprehensive, cohesive, tool set.

Be sure you take a look at the Azure PHP website as it contains a wealth of knowledge regarding everything from connecting to Windows Azure Storage to managing service deployments and is continuing to grow.

The Azure PHP site can be found at http://azurephp.interopbridges.com

Also keep your eyes on the Open @ Microsoft blog through which the Interoperability team regularly communicates updates and new features.

The Open @ Microsoft blog can be found at http://blogs.msdn.com/b/interoperability

And of course there is also the ever present Open @ Microsoft Twitter account

Microsoft is continuing to push support for PHP on Windows Azure and wants your feedback. If you have questions, ideas for new articles, or just need to gripe, you can contact the Interoperability team here http://www.interoperabilitybridges.com/contactus

Page 4 of 11

Powered by WordPress & Beards