Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Makalah
  • Ke-NU-an
  • Kabar
  • Search
Menu

Belajar PHP: Membuat Peta Folder (Map Directory)

Posted on August 12, 2012

Peta Folder atau Map Directory yang saya maksud adalah peta folder dan file yang ada pada suatu folder target, tersusun secara hierarkis dan dapat digunakan untuk keperluan lebih lanjut, seperti membangun interface treeview layaknya Windows Explorer.

Untuk membangun peta folder/map directory tersebut, saya menemukan fungsi menarik yang terpaketkan bersama CodeIgniter 2.0 (directory_helper.php). Berikut fungsinya:

[sourcecode language=”php”]
/**
* Create a Directory Map
*
* Reads the specified directory and builds an array
* representation of it. Sub-folders contained with the
* directory will be mapped as well.
*
* @access public
* @param string path to source
* @param int depth of directories to traverse (0 = fully recursive, 1 = current dir, etc)
* @return array
*/
function directory_map($source_dir, $directory_depth = 0, $hidden = FALSE)
{
if ($fp = @opendir($source_dir))
{
$filedata = array();
$new_depth = $directory_depth – 1;
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;

while (FALSE !== ($file = readdir($fp)))
{
// Remove ‘.’, ‘..’, and hidden files [optional]
if ( ! trim($file, ‘.’) OR ($hidden == FALSE && $file[0] == ‘.’))
{
continue;
}

if (($directory_depth < 1 OR $new_depth > 0) && @is_dir($source_dir.$file))
{
$filedata[$file] = directory_map($source_dir.$file.DIRECTORY_SEPARATOR, $new_depth, $hidden);
}
else
{
$filedata[] = $file;
}
}

closedir($fp);
return $filedata;
}

return FALSE;
}
[/sourcecode]

Untuk mencobanya, silakan buat satu script PHP kecil:
[sourcecode language=”php”]
$map =directory_map(‘./wp-admin’,2);
echo ‘<pre>’;
print_r($map);
echo ‘</pre>’;
[/sourcecode]

Contoh diatas akan membangun peta folder dari folder wp-admin dengan kedalaman hierarkis 2 layer. hasilnya:
[sourcecode]
Array
(
[0] => about.php
[1] => admin-ajax.php
[2] => admin-footer.php
[3] => admin-functions.php
[4] => admin-header.php
[5] => admin-post.php
[6] => admin.php
[7] => async-upload.php
[8] => comment.php
[9] => credits.php
[css] => Array
(
[0] => colors-classic.css
[1] => colors-classic.dev.css
[2] => colors-fresh.css
[3] => colors-fresh.dev.css
[4] => customize-controls-rtl.css
[5] => customize-controls-rtl.dev.css
[6] => customize-controls.css
[7] => customize-controls.dev.css
[8] => farbtastic.css
[9] => ie-rtl.css
[10] => ie-rtl.dev.css
[11] => ie.css
[12] => ie.dev.css
[13] => install.css
[14] => install.dev.css
[15] => media-rtl.css
[16] => media-rtl.dev.css
[17] => media.css
[18] => media.dev.css
[19] => wp-admin-rtl.css
[20] => wp-admin-rtl.dev.css
[21] => wp-admin.css
[22] => wp-admin.dev.css
)

[10] => custom-background.php
[11] => custom-header.php
[12] => customize.php
[13] => edit-comments.php
[14] => edit-form-advanced.php
[15] => edit-form-comment.php
[16] => edit-link-form.php
[17] => edit-tag-form.php
[18] => edit-tags.php
[19] => edit.php
[20] => export.php
[21] => freedoms.php
[22] => gears-manifest.php
[images] => Array
(
[0] => align-center.png
[1] => align-left.png
[2] => align-none.png
[3] => align-right.png
[4] => archive-link.png
[5] => arrows-dark-vs.png
[6] => arrows-dark.png
[7] => arrows-vs.png
[8] => arrows.png
[9] => blue-grad.png
[10] => bubble_bg-rtl.gif
[11] => bubble_bg.gif
[12] => button-grad-active.png
[13] => button-grad.png
[14] => comment-grey-bubble.png
[15] => date-button.gif
[16] => ed-bg-vs.gif
[17] => ed-bg.gif
[18] => fade-butt.png
[19] => fav-arrow-rtl.gif
[20] => fav-arrow.gif
[21] => fav-vs.png
[22] => fav.png
[23] => generic.png
[24] => gray-grad.png
[25] => gray-star.png
[26] => icons32-2x.png
[27] => icons32-vs-2x.png
[28] => icons32-vs.png
[29] => icons32.png
[30] => imgedit-icons.png
[31] => list.png
[32] => loading-publish.gif
[33] => loading.gif
[34] => logo-ghost.png
[35] => logo-login.png
[36] => logo.gif
[37] => marker.png
[38] => mask.png
[39] => media-button-image.gif
[40] => media-button-music.gif
[41] => media-button-other.gif
[42] => media-button-video.gif
[43] => media-button.png
[44] => menu-2x.png
[45] => menu-arrow-frame-rtl.png
[46] => menu-arrow-frame.png
[47] => menu-arrows.gif
[48] => menu-bits-rtl-vs.gif
[49] => menu-bits-rtl.gif
[50] => menu-bits-vs.gif
[51] => menu-bits.gif
[52] => menu-dark-rtl-vs.gif
[53] => menu-dark-rtl.gif
[54] => menu-dark-vs.gif
[55] => menu-dark.gif
[56] => menu-shadow-rtl.png
[57] => menu-shadow.png
[58] => menu-vs.png
[59] => menu.png
[60] => no.png
[61] => press-this.png
[62] => required.gif
[63] => resize-rtl.gif
[64] => resize.gif
[65] => screen-options-toggle-vs.gif
[66] => screen-options-toggle.gif
[67] => screenshots
[68] => se.png
[69] => sort.gif
[70] => star.png
[71] => stars-rtl.png
[72] => stars.png
[73] => toggle-arrow-rtl.gif
[74] => toggle-arrow.gif
[75] => upload-classic.png
[76] => upload-fresh.png
[77] => wheel.png
[78] => white-grad-active.png
[79] => white-grad.png
[80] => widgets-arrow-vs.gif
[81] => widgets-arrow.gif
[82] => wordpress-logo-2x.png
[83] => wordpress-logo.png
[84] => wp-badge-2x.png
[85] => wp-badge.png
[86] => wp-logo-vs.png
[87] => wp-logo.png
[88] => wpspin_dark.gif
[89] => wpspin_light.gif
[90] => xit.gif
[91] => yes.png
)

[23] => import.php
[includes] => Array
(
[0] => admin.php
[1] => ajax-actions.php
[2] => bookmark.php
[3] => class-ftp-pure.php
[4] => class-ftp-sockets.php
[5] => class-ftp.php
[6] => class-pclzip.php
[7] => class-wp-comments-list-table.php
[8] => class-wp-filesystem-base.php
[9] => class-wp-filesystem-direct.php
[10] => class-wp-filesystem-ftpext.php
[11] => class-wp-filesystem-ftpsockets.php
[12] => class-wp-filesystem-ssh2.php
[13] => class-wp-importer.php
[14] => class-wp-links-list-table.php
[15] => class-wp-list-table.php
[16] => class-wp-media-list-table.php
[17] => class-wp-ms-sites-list-table.php
[18] => class-wp-ms-themes-list-table.php
[19] => class-wp-ms-users-list-table.php
[20] => class-wp-plugin-install-list-table.php
[21] => class-wp-plugins-list-table.php
[22] => class-wp-posts-list-table.php
[23] => class-wp-terms-list-table.php
[24] => class-wp-theme-install-list-table.php
[25] => class-wp-themes-list-table.php
[26] => class-wp-upgrader.php
[27] => class-wp-users-list-table.php
[28] => comment.php
[29] => continents-cities.php
[30] => dashboard.php
[31] => deprecated.php
[32] => export.php
[33] => file.php
[34] => image-edit.php
[35] => image.php
[36] => import.php
[37] => list-table.php
[38] => manifest.php
[39] => media.php
[40] => menu.php
[41] => meta-boxes.php
[42] => misc.php
[43] => ms-deprecated.php
[44] => ms.php
[45] => nav-menu.php
[46] => plugin-install.php
[47] => plugin.php
[48] => post.php
[49] => schema.php
[50] => screen.php
[51] => taxonomy.php
[52] => template.php
[53] => theme-install.php
[54] => theme.php
[55] => update-core.php
[56] => update.php
[57] => upgrade.php
[58] => user.php
[59] => widgets.php
)

[24] => index-extra.php
[25] => index.php
[26] => install-helper.php
[27] => install.php
[js] => Array
(
[0] => cat.dev.js
[1] => cat.js
[2] => categories.dev.js
[3] => categories.js
[4] => comment.dev.js
[5] => comment.js
[6] => common.dev.js
[7] => common.js
[8] => custom-background.dev.js
[9] => custom-background.js
[10] => custom-fields.dev.js
[11] => custom-fields.js
[12] => customize-controls.dev.js
[13] => customize-controls.js
[14] => dashboard.dev.js
[15] => dashboard.js
[16] => edit-comments.dev.js
[17] => edit-comments.js
[18] => editor.dev.js
[19] => editor.js
[20] => farbtastic.js
[21] => gallery.dev.js
[22] => gallery.js
[23] => image-edit.dev.js
[24] => image-edit.js
[25] => inline-edit-post.dev.js
[26] => inline-edit-post.js
[27] => inline-edit-tax.dev.js
[28] => inline-edit-tax.js
[29] => link.dev.js
[30] => link.js
[31] => media-gallery.dev.js
[32] => media-gallery.js
[33] => media-upload.dev.js
[34] => media-upload.js
[35] => media.dev.js
[36] => media.js
[37] => nav-menu.dev.js
[38] => nav-menu.js
[39] => password-strength-meter.dev.js
[40] => password-strength-meter.js
[41] => plugin-install.dev.js
[42] => plugin-install.js
[43] => post.dev.js
[44] => post.js
[45] => postbox.dev.js
[46] => postbox.js
[47] => revisions-js.php
[48] => set-post-thumbnail.dev.js
[49] => set-post-thumbnail.js
[50] => tags.dev.js
[51] => tags.js
[52] => theme-preview.dev.js
[53] => theme-preview.js
[54] => theme.dev.js
[55] => theme.js
[56] => user-profile.dev.js
[57] => user-profile.js
[58] => user-suggest.dev.js
[59] => user-suggest.js
[60] => utils.dev.js
[61] => utils.js
[62] => widgets.dev.js
[63] => widgets.js
[64] => word-count.dev.js
[65] => word-count.js
[66] => wp-fullscreen.dev.js
[67] => wp-fullscreen.js
[68] => xfn.dev.js
[69] => xfn.js
)

[28] => link-add.php
[29] => link-manager.php
[30] => link-parse-opml.php
[31] => link.php
[32] => load-scripts.php
[33] => load-styles.php
[maint] => Array
(
[0] => repair.php
)

[34] => media-new.php
[35] => media-upload.php
[36] => media.php
[37] => menu-header.php
[38] => menu.php
[39] => moderation.php
[40] => ms-admin.php
[41] => ms-delete-site.php
[42] => ms-edit.php
[43] => ms-options.php
[44] => ms-sites.php
[45] => ms-themes.php
[46] => ms-upgrade-network.php
[47] => ms-users.php
[48] => my-sites.php
[49] => nav-menus.php
[network] => Array
(
[0] => about.php
[1] => admin.php
[2] => credits.php
[3] => edit.php
[4] => freedoms.php
[5] => index-extra.php
[6] => index.php
[7] => menu.php
[8] => plugin-editor.php
[9] => plugin-install.php
[10] => plugins.php
[11] => profile.php
[12] => settings.php
[13] => setup.php
[14] => site-info.php
[15] => site-new.php
[16] => site-settings.php
[17] => site-themes.php
[18] => site-users.php
[19] => sites.php
[20] => theme-editor.php
[21] => theme-install.php
[22] => themes.php
[23] => update-core.php
[24] => update.php
[25] => upgrade.php
[26] => user-edit.php
[27] => user-new.php
[28] => users.php
)

[50] => network.php
[51] => options-discussion.php
[52] => options-general.php
[53] => options-head.php
[54] => options-media.php
[55] => options-permalink.php
[56] => options-privacy.php
[57] => options-reading.php
[58] => options-writing.php
[59] => options.php
[60] => plugin-editor.php
[61] => plugin-install.php
[62] => plugins.php
[63] => post-new.php
[64] => post.php
[65] => press-this.php
[66] => profile.php
[67] => revision.php
[68] => setup-config.php
[69] => theme-editor.php
[70] => theme-install.php
[71] => themes.php
[72] => tools.php
[73] => update-core.php
[74] => update.php
[75] => upgrade-functions.php
[76] => upgrade.php
[77] => upload.php
[user] => Array
(
[0] => about.php
[1] => admin.php
[2] => credits.php
[3] => freedoms.php
[4] => index-extra.php
[5] => index.php
[6] => menu.php
[7] => profile.php
[8] => user-edit.php
)

[78] => user-edit.php
[79] => user-new.php
[80] => users.php
[81] => widgets.php
)
[/sourcecode]

Selamat mencobanya!

NB. Thanks to CodeIgniter.

Terbaru

  • Cara Memperbaiki Error Windows “Failed to update the system registry”
  • Cara Memperaiki LGPO/exe/g
  • Cara Memperbaiki Error Tidak bisa Add Calendar di Outlook
  • Cara Memperbaiki File Transfer Drop ke 0 di Windows 11
  • Cara Memperbaiki Microsoft Copilot Error di Outlook
  • Cara Memperbaiki Error Virtualbox NtCreateFile(\Device\VBoxDrvStub) failed, Not signed with the build certificate
  • Cara Memperbaiki Error “the system detected an address conflict for an IP address, with Event ID 4199”
  • Cara Memperbaiki Password Microsoft Edge yang Hilang
  • Cara Memperbaiki Email Outlook yang Hilang atau Tidak Muncul
  • Cara Menemukan Username dan Password di Windows 11
  • Cara Mengatasi Error Virtualbox not detecting Graphics Card di Windows 11
  • Cara Mengatasi Error Windows MFReadWrite.dll not found or missing
  • Cara Membuat Formulir Menggunakan Zoho Form
  • Pemerintah Ganti Ujian Kesetaraan Dengan TKA 2025
  • Ini Perbedaan TKA vs Ujian Nasional: TKA Lebih Sakti?
  • Daftar TKA Tutup 5 Oktober: Sudah 3.3 Juta Yang Daftar
  • Review Aplikasi ClipClaps: Penipuan atau Tidak?
  • Review Aplikasi Wibuku: Alternatif Nonton Anime Gratis untuk Para Wibu Indonesia!
  • Inilah Alat dan Software Phone Farming dengan Samsung Galaxy J7 Prime
  • Cara Cek Paket Internet Telkomsel Kena Pembatasan/Throttling Atau Tidak
  • Cara Mengatasi YMusic APK Error Tidak Bisa Dibuka
  • Cara Memblokir Akun Teman di Mobile Legend: Panduan Lengkap
  • Profil Farida Farichah, Wakil Menteri Koperasi Kabinet Merah Putih Reshuffle 17 September 2025
  • Ini Info Terbaru Pencairan BSU BPJS Ketenagakerjaan 2025!
  • Cara Reset Printer Epson L3110 2025
  • WhatsApp Tiba-tiba Keluar dan Meminta Verifikasi: Apa yang Harus Dilakukan?
  • Bisakah Saldo BNI Kamu Nol? Fakta dan Cara Mengatasinya
  • Inilah Tanda-tanda Chat Audio di Grup WhatsApp Sudah Disadap
  • Cara Mengatasi Tidak Bisa Live Instagram Karena Tidak Memenuhi Syarat
  • 7 Spek Laptop yang Ideal untuk Coding & Ngoding Web/App
  • Cara Memperbaiki Error Windows “Failed to update the system registry”
  • Cara Memperaiki LGPO/exe/g
  • Cara Memperbaiki Error Tidak bisa Add Calendar di Outlook

©2025 emka.web.id | Design: Newspaperly WordPress Theme