OTFG Step 4: Running the Import Script

At long last here's the import script I used: import-flickr-photos.php.

If you want to try it out, be sure to add all of your personalized information I've mentioned in the previous steps (1,2,3) to the top of the script.

Here's what the script does:
  • Authenticates the person running the script at Flickr via the browser. (You'll have to give your script permission to read all of your photos.)
  • Requests the total number of photos for the authenticated account from the Flickr API (to help with looping).
  • Requests all of the account's photos, asking for standard info (title, FlickrID, whether or not it's public) and some extra details (relevant dates, and the longitude and latitude).
  • Loops through every photo, adding the photo information to the database if it isn't already there.
  • Downloads the original photo file from Flickr and saves it locally if it isn't already there.
  • Requests the description (caption) and tags for the particular photo from the Flickr API. (Requires a separate API call, unfortunately.)
  • Adds the file location, description, and tags to the db.
  • Finally, the script sleeps for one second before doing anything else. (Seemed like the polite thing to do so the script doesn't hammer the API.)
In my last post I mentioned that Flickr Backup was clunky to use, but this script is a thousand times clunkier. If Flickr Backup is a Ford Taurus, then running this script is like taking your covered wagon out on the Oregon trail: no shocks, no rubber tires, no paved roads, and you'll likely die of cholera before it's finished. I'm kidding on that last part, but the script probably will die before all of your photos are saved locally. Not to worry, you can run the script multiple times without duplicating files or data. The script checks for existing records and files before taking any action. I have 513 photos at Flickr—which isn't too many in the scheme of things—and I still needed to run the script a couple times to get all of them. (I set a ridiculously high timeout at the top of the script, but the script seemed to die anyway.)

To run the script, open it in your browser. The URL should be something like:


The script will redirect you to Flickr where you'll need to log in and/or authorize the script. From there, the magic starts. The script will try to give you some info about what's happening, but if you don't see anything but a blank page, don't worry, it's probably working. If you can, log into your server and check out the photos directory you set up. You should see folders and files appearing. Another way to check progress is by firing up MySQL and running some counts on your table. Something like this:

SELECT Count(PhotoID) FROM photos

If the script is working the count will be higher than zero.

It's important to note that the import script is grabbing every photo you uploaded to Flickr, even those marked as friends and family only. This is exactly what I wanted to happen, but if you want something different, check out the documentation for the flickr.photos.search method and tweak line 65 of the script. You can set a privacy_filter argument in the call to get a list of only photos that are public, for example.

So, once this script finished, I had a bunch of local directories filled with photos that I'd uploaded to Flickr over the past three years. I also had 513 records in the photos table and 1,646 records in the tags table describing those photos. That means I (and some others) added about 3.2 tags per photo. huh. So I can't really look at my photos through the Web yet, but at least they're ready for the next phase. Not too shabby for a few hours on a Saturday afternoon.

Disclaimer: As I mentioned before, OTFG is an off-the-top-of-my-head project. So if you try any of this stuff out, please don't hold me responsible for your toaster catching on fire. I'm sharing this project publicly to show how I'm going off the Flickr grid, and to hopefully get some feedback in the process.

Next Up: Set Theory
« Previous post / Next post »



Loving what you're doing here. I'm following along as well, and wanted to note that the script you've got (as you no doubt already know) fails completely under PHP4 for the following:

1) The recursive argument to mkdir() wasn't added until PHP5.
2) There is no file_put_contents() function in PHP4.

Also, I had a problem with case sensitivity (your MySQL script from the first day created tables called 'photos' and 'tags,' but PHP croaked on me because it was looking for tables called 'Photos and Tags').

And, well, a note would be handy to point out that the path name for photos on disk REQUIRES a slash at the end of it.

Thought I'd toss this up for any other PHP4 users and/or people having the case sensitivity issue: http://pics.ariffic.com/import.phps

Again, this is really fascinating stuff, and something I've wanted to do for a while now. Something's always nagged at me about using Flickr for my photos, but I could never quite put my finger on it. :) Looking forward to the next article!
Thanks for the encouragement, Phil! And thanks for your PHP4 notes. pics.arrific.com isn't resolving for me right now, but I'm looking forward to checking out your changes.

I fixed the table names in the script here, and added a note about a trailing slash in the comments.
aha now your site is working for me, nice! Good point on the slashes for Windows vs. Unix around line 130. Maybe the slash style could be set in a variable at the top of the script. Definitely something to note for future scripts.
If you have GIFs and PNGs at Flickr in addition to JPEGs, be sure to check out Phil's comment on another post:


This import script only supports JPEGs, so you'll have to do a little work to grab other file types.
Hello, I know that your not here to do support on this and your are already generous to share your script but mabe you can give me a hint :
all the three firts steps did go well for me but wen I run the script the page is not able to load and I have a firefox warning :

Firefox detected that the server redirects the request for this address in a manner which will not succeed.

Can it be due to a missconfiguration of the scipt (ex: is it because I did not put the good path to photo directory ?)

(sorry for the poor english....)
Hi Guillaume, the only piece of this script that could be doing any sort of redirecting is the authentication stuff in phpFlickr. You might make sure that the files phpFlickr.php and auth.php are in the same directory as your import script. You might also double-check your callback URL setting for your Flickr API key. It really sounds like this issue is related to phpFlickr, so I think you could start debugging there.
ok thanks for this rapid answer
I will look my phpFlick files !
Hi! You're reading a single post on a weblog by Paul Bausch where I share recommended links, my photos, and occasional thoughts.

Search Results

No emoji found