How to recursively make a directory (folder) with PHP

When I need to generate or save files in a web application it’s important that I keep all the files in an orderly fashion.

Sometimes I think that there’s a fine line between impressionistic and messy.
Lady Gaga

Here’s the go-to code I use to generate the directory structure.

You can find more solution on this StackOverflow thread.

How it works

is_dir() is a native PHP function that will say if a relative path is given is a directory or not. If the path doesn’t exist, it will return false.

|| – is the OR operator in PHP. If the path is a directory exists the function will return without moving over to the second part, because we’re saying do this OR that.

mkdir() is a native PHP function that will create a directory at the path location that is given.

The parameter $path is the path where the directory should be created.

The parameter $permissions is the chmod() settings of directory being created.

The ‘true’ creates the directory recursively. This means that you only need to create the path, and you don’t need to make sure the whole path exists. It will fill in the gaps.

Why not just use mkdir() by itself?

You can, but if you do, and the directory already exists, it will send out an error to the log. Nothing terrible with that, I just like keeping things clean.

Don’t be TOO clever with your UX designs – Why I cancelled my Coin

Edit: After reviewing Plastic, I decided to go with them. First and foremost, their security isn’t over-thinking things. Second, their card actually recharges, so you won’t have to buy a new one after a year+.

* * *

I was excited when Coin announced their campaign. I love technology, and this is a great example of innovation being used to make life easier. I have several credit cards, it’s good for your credit score, and keeping track of them is a pain and risky. I’d rather keep them safely at home and use secure device out in the wild.

When I first received the notification to download the app I set it up. I had a little trouble setting up the Tap Code. I had a little musical training growing up, but that was a long time ago, and I never got around to learning Morse Code. I finally entered a code that was acceptable, but how do you write that down?

Months later I saw that Coin was starting to ship so I tried to sign into my app to see when mine would come. I’d been waiting for it for a while and was excited.

Tap Code: Damn. What was it again?

IMG_0097

I went to reset it, had trouble getting something complicated enough that was acceptable. Again. This time I tried noting it down in 1Password with dashes and dots.

Later that day I decided to try signing in again. Even with the dashes and dots I couldn’t sign into the app.

I applaud them for trying something new, but at that point I was starting to get concerned about ever being able to use my Coin. So I looked through the app, no there is no alternative to using the Tap Code.

At that point I wrote to them asking them if there are plans for an alternative for people like me who can’t seem to get their innovative password alternative working. The email I received in response, I felt, was patronizing. It explained how to set up the Tap Code, and made me feel like an idiot. I’ll give them the benefit of the doubt, maybe they’ve had a bunch of people who couldn’t set it up, maybe due to my frustration I was reading into the tone of the email. The person clearly didn’t read my complaint and sent me an answer explaining what they thought I was asking about.

Maybe I should learn Morse Code, but that wasn’t even the issue, I couldn’t get it working even after I had written down my code. I responded asking if they have plans integrating an alternative for people like me, and no they didn’t . So I requested a refund.

With innovations, it’s important to make sure they work well, if you want people to use them. If your innovation is truly unique and different, It’s also important to make sure there are alternatives, if you want your users to be comfortable enough to try your innovation out.

Even Apple had a password fallback when they first implemented the fingerprint reader. Now the password fallback is gone, but that was there for the first year+, until they ironed out all the details.

In addition to being concerned about being able to use my coin at all, I was just as concerned about their approach to UX. I’m not an average user, and I couldn’t use their technology effectively. If they’re not responsive nor thoughtful about THIS innovation, what other aspects of their system and security are they NOT thinking through?

At least I got my money back, but I’m disappointed. I wanted to use their tech.

I don’t know how many people they testing this concept with. I couldn’t find many articles about the security benefits of tap codes over pins. Historically Tap Codes are interesting, but they’re for communicating, not for identity. I think it’s a real shame that their approach is bullheaded and unthoughtful.

If you feel that this site is running snappier, you’re probably right.

I went to 3 meetups this week: Codepen.io, WPNYC, and Web Performance NY.

At two of them people mentioned Percona. So I thought I’d check it out. Digital Ocean has a great tutorial if you’d like to check it out: How To Install a Fresh Percona Server or Replace MySQL. I got stuck for a bit, but found a solution deep in the comments of a StackOverflow answer. It turns out that when I removed MySQL to install Percona it removed the php5-mysql module.

Before installing it commando on my personal site, I tried it first in my dev box at work and got a 5x improvement in speed.

This is not a scientific experiment, I implemented two other improvements as well:

  • Switched from W3 Total Cache to WP Super Cache.
  • Expanded my Digital Ocean instance as Percona recommends 1GB RAM.

Hope this helps you’re reading experience at my digital home.

Cheers!

“The Crystal Goblet or Printing Should be Invisible”

This metaphor is truly stunning and puts into words the careful balance between that must be maintained between content and container. This is as true for a site’s design and the content it contains as it is for typography and the text it contains.

“Imagine that you have before you a flagon of wine. You may choose your own favorite vintage for this imaginary demonstration, so that it be a deep shimmering crimson in colour. You have two goblets before you. One is of solid gold, wrought in the most exquisite patterns. The other is of crystal-clear glass, thin as a bubble, and as transparent. Pour and drink; and according to your choice of goblet, I shall know whether or not you are a connoisseur of wine. For if you have no feelings about wine one way or the other, you will want the sensation of drinking the stuff out of a vessel that may have cost thousands of pounds; but if you are a member of that vanishing tribe, the amateurs of fine vintages, you will choose the crystal, because everything about it is calculated to reveal rather than to hide the beautiful thing which it was meant to contain.

“Bear with me in this long-winded and fragrant metaphor; for you will find that almost all the virtues of the perfect wine-glass have a parallel in typography. There is the long, thin stem that obviates fingerprints on the bowl. Why? Because no cloud must come between your eyes and the fiery heart of the liquid. Are not the margins on book pages similarly meant to obviate the necessity of fingering the type-page? Again: the glass is colourless or at the most only faintly tinged in the bowl, because the connoisseur judges wine partly by its colour and is impatient of anything that alters it. There are a thousand mannerisms in typography that are as impudent and arbitrary as putting port in tumblers of red or green glass! When a goblet has a base that looks too small for security, it does not matter how cleverly it is weighted; you feel nervous lest it should tip over. There are ways of setting lines of type which may work well enough, and yet keep the reader subconsciously worried by the fear of ‘doubling’ lines, reading three words as one, and so forth.”

“The Crystal Goblet or Printing Should be Invisible”
from Beatrice Warde, The Crystal Goblet, Sixteen Essays on Typography, Cleveland, 1956

Learning to Walk in Space

When a child learns to walk, they take leaps beyond what their skill and strength should allow. They look out at all the people walking around them, and see what is possible. Their muscles may not be strong enough to fully support them, to balance and stand, but they see what is possible. They pull themselves up by a table leg and stand. They may bounce on their feet a few times in anticipation of walking, and then they leap over, stumble a few steps, and fall onto a couch, or into a parent’s arms.

These are a child’s first steps.

When we, humanity, started our space journey our technological muscles were only beginning to be formed. Germanium was still being used in transistors, and Apollo’s computers had approximately 64Kbyte of memory and operated at 0.043MHz.

Landing on the moon was that stumbling first step that a baby takes from the table, lunging towards a couch.

Since then we’ve been strengthening our muscles. We now have cars that can drive themselves and more computing power than we know what to do with in our own pockets.

We have more than reached the time when we need to let go from holding on to the safety of the coffee table. We need to let go, and take steps out into the universe.

We dreamt of hotels on the moon and we think that we failed because they don’t exist yet.

But those dreams were the dreams of a baby believing they’re walking because a parent is holding their hands up. Our technological muscles weren’t strong enough to support hotels on the moon.

Our technology has matured and now is the time to build those hotels. Solving the problems that we will need to solve, to have a manned base on the moon, will reverberate through our economy. Such a project will reap dividends beyond our imagination.

There are a lot of technological problems that will need to be solved for man to safely set up camp off our planet. How will we cheaply and efficiently get past lower orbit? What sort of life sustaining systems will we need to build to survive there? Are there building materials there? How much water can be found there?

Cellphone cameras, solar cells, and artificial limbs each were developed by NASA to solve a problem, and each became an industry in itself, generating jobs, creating wealth, and improving our overall quality of life. The solutions we create to set up camp on the moon will do the same.

We should do this in parallel to the missions planned to Mars. The moon is close and shouldn’t wait. What we learn there will apply further out and it should pay for itself in dividends.

We should send man back to the moon. We should build science stations on the moon.

We should build hotels on the moon.

I Didn’t MVP

I’m currently learning iOS development.

When I start learning something new, I like to play with it. I like to take it apart into tiny pieces and then put it back together. “What happens with I put this piece back over here? Or add this one instead?” You can do that with code, and that’s a lot of fun.

When I started to gain momentum, I decided to do a project, and use that project to learn more about the language, the process, and the iOS ecosystem. I set a deadline; I wanted to have an app ready and in the app store before the end of the February. I didn’t make that deadline.

I think that learning a new language, for me at least, contradicts the concepts of developing a product.

When I’m learning how to code something, I add features, why? Why not? It’s something else to play with and it doesn’t matter how clunky my code is because it’s only there for me.

When building a product you really need to keep it as simple as possible. At least for v1. If you want to test a concept, see if it has appeal and will be useful for your target audience, you need to keep the experience simple, and the features to a minimum.

So that’s what happened. I didn’t MVP. But I’m okay with that. I have a working prototype of a much more complex app on my phone that I can show people, it’s the conversation piece I wanted it to be and looks and works pretty rad.

The MVP is MUCH simpler, and I’ll have it out much sooner than I could the current working model.

Long Live the QR Code

QR codes aren’t dead, they were just never implemented well.

In the past two weeks WhatsApp and Snapchat both announced different implementations of QR codes. What’s different now? Why would these work, and nothing else before them?

WhatsApp uses the code to connect a specific browser with the app so that WhatsApp Web can be loaded, similar to the WordPress plugin I wrote. Snapchat added QR codes for connecting with people reducing the friction of having to remember/type in a username.

There’s a lot of hate and cynicism towards QR codes. What they’re complaining about, mostly, comes down to ridiculous implementation and unnecessary use of the codes. Frankly they sound like luddites with their complaining.

I’ve been disappointed with the overall implementation. QR codes are great tools to pass a small amount of information from the physical world to your mobile device. Unfortunately they’re mostly used as a gimmick. Think of them as an alternative to a shortened URL. If you don’t want to force someone type in a long URL, that would be an ideal use for a QR code.

But you still probably shouldn’t use a QR code for most situations today; unfortunately, you need a special app to use them. Why? I don’t know, QR code deciphering libraries are built into the native iOS camera library. What’s really wrong with QR codes today is that they’re not easy enough to use.  If the camera had a “scan” setting along side the useless auto crop tool “square” people might actually discover intelligent implementations of QR codes.

The vast majority of implementation today are ads. When I see one out in the wild I usually won’t pull out my phone because there aren’t good apps for scanning QR codes. I usually snap a picture of the ad if I’m really interested in it, then pull up the ad later when I have an extra minute.

A good, and relevant use of a QR code for an ad would integrate location. If you scan the code, and that takes you to an app or site that verifies your location you get 30% off. But that would only really work if that specific location was important. If you’re an ad agency, there’s a freebie for you. Use it for the grand opening of a new store and place a treasure hunt in a 6 block radius of the new location.

There’s very little you can do with QR codes that you can’t with NFC or iBeacons. But there IS a bar of technical know-how for implementing either low frequency radio powered tool, that you don’t have with QR codes. Implementing QR codes properly is really a matter of treating them as a tool, and implementing them in a way where people might actually find them useful.

Enter WhatsApp and Snapchat. Here’s what they did right:

  1. The functionality is built straight into the app, no need for a nonexistent well-designed QR code reader to use the new feature.
  2. It reduces friction by providing a less complicated way to do something that might otherwise be more complicated. They did not use the codes as a gimmicky extra step.

An added benefit is that it actually DOES have the intrigue a gimmick might have due to the fact that they actually got the implementation right. Proof, all the great press they got for it.

P.S. I’m currently working on a companion iOS app for the plugin I wrote. Stay tuned…

Support NASA – The Plan

Anyone who knows me and has ever been around when the topic of space has come up knows that I’m a little obsessed with space and NASA. I know I’m not the only one. So once this idea stuck in my head, it’s stuck.

The Problem

The NASA Administrator is authorized by 42 U.S.C. 2473(b) to accept unconditional gifts or donations of services, money and property. However they are not allowed to solicit donations. Because of that, until now, the best way to donate to NASA has been through here.

That is inconvenient and we are sure deters many people from sending in checks or money orders.

The Plan

An simple organization whose sole purpose is to collect those monies and donate them to NASA. (I may have an accountant to help me set this up.)

A simple landing page (SupportNASA.org – yes I own it now) that enables people to donate to NASA. Money will be collected via online payment form and donated to NASA on a regular basis. Of course,

Org will be run completely off donated time and services. If money is needed that will be raised separately. Due to the simplicity of the plan I don’t think there are many administrative costs aside from money fees.

Bonus: Volunteer designers and developers will build a new donate page monthly… Because it’s cool. (Already have the launch landing page lined up. If you’re interested in grabbing a month, let me know.)

Questions:

  • Does anyone see any issues with this plan? (legal, practical…)

  • Will NASA be okay with this? Will they take our money?

Why?

If you want to build a ship, don’t drum up people to collect wood and don’t assign them tasks and work, but rather teach them to long for the endless immensity of the sea.

– Antoine de Saint-Exupery

In depth, in the words of Neil deGrasse Tyson. 

Brain trust: Feedback please!

Dear NASA

I just emailed NASA. If anyone knows anyone who works there, please put me in touch. This HAS to happen, doesn’t need to be me…  But this HAS to happen.

To whom it may concern,

I am a space enthusiast and web developer. I manage a team of developers and have been building websites for over 10 years now.

I saw the video here http://www.penny4nasa.org and was inspired, but also frustrated. It’s a beautiful campaign, but the monies do not go directly to you. I’m sure what they’re doing is important, but I know people would prefer to donate directly.

I truly believe that most americans would happily donate $1/yr and many $1/mo just to see NASA do all it wants to do.

I was upset to discover that the only way to donate directly is via this form: http://nodis3.gsfc.nasa.gov/displayDir.cfm?Internal_ID=N_PD_1210_001G_&page_name=main

How are people supposed to do that? Countless dollars have gone into researching the best ways to remove all boundaries from people who want to donate/pay with their money.

Is there no legal way to bring this into a simple online form?

I would be happy to set up a simple page through which all the money would go directly to you. I would be happy to do this gratis, only for my love of NASA and all NASA has done and does. I believe that simple metrics on that page showing a goal and how far we are from there would be all that’s needed to inspire.

Please let me do this for you!!!

If you are interested in considering just let me know and I’ll mock up a design.

Best wishes,

Jack Reichert

How to use JSONP in WordPress Development. The Quick-and-Dirty QuickStart Guide

Edit: Looking for an easier way to do JSONP in WordPress?

What IS JSONP and why should I care?

There’s been a lot of debate lately about whether to move your site to SLL (https) or not. It’s clear, though, that all your forms, at least the ones that handle sensitive data like logins, should go over a secure connection.

If your whole site is SSL then you won’t have any issues with making AJAX calls; however, if you DO go hybrid then Javascript will consider all the pages that are SSL as a separate domain and will block AJAX calls to them.

Use case: You want to secure your admin area and you add define(‘FORCE_SSL_ADMIN’, true) to your wp-config.php file. You’ll remember that to make AJAX calls in WordPress you need to call: “/wp-admin/admin-ajax.php.” But that will now be an SSL connection and violates Javascript’s “same-origin policy“.

This is where JSONP comes into the picture. As you probably know, you CAN include scripts from other sites using a script tag, that ability is the foundation of CDNs. JSONP takes advantage of that functionality.

Basically, instead of including a static script on your site hosted on a  CDN, you’re sending a GET request to the domain of choice, and asking it to send back a callback method including within it a JSON object with the data you want. In essence, you’re spoofing your browser to think that it’s not actually doing a cross-domain ajax call, but in practice, you are.

There’s a great discussion on StackOverflow about JSONP for further exploration.

How do I do it?

Below you’ll find two files, upload them to a folder in your WordPress plugins directory and activate. Note: you should probably run this on a test site. It won’t do anything bad to your site, but it’s never a good idea to play around on your production site.

To test it add the following to any page:

<input name="title" type="text" /> <input name="titleSubmit" type="button" />

The javascript looks for an input button, on ANY page of your site, with the name “titleSubmit” and does the JSONP call with the content of an input text field on that page with the name of “title”.

The most important pieces in the example that you’ll need to get your code running are:

In the js file:

Clearly without any of the code it won’t work properly; however, the above settings are not typical of most of your standard AJAX calls.

type: GET: By it’s very nature a JSONP call cannot be POST. You’re essentially embedding a script tag on your page with GET variables in the URL of the script.

cache: false: Removing this will not break your code, but for testing it’s best if you your browser doesn’t cache the responses.

dataType: “jsonp”: You’re telling jQuery that it’s a JSONP call. Duh.

crossDomain: true: Perhaps with the dataType set to JSONP you’d think the that this would be enabled, that’s the whole point, but you still need to set this.

In the PHP file:

content-type: text/javascript: You’re setting the response type being sent, application/javascript will work as well.

access-control-allow-origin: *: This is another security check, you need to enable any origin for JSONP to work.

$_GET[‘callback’]: Without the callback wrapping your JSON response jQuery will not recognize this as a valid response. It is set in place as a security measure similar to NONCEs.

Conclusion

Once you have the right recipe, JSONP with WordPress is not all that difficult. Use it well.

As always, questions? comments? Don’t hesitate to do so below.