:::: MENU ::::

Reading PHP serialized data quickly in a Linux Terminal

Here’s a quick script for reading a data file containing serialized data generated by PHP, and outputting it in a human readable format to a Linux command line interface. A short web search found no existing utilities for achieving this, so hopefully it will be useful.

You must use absolute paths (using tilde also expansion works, of course). Save this as a file called deserialize-php, make it executable, and move it into your /usr/bin folder to make it universally accessible.

#!/usr/bin/env php
<?php
 
// Enable error reporting
error_reporting(E_ALL | E_STRICT);
 
// Get command line arguments
$userArgs = $argv;
 
// Check that an argument was provided
if ( !isset( $userArgs[1] ) ) {
    echo "\nNo path submitted: a path to a serialized PHP data file is required'\n\n";
    die();
} elseif ( '/' == substr( $userArgs[1], 0, 1 ) ) {
    // The supplied path is absolute
    $serializedPath = $userArgs[1];
} else {
    // If the supplied path is relative
    echo "\nRelative path supplied but not supported, please provide an absolute path\n\n";
    die();
}
 
// Get the serialised data for processing
$serialized = file_get_contents( $serializedPath );
 
// Unserialize the data
$unserialized = unserialize( $serialized );
 
// Print the data array in readable form
print_r( $unserialized );

Then you should be able to use it like this:

$ unserialize-php serialized-data.php

2 Comments

  • Reply Per Westermark |

    Why do you require absolute path for the input file?

    That is a very impractical requirement when someone is standing in the directory where the serialized data is.

    Consider programs like cp, mv, diff, wc, …

    They would not be fun to use if they only allowed absolute paths.

So, what do you think ?