Joho the Blog » programming

March 8, 2019

Keep JavaScript dumb

I’ve been a hobbyist programmer since I got my first computer in 1984 or so. I greatly enjoy it and I’m terrible at it.

I mainly use JS to create utilities for myself that take me 1,000 hours to write and save me a lifetime of 45 seconds. I like Javascript for tawdry reasons: It’s straightforward, there’s a huge collection of libraries, any question I might ever have has already been asked and answered at StackOverflow, and I get to see the results immediately on screen. It’s of course also useful for the various bush league Web sites I occasionally have to put up (e.g., Everyday Chaos). Also, jQuery makes dumb HTML (DOM) work easy.

But here’s the but…

But, ECMA is taking JS in a terrible direction: it’s turning it professional, what with the arrow functions and the promises, etc. If you’re a hobbyist who enjoys programming for the logic of it, the new stuff in JS hides that logic on behalf of things I happen not to care about like elegance, consistency, and concision.

Now, I know that I don’t have to use the new stuff. But in fact I do, because the community I rely on to answer my questions — basically StackOverflow — increasingly is answering with the new stuff.

There’s a reason JS became the most used language on the planet: not only can you do webby stuff with it, it has a pretty linear learning curve. Now I literally feel like I’m in danger of losing “View Source” from my browser … literally because while I can view the source, increasingly I can’t understand it.

I’m going to lose this argument. I already have lost it. I should lose it. My position is wrong. I know that. Nevertheless, I stand firmly on the wrong side of history as I declare in my lonely, quavering voice: Keep. JavaScript. Dumb.


December 27, 2015

Embedded endnote extractor

I’ve updated a 2009 utility that lets you embed your end notes in the text you’re typing. The utility, Footnoter, extracts the endnotes, leaves a footnote number, and compiles a list of the endnotes with numbers and links. It now works with Markdown as well as with HTML; I use Markdown for most of what I write these days.

In other words, let’s say you type this in a document you’re creating with Markdown:

I write using Markdown. ((See John Gruber’s Daring Fireball for more.)) Markdown lets you embed formatting codes into plain text that are then rendered into formatted HTML, Word, etc.((The Marked app adds a viewer with export capabilities. It’s on sale for $9.99 right now.)), enabling me to focus purely on what I’m saying. It also lets me keep my fingers on the keyboard.

If you paste this text into Footnoter and tell it you want Markdown output, it will treat the comments between the double parentheses as endnotes. It will remove those comments from the body of the text, leaving the Markdown code for an endnote number, and will compile a list of endnotes with the proper references back to their endnote numbers. That is, it does what you would expect. At least with my limited testing.

For Markdown, that means the above text gets turned into this:

I write using Markdown.[^fn2] Markdown lets you embed formatting codes into plain text that are then rendered into formatted HTML, Word, etc.[^fn3], enabling me to focus purely on what I’m saying. It also lets me keep my fingers on the keyboard.

[^fn2]:See John Gruber’s Daring Fireball for more.
[^fn3]:The Marked app adds a viewer with export capabilities. It’s on sale for $9.99 right now.

Don’t be freaked out. That’s what endnotes look like in Markdown. When you run them through a parser, they’ll have appropriately numbered superscripts. (Footnoter generates arbitrary unique Markdown labels for endnotes; they start with “fn” and then have numbers appended sequentially. Those numbers have nothing to do with the number the parser will assign to the endnote itself. Also, yes, it’s a little bug that Footnoter starts with fn2 instead of fn1. Non-critical. I’m working on it. [Minutes later]: Fixed it. I think.)

The same thing happens if you are writing HTML except the markup that’s generated is more like this:

I write using Markdown.<span class=’fn_in_text’><a name=’fn2′><a href=#fnend2>2</a><</span> Markdown lets you embed formatting codes into plain text that are then rendered into formatted HTML, Word, etc.<span class=’fn_in_text’><a name=’fn3′><a href=#fnend3>3</a></span>, enabling me to focus purely on what I’m saying. It also lets me keep my fingers on the keyboard.

And that gets rendered in a browser as this:

I write using Markdown.2 Markdown lets you embed formatting codes into plain text that are then rendered into formatted HTML, Word, etc.3, enabling me to focus purely on what I’m saying. It also lets me keep my fingers on the keyboard.

There are a number of options, including setting the delimiters for endnotes and, for HTML, which endnote number to begin with. By default it removes the space before an endnote, so you can put a space between the word where the superscript should be and your delimiters, making your text easier to read when you’re working on it.

Also, if you work on a text, run it through Footnoter, work on it some more and add more endnotes, Footnoter should detect that and begin its arbitrary numbering of Markdown endnotes above where you left off. That means you can run it through more than once and it should still work.


Note: This code is from 2009. I’ve learned some stuff since then, including that jQuery makes life easier. When I added the Markdown option yesterday, I didn’t bother cleaning up the old code. It is particularly hideous. You can gape at its uglinesss at github.

PS: Yes, I really should have named it “Endnoter.”

Comments Off on Embedded endnote extractor

April 3, 2014

Hackathon love

Last weekend I was a judge at the Toronto Startup Weekend – Library Edition and was reminded again not ony how much I love hackathons, but how unexpected they are.

Toronto Startup Weekend for Libraries

The Toronto event wasn’t strictly speaking a hackathon. A hundred people met, many pitched ideas, and then people formed teams. They had to come up with a business idea and pitch it to five judges, explaining their idea, perhaps including a demo, showing their research (including user surveys if appropriate), and making the case for it as a sustainable business enterprise. (Non-profits welcome.) It was a fantastic event.

But to keep things simple, consider a classic hackathon: developers get together for a day or a weekend and are challenged to write working code, usually constrained to a particular genre (e.g., games) or using an open data set (e.g., the DPLA hackathon or the Open Syllabus Project hackathon). And the amazing thing is that they do it.

Just think about all that had to happen to make a hackathon possible and not a cruel joke.

We need browsers and HTTP and the ability to request data through them.

We need well-documented, standard ways of requesting that data.

We need open sources of data.

We need Open Source software to let us build on work done by others.

We need frameworks that let us do easy things incredibly easily.

We need libraries so we can do complex things incredibly easily, such as visualizing data.

We need an Internet to connect programs to data, software to users, and everyone to everyone.

We need an ethos that encourages sharing, experimenting, and prototyping — finding what’s right in a project not all that’s gone wrong or has been left unfinished.

I love hackathons.

Comments Off on Hackathon love

March 29, 2013

[2b2k] Back when not every question had an answer

Let me remind you young whippersnappers what looking for knowledge was like before the Internet (or “hiphop” as I believe you call it).

Cast your mind back to 1982, when your Mommy and Daddy weren’t even gleams in each other’s eyes. I had just bought my first computer, a KayPro II.

I began using WordStar and ran into an issue pretty quickly. For my academic writing, I needed to create end notes. Since the numbering of those notes would change as I took advantage of WordStar’s ability to let me move blocks of text around (^KB and ^KK, I believe, marked the block), I’d have to go back and re-do the numbering both in the text and in the end notes section. What a bother!

I wanted to learn how to program anyway, so I sat down with the included S-Basic manual. S-Basic shared syntax with BASIC, but it assumed you’d write functions, not just lines of code to be executed in numbered order. This made it tougher to learn, but that’s not what stopped me at first. The real problem I had was figuring out how to open a file so that I could read it. (My program was going to look for anything between a “[[” and a “]]”,, which would designate an in-place end note.)The manual assumed I knew more than I did, what with its file handlers and strange parameters for what type of file I was reading and what types of blocks of data I wanted to read.

I spent hours and hours and hours, mainly trying random permutations. I was so lacking the fundamental concepts that I couldn’t even figure out what to play with. I was well and truly stuck.

“Simple!” you say. “Just go on the Internet…and…oh.” So, it’s 1982 and you have a programming question. Where do you go? The public library? It was awfully short on programming manuals at that time, and S-Basic was an oddball language. To your local bookstore? Nope, no one was publishing about S-Basic. Then, how about to…or…well…no…then?…nope, not for another 30 years.

I was so desperate that I actually called the Boston University switchboard, and got connected to a helpful receptionist in the computer science division (or whatever it was called back then), who suggested a professor who might be able to help me. I left a message along the lines of “I’m a random stranger with a basic question about a programming language you probably never heard of, so would you mind calling me back? kthxbye.” Can you guess who never called me back?

Eventually I did figure it out, if by “figuring out” you mean “guessed.” And by odd coincidence, as I contemplate moving to doing virtually all my writing in a text editor, I’m going to be re-writing that little endnoter pretty soon now.

But that’s not my point. My point is that YOU HAVE NO IDEA HOW LUCKY YOU ARE, YOU LITTLE BASTARDS.


For those of you who don’t know what it’s like to get a programming question answered in 2013, here are some pretty much random examples:


February 5, 2013

[berkman] Diana Kimball: Coding as a Liberal Art

Diana Kimball [twitter:dianakimball] is giving a Berkman lunchtime talk on coding as a liberal art. She’s a Berkman Fellow and at the Harvard Business School. (Here are some of her posts on this topic.)

NOTE: Live-blogging. Getting things wrong. Missing points. Omitting key information. Introducing artificial choppiness. Over-emphasizing small matters. Paraphrasing badly. Not running a spellpchecker. Mangling other people’s ideas and words. You are warned, people.

She says that she’s loved computers since she was a kid. But when she went to Harvard as an undergrad she decided to study history, in part because there’s a natural specialization that happens in college: the students who come in as coders are fantastic at coding, whereas Diana had greater strengths as a writer of prose. She found HTML and programming intimidating. But in her third year, she got interested in coding and Internet culture. She was one of the founders of ROFLcon [yay!]. She got hired by Microsoft after college, as a technical product manager with the Powerpoint team in Silicon Valley. “This was culture shock in the best possible way.”

When she graduated in 2009, she and some friends started found the SnarkMarket blog that considers what the new liberal arts might be (inspired by Kottke). She wrote an essay that’s a proposal for coding and decoding. She reads it. (It’s short.) An excerpt:

Coding and Decoding is about all modes of communication, and all are in its view. But it is built with particular attention to the future, and what that future will be like. Technological experts can seem like magicians, conjuring effects wordlessly. By approaching that magic as a collection of component parts instead of an indivisible miracle, we can learn to see through these sleights of typing hands. In seeing through, we will learn to perform them ourselves; and think, as magicians, about the worlds we will build.

Language, now, is about more than communication. It is the architecture behind much of what we experience. Understanding that architecture will allow us to experience more.

Her boyfriend taught her how to code. They spent a lot of time on it. “He picked up on something I’d said and took it seriously.” After two years at Microsoft, she was enthusiastic, but still a beginner. It wasn’t until she started at Harvard Business School that coding really took off for her. The entrepreneurial atmosphere encouraged her to just do it. Plus, she was more of a geek than most of the other students. “This was great for my identity, and for my confidence.” She also found it a social refuge. “It takes a lot of time to get over the hump.” She refers to Ellen Ullman’s “Close to the Machine” that talks about the utility of being arrogant enough to obsess over a project, cycling back to humility.

She decided to code up her own site for a project for school, even though the team had been given the money to hire devs for the task. Last fall she took the famous CS50 course [Harry Lewis, who created the course in about 1981, is sitting next to me.] CS50 teaches C, targeted at people who are either taking only that one class, or are going to take many more. For her final project, she did a project that used multiple APIs that she was very proud of. She’s also proud of her Ruby projects folder. Each project is something she was trying to teach herself. She’s more proud of the list than the finished products.

“Learning to code means reclaiming patience and persistence and making them your stubborn own.” [nice]

Ideally, everyone should be exposed to programming, starting at 5 yrs old, or even earlier, Diana says. Seymore Papert’s “Mind-Storms” has greatly influenced her thinking about how coding fits into education and citizenship. At a university, it ought to be taken as a liberal art. She quotes Wikipedia’s definition. And if “grammar, rhetoric, and logic were the core of the liberal arts,” then that’s sound like coding. [Hmm.] What the law was to the liberal arts, programming ought to be, i.e., that which you try if you don’t know what else to do with your liberal arts degree.

Why isn’t it seen that way? When computer scientists teach you, they teach they way they learned: at school. But many of the best programmers are self-taught. CS50 does give a variety of assignments, but it’d be better if students solved their own problems much earlier.

But the number one problem is the academic attitude, she says. Students get fixated on the grade, even when it doesn’t matter. Coding is critical for children because debugging is part of it, as Papert says. But grades are based on the endpoints. Coding is much more like life: You’re never done, you can always make it better.

Diana has a proposal. Suppose coding classes were taught like creative writing workshops. Take it whenever you’re ready. Taught by hackers, esepcially autodidacts. It’d vary in substance — algorithms, apis, etc. — and you’d get to choose. You’d get to see something on screen that you’d never seen before And you’d be evaluated on ingenuity and persistence, rather than only on how well your code runs.

She says what her syllabus would look like:

“Coding should be taught in the same breath as expository writing… Everyone deserves to be exposed to it.” She’s not sure if it should be required.

She quotes Papert: “…the most powerful idea of all is the idea of powerful ideas.” There’s no better example of this, she says, than open source software. And David Foster Wallace’s commencement address: “Learning how to think really means learning to exercise some control over how and what you think…If you cannot exercise this sort of choice in adult life, you will be totally hosed.” Diana says that’s her. She was wrapped up in writing from an early age. She has a running internal commentary. [Join the club!] Coding swaps in a different monologue, one in which she’s inventing thing. That’s the greatest gift: her internal monologue is much more useful and interesting. “If you wanted to be a novelist in 1900, you’d want to be a programmer today.” The experience of creating something that people use is so motivating.


Q: Would you be willing to webcast yourself programming and let people join in? I do this all the time when at hackathons. I think, OMG, there must be 10,000 kids in India who want to be here. And so here they are. “Hackers at Berkeley” does this really well.

A: That’s awesome. I want more people to have more access to that experience of sharing.

Q: Are you familiar with RailsBridge — non-computer scientists who are teaching themselves how to code via weekend workshops.

A: RailsBridge is extraordinary. It’s great to see this happening outside of the university context.

A: [me] Great talk, and I’m a humanities major who spends most of his hobby time programming. But aren’t you recommending the thing that you happen to love? And programming as opposed to traditional logic is an arbitrary set of rules…

Q: Yes, but it would be really useful if more people loved it. We could frame it in a way that is exciting for humanities majors. I’m proposing an idea rather than making an airtight argument. “You’re basically right but I don’t really care” (she says laughing :).

Q: I like your idea of teaching it like a writers workshop so that it doesn’t turn into just another course. But I’m not sure that colleges are the best at doing that.

A: not everyone loves programming.

Q: [harry lewis] I take responsibility for eliminating the Harvard requirement for a programming course. Also, take a look at Third, the academic world treats computer science the way it does because of our disciplinary specialization. That label — computer science — came about in the context of fields like political science, and arose when computers were used not for posting web sites but for putting people on the Moon where a bug could kill someone. The fact that CompSci exists in academic departments will make it very difficult for your vision of computing to exist, just as creative writing is often an uneasy fit into English curricula.

A: That’s very fair. I know it’d be hard. RIT has separate depts for CompSci and coding.

Q: There’s an emergent exploration of coding in Arts schools, with a much more nimble, plug and play approach, very similar to the one you describe. My question: What do the liberal arts have to offer coding? Much of coding is quite new, e.g., open source. These could be understood within a historical context. Maybe these need to be nurtured, explored, broken. Does seeing coding as a liberal art have something to offer sw development?

A: ITP is maybe the best example of artists working with coders. Liberal Arts can teach programmers so much!

Q: Can we celebrate failure? That’d be a crucial part of any coding workshop.

A: Yes! Maybe “find the most interesting bug” and reward introspection about where you’ve gone wrong. But it’s hard in a class like CS50 where you’re evaluating results.

Q: This is known as egoless programming. It’s 40 years old, from Weinberger [no relation].

Q: You’re making a deeper point, which is not just about coding. The important thing is not the knowledge you get, but the way you get there. Being self-reflective about you came about how you learn. You can do this with code but with anything.

A: You’re so right. Introspection about the meta-level of learning is not naturally part of a course. But Ruby is an introspective language: you can ask any object what it is, and it will tell you. This is a great mirror for trying to know yourself better.

Q: What would you pick to teach?

A: I love Ruby. It would be a good choice because there’s a supportive community so students can learn on their own afterwards, and it’s an introspective language. And the lack of ornament in Ruby (no curly braces and little punctuation) makes it much more like English. The logic is much more visible. (My preference is Sinatra, not Rails.)

Q: What sort of time commitment the average person would have to put in to have a basic grasp of a programming language? Adults vs. children learning it?

A: I’d love to see research on this. [Audience: Rottmeyers, CMU (?)] A friend of mine reported he spent 20 hours. The learning curve is very halting at first. It’s hard to teach yourself. It helps to have a supportive in-person environment. CS50 is a 10-20 hour commitment/week and who has that sort of time except for fulltime students? To teach yourself, start out a few hours a time.

Q: How about where the MOOCs are going? Can you do a massively online course in compSci that would capture some of what you’re talking about?

A: The field is so focused on efficiency that MOOCs seem like an obvious idea. I think that a small workshop is the right way to start. CS50 requires so much fear of failure and resilience that it wouldn’t have been a good way for me to start. At CS50, you can’t let others read your code.

Q: We shouldn’t put together Computer Science and programming. Programming is just a layer of expression on top of computer science. You don’t need compSci to become a programmer. And the Net is the new computer; we’re gluing together services from across the Net. That will change how people think about programming because eveyrone will be able to do it. The first language everyone should learn is

Q: I’m a NY Times journalist. I love languages. And I love the analogy you draw. I’m 30. Do you think coding is really essential? Would it open my eyes as a journalist?

A: It’s never too late. If you keep asking the question, you should probably do it. You don’t have to be good at it to get a lot out of it. It’s so cool that your children are learning multiple languages including coding. Learn alongside them.


September 9, 2012

Beginner2Beginner: Javascript multi-file upload + PHP to process it

Much as I love being a hobbyist programmer, it can sometimes be frustrating as all get-out. Sometimes it’s just a bug because I made a dumb error (getting a variable’s scope wrong) or because I made an assumption about how something works (BBedit‘s hex dump does not show you the content of the file on the disk, but of the file in memory, including the line endings it’s transformed). But then there are the frustrations that come from not having the slightest idea of the basics. The worst are the basics that are so basic that the explanations of them assume you already know more than you do.

Welcome to the world of pain known as uploading multiple files using Javascript. For example, suppose you are writing an app that lets users take notes on an article using a plain old text processor. They can then upload those note files to some code that processes them, perhaps turning them into a database. Rather than having users upload one file at a time, you want to let them upload a bunch.

There are plenty of slick, free examples on the Web that provide beautiful front ends for doing this. Many of them I could get to work, but not exactly the way that I wanted. Besides, I was really really really confused about what happened after the front end.

So, after a lot of skullpounding and forehead slapping, here’s a guide. But please take seriously this warning: I barely know what I’m doing, and I’m undoubtedly doing this in the clunkiest fashion possible. I am very likely getting important things wrong. Some of them may be fatal, at least in the programmatic sense. (If you have a correction, please let me know. Note I may fix the code that follows, rather than doing the usual — and proper — Web thing of striking through the errors, etc.) Here goes….

To enable uploads, you’ll be writing code that will live in two places. The user interface code is in the HTML running in the user’s browser. The files are going to be uploaded — copied — to a web server. The code in the browser is going to be Javascript. The code on the server is going to be PHP, because Javascript is for browsers. (Oversimplification noted.) Those two places can be physically the same machine. If you’re using a Mac, a web server comes standard; if you don’t have a web server handy, there are bunches of free ones; that’s beyond the scope of this post.

After experimenting with many of the beautiful packages that are available, I gave up and went with the HTML5 solution. Modern browsers will have no problem with this. If you need to design for the world of old-fashioned browsers, then you need to find a competent explanation. In short: I love you, but go away! (Apparently, if you use the HTML5 solution, it will still be usable in lesser browsers, although it will allow users to select only one file at a time.)

HTML5 makes it ridiculously easy to do the user interface bit: you just tell it you want to allow multiple selections within a file-chooser dialogue. This comes from a post by Tiffany Brown:

<form action="processThem.php" method="post" enctype="multipart/form-data">
<input type="file" value="" name="upload[]" multiple>
<button type="submit">Upload!</button>

This will create an input field that when clicked will launch a file-browsing dialogue box in which the user can select multiple files. It then sends it to the PHP script called “processThem.php” on your server. The two key points to note are that the first line does the work of allowing multiple choices, and the “[]” in the name turns that variable into an array that will pass the entire list of user choices to the PHP script waiting on your server.

Here’s what it looks like, although this particular example won’t actually upload anything:

Now you have to create the “processThem.php” script (or whatever name you’ve specified) on your server. The uploaded files get placed in an array called $_FILES. But, they don’t get stored on the server for long: they are stored in a temporary folder from which they are automatically deleted after a little while. So, you need to process them, and quite possibly move them for permanent storage to a folder of your choosing. Here’s some sample PHP code from an anonymous commenter (“Me”) on the Tiffany Brown post:

$error_message[0] = "Unknown problem with upload.";
$error_message[1] = "Uploaded file too large (load_max_filesize).";
$error_message[2] = "Uploaded file too large (MAX_FILE_SIZE).";
$error_message[3] = "File was only partially uploaded.";
$error_message[4] = "Choose a file to upload.";

$upload_dir  = './tmp/';
$num_files = count($_FILES['upload']['name']);

for ($i=0; $i < $num_files; $i++) {
    $upload_file = $upload_dir . urlencode(basename($_FILES['upload']['name'][$i]));

    if (!preg_match("/(gif|jpg|jpeg|png)$/",$_FILES['upload']['name'][$i])) {
        print "I asked for an image...";
    } else {
        if (@is_uploaded_file($_FILES['upload']['tmp_name'][$i])) {
            if (@move_uploaded_file($_FILES['upload']['tmp_name'][$i], 
                $upload_file)) {
                /* Great success... */
                echo "hooray";
                //$content = file_get_contents($upload_file);
                //print $content;
            } else {
                print $error_message[$_FILES['upload']['error'][$i]];
        } else {
            print $error_message[$_FILES['upload']['error'][$i]];

Let’s walk through this.

$error_message[0] = "Unknown problem with upload.";
$error_message[1] = "Uploaded file too large (load_max_filesize).";
$error_message[2] = "Uploaded file too large (MAX_FILE_SIZE).";
$error_message[3] = "File was only partially uploaded.";
$error_message[4] = "Choose a file to upload.";

In the first few lines, Me does us the favor of providing non-technical explanations of possible errors, so that if something goes wrong, it will be easier to know exactly what it was.

$upload_dir  = './tmp/';

Then Me designates a particular folder as the container for the uploaded files. Me chooses one called “tmp” in the same directory as the PHP script. (Make sure you have such a folder and the permissions are set, or, of course, create one with whatever name you’d like.)

$num_files = count($_FILES['upload']['name']);

Then Me gets a count of how many files were uploaded, and stores it in the variable $num_files. You tell that variable that you want the number of files that were included in “upload[]” in the form on your HTML. (You can use whatever name you want on that form, so long as you use the same one in your PHP.)

for ($i=0; $i < $num_files; $i++) {
    $upload_file = $upload_dir . urlencode(basename($_FILES['upload']['name'][$i]));

Then Me loops through all the files, assigning each one in turn to the variable $upload_file. But notice the weirdness of this part of the line:

$upload_file = $upload_dir . urlencode(basename($_FILES[‘upload’][‘name’][$i]));

First the easy parts. The baseline function returns just a file’s name without any path information; we want that because the point of this line is build a path to where the file will be saved in the folder you’ve set up for it. Also, I added the urlencode function in case the name of the file your user uploaded contains spaces or other characters that makes your server barf.

Now consider $_FILES[‘upload’][‘name’][$i]. It’s got those weird bracketed terms because $_FILES is an associative array. You can think of the usual sort of arrays as pairing a value with a number; give the array the number and it returns the value. In an associative array, values are paired with arbitrary keys (i.e., a word); give it the key and it returns the value. Here are the pre-defined keys for the associative array that gets sent to the PHP script when a user uploads files:

  • name: The file name of the uploaded file
  • type: Is it an image? A music file? etc.
  • size: The size in bytes
  • tmp_name: The crazy-ass name of the copy being stored on the server
  • error: Any error codes resulting from the upload

So, suppose you’re cycling through the array of uploaded files as in our example, and you want to get the name of the current file (i.e., file $i in the sequence):

$fname = $_FILES['upload']['name'][$i];

The [‘upload’] designates the array of all uploaded files. The [$i] pulls out of that array all of the information about one particular uploaded file, just like with ordinary array. The [‘name’] gets the value associated with that key for that particular file. As $i is incremented, you get the name of each file. If you wanted the crazy-ass temporary name, you would put in tmp_name instead of name, and likewise for the other parameters.

if (!preg_match("/(gif|jpg|jpeg|png)$/",$_FILES['upload']['name'][$i])) {
        print "I asked for an image...";

Next Me optionally checks the name of the uploaded file for a particular set of extensions in case you want to limit the uploads to images (as in Me’s example) or whatever. Me is using regex to do the work, a function for which I am doomed to a copy-and-paste understanding.

  if (@is_uploaded_file($_FILES['upload']['tmp_name'][$i])) {

Next Me does a check to make sure that the file was actually uploaded and is not the result of malicious code. is_uploaded_file is a built-in PHP function. The preceding “@” suppresses error messages; I don’t know why Me did that, but I’m confident it was the right thing to do. While you are debugging, you might want to take the @ out.

move_uploaded_file($_FILES['upload']['tmp_name'][$i], $upload_file)

If the file passes that test, then Me moves it to the folder s/he designated. Note that we’ve already put the pathname to the storage folder into $upload_file.

Obviously, where the script has the comment “Great Success” you would put the code that does what you want to do to the uploaded file. I have added two lines — commented out — that get and print the content of the file; that gets ugly if the file isn’t some type of text file.

So, please let me know what I’ve gotten wrong here. (And maybe sometime I’ll give you some truly embarrassing code for returning the results of the PHP on the page that has the upload form on it.)


May 22, 2011

Remember what it was like to be dumb?

No, kids, you probably don’t.

I used to be a terrible, horrible, miserable hobbyist programmer. I enjoyed it a great deal, but land-o-lakes was I dumb!

I learned out of books, most of which are still bending the shelves they sit on. A good programming book is a pleasure. It teaches you the principles and the basic moves. But, programming is fun because it’s so specific. You need to measure the length of a line displayed in a particular font, or you want to set the opacity of a circle based on its diameter, and the book you’re using just does not happen to hit those examples. The time I used to spent guessing and poking around was not instructive and did not build character. It was simply what you had to do when you were dumb.

I am still a terrible, horrible, miserable hobbyist programmer. But my ability to solve problems, and, yes, eventually even to learn, has gone up orders and orders of magnitude because of three inter-related things:

1. All problems only arise the first time in a population once. Therefore, most problems have already been addressed by someone before you. They’ve either been solved by someone else or, if there are no solutions, someone has already discovered that.

2. It’s now so easy to make your work public

3. The hacker ethos has resulted in superb developers making their work available as examples and as entire libraries.

The second and third together has resulted in an enormous and public repository of questions, answers, examples, and explanations. (For example, see Rebecca Murphey’s introduction to JQuery…and then consider the centuries of engineering time libraries like JQuery have saved us. (Hat tip to ReadWriteWeb for the link to Rebecca’s book.))

4. Search engines are so damn good that we can find our way through that gigantic, unplanned repository.

You know every single thing I’ve just said. Still, it’s just good to remember now and then how amazing it is that we all know this as if it were always so. Especially if for you it has always been so.


January 20, 2011

If you laid out all the shelves in Harvard’s libraries…

Mainly because I wanted to futz around with the Google Maps API, I’ve created a mashup that pretends to lay out all the shelves in Harvard’s 73 libraries on a map.

Screen capture of map
Click to go to the page

You can choose your starting point — it defaults to Widener Library at Harvard — and choose whether you’d like to see a line of markers or concentric circles. It then pretends to map the shelves according to how many books there are in each subject.

Here’s where the pretending comes in. First, I have assumed that each book in the 12,000,000 volume collection is one inch thick. Second, I have used the Dewey Decimal system’s ten subject areas, even though Harvard doesn’t use Dewey. Third, I used an almost entirely arbitrary method to figure out how many books are in each subject: I did keyword subject searches. Sometimes, when the totals seemed way too low, I added in searches on sub-headings in Dewey. At the end, the total was probably closer to 4 million, which means my methodology was 300% unreliable. (Note: Math was never my strong suit.)

So, the actual data is bogus. For me, learning how to use the API was the real point. If you happen to have actual data for your local library, you can download the page and just plug them into the array at the beginning of the page. (All the code is in the .html file.)