Listing Magento Categories & their IDs

Sometimes you may want a list of all the categories you have on your Magento store and their corresponding IDs.  For instance if you are uploading multiple products and want a reference to all of your category IDs.

Below is a simple php script which will generate a csv file which you can open in excel to see a list of your categories, their IDs and URL path.

define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();

$category = Mage::getModel ( 'catalog/category' );
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();

if ($ids) {
    $file = "var/category-ids.csv";
    file_put_contents($file,"Category ID, Category Name, URL Path");
    foreach ( $ids as $id ) {
        $string = "\n";
        $string .= $id . ", " .$category->load($id)->getName() . ", " . $category->load($id)->getUrlPath() . ", ";
        file_put_contents($file,$string,FILE_APPEND);
    }
}

Place the php script in your magento root directory and call it get-category-ids.php. Then point your browser to yourmagento.com/get-category-ids.php and the csv will be created in your var/directory of your magento root.