Programming

My Hour of Code Playlist for First Graders

This year I volunteered for Hour of Code. I visited my older son’s classroom, and it was so rewarding to see all 25 first graders in the room deeply engaged in a coding activity. We talked about Computer Science, my job as a Software Engineer and the company I work for, watched some videos, and worked on “The Foos”, a really fun game/coding activity designed by Code Spark Academy for the Hour of Code. Continue reading

Standard
PHP, Programming

Creating a Directory, Adding Files and Deleting Everything

I was running a test and it came really handy to create a temporary directory, store some files in it, and later remove all the files and the directory itself.

$path = 'path/to/your/new/directory';

// Create directory if it doesn't already exist
if(!file_exists($path)) {
    mkdir($path, 0777, true);
}

// Add a text file
file_put_contents($path . 'text.txt', 'Hello World');

// Add a base64 image file
$data = 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAB1ElEQVRYR+2XzVHD' .
  'MBSEd31guBE6UNIAoQKgAnJJwo1QAe6AUAGkgsANwoF0QNJBKgDRARyBGS0of/xMkpExIh' .
  'zQjO2xR9J+elr7PRPjpnpxAHBjch/1Kg3Yudv0GvQn7ZltKLmJKvp1cuc2eWUHnwGkPjt3' .
  '2zFBVCv2QG6BbocXtvcFAMfs3DbjApSaII6CAVQ1ZZBtkH1e3qZTz/jnL7Ds2ocswKplAF' .
  'DVNECegLiHU8PvmSqmgJWkDaIC5w54Zc9+HOCTiHSOZ6V+pSOzsg1xHXANdmw3i/jQ8CER' .
  'GHYC0o8i04FSH4kavLA2q3g4gA/zKgpeRHvGwPEaZBnKb9KgCLx/nEoppCMQj3CqeA98Z9' .
  'UfxwQDqF7sAtwF0MKTa2Z1+zzQIICRCdkFdPodoy2KUhBA3jD/A/xHYGEE6qVTAIezkxFk' .
  'AaVwyJRggk2boADxDGRhDkDwVPk7zqwH8k8bPsPfAhjleAtiLXwJOXoKj3h2xn/ehyXZuD' .
  'A1APzxG81O0vkU4DdUZ2lQNVMBebgUAKnFaZm8DAL/GxAJoAW62fWikjKAk5HxYgGM3/G5' .
  'RUm9pLgAUA9ibw5AeVjSR41AqJ+ibUFGAJ+d9kPH/Gi/t5+eV4F3mU/7boBsAAAAAElFTk' .
  'SuQmCC';

$data = base64decode($data);
$image = imagecreatefromstring($data);
imagepng($image, $path . '/image.png', 0);
imagedestroy($image);

// Up until this point, if you step debug you should
// be able to see the directory and files in it.
// Let us now clean up and remove everything.

// I'm going to use PHP's DirectoryIterator class
// If you're not familiar with it you should check it out
// It has some cool methods.
$tmp_files = new DirectoryIterator($path);
foreach($tmp_files as $file) {
    // I only created files, but there are a couple of 
    // directories that get in the way (i.e. '.' and '..')
    if($file->isFile()) {
        // So, if it's a file let's get rid of it
        unlink($path . $file->getBasename());
    }
    // now remove the directory
    rmdir($path);
}

 

Standard
Symfony

Creating a Database With Symfony and Doctrine

Today, I created my first sample database using Symfony. In just a few steps I was able to have my db up and running. It was awesome!

First, I updated my db parameters in “parameters.yaml”. I was worried that this wouldn’t work because I’m using a Vagrant box (Laravel Homestead) and you don’t really see examples for this approach in the documentation. So, I just added theconfig items (db host, port, name, user, etc.) the same way I have them configured in Sequel Pro. It worked.

Here’s a sample parameters.yml:

parameters:
    database_host: 127.0.0.1
    database_port: 33060
    database_name: product_db
    database_user: homestead
    database_password: secret
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: dcc9090991abc12aaaadbb787111cee

Next, I created an Entity class. Continue reading

Standard
Uncategorized

Hello world!

What better way for a coder than starting his blog with a “Hello World”. It’s just perfect! What you will find here is just stuff that I find interesting, not necessarily about programming. It could be photography, gadgets, or soccer, but in the end it could lean more towards programming and the like. In summary, this blog is just going to be about anything that catches my interest.

Cheers,

David.

Standard